Spring基于注解配置AOP詳解
一、概述
Spring 項(xiàng)目使用 AOP 功能需要定義三個(gè)部分:切面、切點(diǎn)和通知。
二、AOP 使用
Spring 基于注解配置 AOP 需要啟用 AspectJ 自動(dòng)代理功能。
基于 Java 配置
@Configuration
@EnableAspectJAutoProxy
public?class?AppConfig?{
}基于 XML 配置
<aop:aspectj-autoproxy/>
1. 定義切面
在 Spring 管理的 Bean 類(lèi)上使用 @Aspect 注解就可以定義一個(gè)切面。
@Aspect
@Component
public?class?DemoAspect?{
}2. 定義切點(diǎn)
在切面類(lèi)的方法使用 @Pointcut 注解來(lái)定義切點(diǎn),然后在通知注解中使用方法簽名來(lái)指定切點(diǎn)。
切點(diǎn)表達(dá)式用來(lái)匹配切入的目標(biāo)類(lèi)和方法。目標(biāo)類(lèi)只能是 Spring 容器管理的類(lèi),切面只能切入 Bean 中的方法。
@Aspect
@Component
public?class?DemoAspect?{
????@Pointcut("execution(* cn.codeartist.spring.aop.aspectj.*.*(..))")
????public?void?pointcut()?{
????}
????@Before("pointcut()")
????public?void?doBefore(JoinPoint joinPoint)?{
????????// do sometding
????}
}切點(diǎn)表達(dá)式也可以在定義通知的時(shí)候指定,而不需要使用 @Pointcut 注解。
@Aspect
@Component
public?class?DemoAspect?{
????@Before("execution(* cn.codeartist.spring.aop.aspectj.*.*(..))")
????public?void?doBefore(JoinPoint joinPoint)?{
????????// do sometding
????}
}3. 定義通知
定義通知的時(shí)候需要指定切點(diǎn),通知的類(lèi)型決定了切入的節(jié)點(diǎn)。

前置通知
使用 @Before 注解定義前置通知,在方法執(zhí)行前添加操作。
@Aspect
@Component
public?class?DemoAspect?{
????@Before("pointcut()")
????public?void?doBefore(JoinPoint joinPoint)?{
????????// do sometding
????}
}后置通知
使用 @AfterReturning 注解定義后置通知,在方法正常返回時(shí)執(zhí)行,方法拋異常不執(zhí)行。
@Aspect
@Component
public?class?DemoAspect?{
????@AfterReturning("pointcut()")
????public?void?doAfterReturning(JoinPoint joinPoint)?{
????????// do sometding
????}
}環(huán)繞通知
使用 @Around 注解定義環(huán)繞通知,切入方法前后,相當(dāng)于攔截器的功能,可以捕獲異常處理。
環(huán)繞通知的切入點(diǎn)參數(shù)為 ProceedingJoinPoint,并且需要手動(dòng)調(diào)用 proceed() 來(lái)執(zhí)行切入點(diǎn)方法的邏輯。
@Aspect
@Component
public?class?DemoAspect?{
????@Around("pointcut()")
????public?Object?doAround(ProceedingJoinPoint joinPoint)?tdrows?tdrowable?{
????????// do sometding
????????Object proceed = joinPoint.proceed();
????????// do sometding
????????return?proceed;
????}
}最終通知
使用 @After 注解定義最終通知,在方法退出時(shí)執(zhí)行,無(wú)論是正常退出還是異常退出。
@Aspect
@Component
public?class?DemoAspect?{
????@After("pointcut()")
????public?void?doAfter(JoinPoint joinPoint)?{
????????// do sometding
????}
}異常通知
使用 @Aftertdrowing 注解定義異常通知,在方法拋出異常時(shí)執(zhí)行。
@Aspect
@Component
public?class?DemoAspect?{
????@Aftertdrowing("pointcut()")
????public?void?doAftertdrowing(JoinPoint joinPoint)?{
????????// do sometding
????}
}4. 通過(guò) Advisor 實(shí)現(xiàn)
使用 Advisor 能以編程的方式創(chuàng)建切面,需要實(shí)現(xiàn)通知的 API 來(lái)定義通知的類(lèi)型。
比起使用注解定義切點(diǎn),這種方式指定切點(diǎn)表達(dá)式更靈活。
@Bean
public AspectJExpressionPointcutAdvisor aspectJExpressionPointcutAdvisor() {
AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor();
advisor.setExpression("execution(* cn.codeartist.spring.aop.aspectj.*.*(..))");
advisor.setAdvice((MetdodBeforeAdvice) (metdod, args, target) -> {
// do sometding
});
return advisor;
}三、附錄
1. 常用配置
| 配置 | 描述 |
| <aop:aspectj-autoproxy/> | 啟用 AspectJ 自動(dòng)代理,通過(guò)注解定義切面 |
2. 常用注解
| 注解 | 描述 |
| @EnableAspectJAutoProxy | 啟用 AspectJ 自動(dòng)代理,通過(guò)注解定義切面 |
| @Aspect | 定義切面類(lèi) |
| @Before | 定義前置通知 |
| @AfterReturning | 定義后置通知 |
| @Around | 定義環(huán)繞通知 |
| @After | 定義最終通知 |
| @Aftertdrowing | 定義異常通知 |
到此這篇關(guān)于Spring基于注解配置AOP詳解的文章就介紹到這了,更多相關(guān)Spring注解配置AOP內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Idea開(kāi)發(fā)工具之SpringBoot整合JSP的過(guò)程
最近在學(xué)習(xí)SpringBoot,看到SpringBoot整合jsp,順帶記錄一下。本文通過(guò)圖文實(shí)例相結(jié)合給大家講解SpringBoot整合JSP的過(guò)程,感興趣的朋友一起看看吧2021-09-09
SpringBoot+Resilience4j實(shí)現(xiàn)接口限流的示例代碼
Resilience4j 是一個(gè)用于實(shí)現(xiàn)熔斷、限流、重試等功能的輕量級(jí)庫(kù),本文主要介紹了SpringBoot+Resilience4j實(shí)現(xiàn)接口限流的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2024-12-12
MybatisPlus EntityWrapper如何自定義SQL
這篇文章主要介紹了MybatisPlus EntityWrapper如何自定義SQL,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Spring 處理 HTTP 請(qǐng)求參數(shù)注解的操作方法
這篇文章主要介紹了Spring 處理 HTTP 請(qǐng)求參數(shù)注解的操作方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友參考下吧2024-04-04
Spring項(xiàng)目中使用Junit單元測(cè)試并配置數(shù)據(jù)源的操作
這篇文章主要介紹了Spring項(xiàng)目中使用Junit單元測(cè)試并配置數(shù)據(jù)源的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09

