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