更新時間:2022-11-01 09:35:00 來源:動力節點 瀏覽1733次
顧名思義,面向切面編程(AOP)在編程中使用方面。它可以定義為將代碼分解為不同的模塊,也稱為模塊化,其中方面是模塊化的關鍵單元。方面支持橫切關注點的實現,例如事務、日志記錄,這些不是業務邏輯的核心,而不會將代碼核心與其功能混為一談。它通過添加作為現有代碼建議的附加行為來實現。例如,安全性是一個橫切關注點,在應用程序中的許多方法中都可以應用安全規則,因此在每個方法中重復代碼,在公共類中定義功能,并控制在整個應用程序中應用該功能。
AOP包括支持和實現代碼模塊化的編程方法和框架。讓我們看一下AOP 中的三個主要框架:
方面:實現 JEE 應用程序橫切關注點(事務、記錄器等)的類稱為方面。它可以是通過 XML 配置或通過使用 @Aspect 注釋的常規類配置的普通類。
編織:將方面與建議對象聯系起來的過程。它可以在加載時、編譯時或運行時完成。Spring AOP 在運行時進行編織。
讓我們編寫我們的第一個方面類,但在此之前看看所需的 jars 和 AOP 的 Bean 配置文件。
package com.aspect
import org.aspectj.lang.annotation.Aspect;
import Java.lang.RuntimeException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
// Logging class is anotated with @Aspect
// and will contain advices.
@Aspect
class Logging {
}
// The class ImplementAspect
// contains method Aspectcall
// and the advices will be applied
// on that method for demo.
public class ImplementAspect {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("my first aspect");
// **Add beanconfiguration file
// in your programme when executing.**
ApplicationContext ctx
= new ClassPathXmlApplicationContext("beanconfigfile.XML");
ImplementAspect call
= (ImplementAspect)ctx.getbean("aspect");
System.out.println("enter an integer");
int a = sc.nextInt();
if (a == 1) {
throw new RuntimeException(msg);
}
else {
call.aspectCall();
}
call.myMethod();
}
public void aspectCall()
{
System.out.println("Applying advices"
+ " for the first time");
}
public void myMethod()
{
System.out.println("This is an"
+ " extra method");
}
}
建議:本應由 Aspect 完成的工作,或者可以定義為 Aspect 在特定點采取的行動。Advice 有五種類型,即:Before、After、Around、AfterThrowing 和 AfterReturning。讓我們對所有五種類型進行簡要討論。
建議類型:
之前:在調用建議的方法之前運行。它由@Before注釋表示。
After:無論結果如何,無論成功與否,都在建議的方法完成后運行。它由@After注釋表示。
AfterReturning:在建議的方法成功完成后運行,即沒有任何運行時異常。它由@AfterReturning注釋表示。
Around:這是所有建議中最強的建議,??因為它環繞并在建議方法之前和之后運行。這種類型的建議用于我們需要頻繁訪問方法或數據庫(如緩存)的地方。它由@Around注釋表示。
AfterThrowing:在建議的方法引發運行時異常后運行。它由@AfterThrowing注解表示。
讓我們在 Aspect 類 Logger 中實現所有 5 條建議
// Program to show types of Advices
@Aspect
class Logging {
// Implementing all the five pieces of advice
// to execute AfterThrowing advice enter integer value as 1.
// **Before**
@Before("execution(public void com.aspect.ImplementAspect.aspectCall())")
public void loggingAdvice1()
{
System.out.println("Before advice is executed");
}
// **After**
@After("execution(public void com.aspect.ImplementAspect.aspectCall())")
public void loggingAdvice2()
{
System.out.println("Running After Advice.");
}
// **Around**
@Around("execution(public void com.aspect.ImplementAspect.myMethod())")
public void loggingAdvice3()
{
System.out.println("Before and After invoking method myMethod");
}
// **AfterThrowing**
@AfterThrowing("execution(" public void com.aspect.ImplementAspect.aspectCall())
")
public void
loggingAdvice4()
{
System.out.println("Exception thrown in method");
}
// **AfterRunning**
@AfterReturning("execution(public void com.aspect.ImplementAspect.myMethod())")
public void loggingAdvice5()
{
System.out.println("AfterReturning advice is run");
}
}
JoinPoints:一個應用程序有數以千計的機會或點來應用 Advice。這些點稱為連接點。例如,可以在每次調用方法或拋出異常時或在其他各個點應用建議。但是 Spring AOP 目前只支持方法執行連接點(建議在 Spring bean 上執行方法)。
讓我們看看連接點在我們的@Aspect 類(Logger)中做了什么
// Program to show JoinPoints
@Aspect
class Logging {
// Passing a JoinPoint Object
// into parameters of the method
// with the annotated advice
// enables to print the information
/// when the advice is executed
// with the help of toString() method
// present in it.
@Before("execution(public void com.aspect.ImplementAspect.aspectCall())")
public void loggingAdvice1(JoinPoint joinpoint)
{
System.out.println("Before advice is executed");
System.out.println(joinpoint.toString());
}
}
切入點:由于在代碼的每個點都應用建議是不可行的,因此,最終應用建議的選定連接點稱為切入點。通常,您使用顯式的類和方法名稱或通過定義匹配的類和方法名稱模式的正則表達式來指定這些切入點。它有助于通過編寫一次并在多個點使用來減少重復代碼,讓我們看看如何。
// Program to shgw PointCuts
@Aspect
class Logging {
// pointcut() is a dummy method
// required to hold @Pointcut annotation
// pointcut() can be used instead of writing line 1
// whenever required, as done in line 4.
// This prevents a repetition of code.
@Pointcut("execution(public void com.aspect.ImplementAspect.aspectCall())") // line 1
public void pointCut()
{
}
// pointcut() is used to avoid repetition of code
@Before("pointcut()")
public void loggingAdvice1()
{
System.out.println("Before advice is executed");
}
}
以上就是關于“Spring框架中的面向切面編程和AOP”的介紹,大家如果對此比較感興趣,想了解更多相關知識,不妨來關注一下本站的Spring教程,里面還有更豐富的知識等著大家去學習,相信對大家一定會有所幫助的。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習