1. 問題背景
工作中遇到這樣的場(chǎng)景:某個(gè)方法需要在不同的業(yè)務(wù)場(chǎng)景下執(zhí)行特定的邏輯,該方法已經(jīng)上生產(chǎn),不想改變?cè)瓉淼拇a,因此決定用AOP做個(gè)切面執(zhí)行邏輯。
2. 不啰嗦,上代碼
以下為核心代碼:
定義注解:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@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(); } |
定義切面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@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ù)代碼加注解
1
2
3
4
5
6
7
8
|
@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),切面的代碼沒有執(zhí)行。
3. 問題排查
3.1 是不是切點(diǎn)寫得有問題,于是換成如下形式:
1
2
3
4
5
6
7
8
9
10
11
|
@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()); } } |
還是不行,但是我的工程中其他地方也是類似的寫法卻沒有問題啊。看起來不像是AOP配置不對(duì)的問題
3.2 是不是使用的地方不是代理對(duì)象
打斷點(diǎn)吧,如下:
是使用cglib生成的代理對(duì)象,沒有問題啊,到底問題在哪里。沒辦法,面向百度編程吧,還真找到問題解決辦法。如下帖子:http://www.ythuaji.com.cn/article/209596.html
4. 問題原因
對(duì)于可重復(fù)注解,如果方法上用多個(gè)可重復(fù)注解,AOP攔截不到。需要用它的包裝類型注解做切點(diǎn),改成以下代碼就可以了:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@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ù)注解沒有生效問題的文章就介紹到這了,更多相關(guān)Spring AOP注解沒有生效內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/qq_39530821/article/details/119900335