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