更新時間:2022-11-01 09:35:00 來源:動力節(jié)點 瀏覽1683次
顧名思義,面向切面編程(AOP)在編程中使用方面。它可以定義為將代碼分解為不同的模塊,也稱為模塊化,其中方面是模塊化的關(guān)鍵單元。方面支持橫切關(guān)注點的實現(xiàn),例如事務(wù)、日志記錄,這些不是業(yè)務(wù)邏輯的核心,而不會將代碼核心與其功能混為一談。它通過添加作為現(xiàn)有代碼建議的附加行為來實現(xiàn)。例如,安全性是一個橫切關(guān)注點,在應(yīng)用程序中的許多方法中都可以應(yīng)用安全規(guī)則,因此在每個方法中重復(fù)代碼,在公共類中定義功能,并控制在整個應(yīng)用程序中應(yīng)用該功能。
AOP包括支持和實現(xiàn)代碼模塊化的編程方法和框架。讓我們看一下AOP 中的三個主要框架:
方面:實現(xiàn) JEE 應(yīng)用程序橫切關(guān)注點(事務(wù)、記錄器等)的類稱為方面。它可以是通過 XML 配置或通過使用 @Aspect 注釋的常規(guī)類配置的普通類。
編織:將方面與建議對象聯(lián)系起來的過程。它可以在加載時、編譯時或運(yùn)行時完成。Spring AOP 在運(yùn)行時進(jìn)行編織。
讓我們編寫我們的第一個方面類,但在此之前看看所需的 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");
}
}
建議:本應(yīng)由 Aspect 完成的工作,或者可以定義為 Aspect 在特定點采取的行動。Advice 有五種類型,即:Before、After、Around、AfterThrowing 和 AfterReturning。讓我們對所有五種類型進(jìn)行簡要討論。
建議類型:
之前:在調(diào)用建議的方法之前運(yùn)行。它由@Before注釋表示。
After:無論結(jié)果如何,無論成功與否,都在建議的方法完成后運(yùn)行。它由@After注釋表示。
AfterReturning:在建議的方法成功完成后運(yùn)行,即沒有任何運(yùn)行時異常。它由@AfterReturning注釋表示。
Around:這是所有建議中最強(qiáng)的建議,??因為它環(huán)繞并在建議方法之前和之后運(yùn)行。這種類型的建議用于我們需要頻繁訪問方法或數(shù)據(jù)庫(如緩存)的地方。它由@Around注釋表示。
AfterThrowing:在建議的方法引發(fā)運(yùn)行時異常后運(yùn)行。它由@AfterThrowing注解表示。
讓我們在 Aspect 類 Logger 中實現(xiàn)所有 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:一個應(yīng)用程序有數(shù)以千計的機(jī)會或點來應(yīng)用 Advice。這些點稱為連接點。例如,可以在每次調(diào)用方法或拋出異常時或在其他各個點應(yīng)用建議。但是 Spring AOP 目前只支持方法執(zhí)行連接點(建議在 Spring bean 上執(zhí)行方法)。
讓我們看看連接點在我們的@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());
}
}
切入點:由于在代碼的每個點都應(yīng)用建議是不可行的,因此,最終應(yīng)用建議的選定連接點稱為切入點。通常,您使用顯式的類和方法名稱或通過定義匹配的類和方法名稱模式的正則表達(dá)式來指定這些切入點。它有助于通過編寫一次并在多個點使用來減少重復(fù)代碼,讓我們看看如何。
// 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");
}
}
以上就是關(guān)于“Spring框架中的面向切面編程和AOP”的介紹,大家如果對此比較感興趣,想了解更多相關(guān)知識,不妨來關(guān)注一下本站的Spring教程,里面還有更豐富的知識等著大家去學(xué)習(xí),相信對大家一定會有所幫助的。
初級 202925
初級 203221
初級 202629
初級 203743