詳解Spring AOP自定義可重復(fù)注解沒(méi)有生效問(wèn)題
1. 問(wèn)題背景
工作中遇到這樣的場(chǎng)景:某個(gè)方法需要在不同的業(yè)務(wù)場(chǎng)景下執(zhí)行特定的邏輯,該方法已經(jīng)上生產(chǎn),不想改變?cè)瓉?lái)的代碼,因此決定用AOP做個(gè)切面執(zhí)行邏輯。
2. 不啰嗦,上代碼
以下為核心代碼:
定義注解:
@Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented @Repeatable(value = StartTaskRuns.class) public @interface StartTaskRun { int businessType() default 0; } @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface StartTaskRuns { StartTaskRun[] value(); }
定義切面
@Aspect @Component public class StartTaskRunAspect { @AfterReturning(pointcut = "@annotation(com.freedom.code.annotation.StartTaskRun)", returning = "retValue") public void startTask(JoinPoint joinPoint, Object retValue) throws Exception { Object[] args = joinPoint.getArgs(); Signature signature = joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature.getMethod(); StartTaskRun[] annotations = method.getAnnotationsByType(StartTaskRun.class); for (StartTaskRun annotation : annotations) { System.out.println(annotation.businessType()); } } }
業(yè)務(wù)代碼加注解
@StartTaskRun(businessType = 5) @StartTaskRun(businessType = 6) @Override @Transactional(rollbackFor = Exception.class) public String doCsmsStrategy(Long id) { // 業(yè)務(wù)邏輯 return userDO.getId().toString(); }
debug的時(shí)候發(fā)現(xiàn),切面的代碼沒(méi)有執(zhí)行。
3. 問(wèn)題排查
3.1 是不是切點(diǎn)寫(xiě)得有問(wèn)題,于是換成如下形式:
@AfterReturning(pointcut = "execution(* com.freedom.code.service.UserServiceImpl.doCsmsStrategy(..))", returning = "retValue") public void startTask(JoinPoint joinPoint, Object retValue) throws Exception { Object[] args = joinPoint.getArgs(); Signature signature = joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature.getMethod(); StartTaskRun[] annotations = method.getAnnotationsByType(StartTaskRun.class); for (StartTaskRun annotation : annotations) { System.out.println(annotation.businessType()); } }
還是不行,但是我的工程中其他地方也是類(lèi)似的寫(xiě)法卻沒(méi)有問(wèn)題啊。看起來(lái)不像是AOP配置不對(duì)的問(wèn)題
3.2 是不是使用的地方不是代理對(duì)象
打斷點(diǎn)吧,如下:
是使用cglib生成的代理對(duì)象,沒(méi)有問(wèn)題啊,到底問(wèn)題在哪里。沒(méi)辦法,面向百度編程吧,還真找到問(wèn)題解決辦法。如下帖子:http://www.dbjr.com.cn/article/220762.htm
4. 問(wèn)題原因
對(duì)于可重復(fù)注解,如果方法上用多個(gè)可重復(fù)注解,AOP攔截不到。需要用它的包裝類(lèi)型注解做切點(diǎn),改成以下代碼就可以了:
@Aspect @Component public class StartTaskRunAspect { @AfterReturning(pointcut = "@annotation(com.freedom.code.annotation.StartTaskRun) || @annotation(com.freedom.code.annotation.StartTaskRuns)", returning = "retValue") public void startTask(JoinPoint joinPoint, Object retValue) throws Exception { Object[] args = joinPoint.getArgs(); Signature signature = joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature.getMethod(); StartTaskRun[] annotations = method.getAnnotationsByType(StartTaskRun.class); for (StartTaskRun annotation : annotations) { System.out.println(annotation.businessType()); } } }
到此這篇關(guān)于詳解Spring AOP自定義可重復(fù)注解沒(méi)有生效問(wèn)題的文章就介紹到這了,更多相關(guān)Spring AOP注解沒(méi)有生效內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java隊(duì)列數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn)
這篇文章主要介紹了Java隊(duì)列數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn),隊(duì)列是一種特殊的線(xiàn)性表,只允許在表的隊(duì)頭進(jìn)行刪除操作,在表的后端進(jìn)行插入操作,隊(duì)列是一個(gè)有序表先進(jìn)先出,想了解更多相關(guān)資料的小伙伴可以參考下面文章的詳細(xì)內(nèi)容2021-12-12記一次Feign中實(shí)現(xiàn)傳實(shí)體Bean的問(wèn)題
這篇文章主要介紹了記一次Feign中如何傳實(shí)體Bean的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03SpringMVC參數(shù)的傳遞之如何接收List數(shù)組類(lèi)型的數(shù)據(jù)
這篇文章主要介紹了SpringMVC參數(shù)的傳遞之如何接收List數(shù)組類(lèi)型的數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10MyBatis-Plus與PageHelper依賴(lài)的jsqlparser庫(kù)沖突
在升級(jí)SpringBoot到3.x版本的同時(shí),升級(jí)MyBatis-Plus后發(fā)現(xiàn)PageHelper無(wú)法使用,原因是MyBatis-Plus和PageHelper都依賴(lài)jsqlparser庫(kù),且PageHelper要求特定版本的jsqlparser,解決方法是在項(xiàng)目中排除這兩個(gè)庫(kù)的jsqlparser依賴(lài),直接引用jsqlparser4.7版本2024-10-10Java 在PPT中創(chuàng)建散點(diǎn)圖的實(shí)現(xiàn)示例
本文將以Java代碼示例展示如何在PPT幻燈片中創(chuàng)建散點(diǎn)圖表。文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11