詳解Spring AOP自定義可重復注解沒有生效問題
1. 問題背景
工作中遇到這樣的場景:某個方法需要在不同的業(yè)務場景下執(zhí)行特定的邏輯,該方法已經上生產,不想改變原來的代碼,因此決定用AOP做個切面執(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è)務代碼加注解
@StartTaskRun(businessType = 5)
@StartTaskRun(businessType = 6)
@Override
@Transactional(rollbackFor = Exception.class)
public String doCsmsStrategy(Long id) {
// 業(yè)務邏輯
return userDO.getId().toString();
}
debug的時候發(fā)現(xiàn),切面的代碼沒有執(zhí)行。
3. 問題排查
3.1 是不是切點寫得有問題,于是換成如下形式:
@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配置不對的問題
3.2 是不是使用的地方不是代理對象
打斷點吧,如下:

是使用cglib生成的代理對象,沒有問題啊,到底問題在哪里。沒辦法,面向百度編程吧,還真找到問題解決辦法。如下帖子:http://www.dbjr.com.cn/article/220762.htm
4. 問題原因
對于可重復注解,如果方法上用多個可重復注解,AOP攔截不到。需要用它的包裝類型注解做切點,改成以下代碼就可以了:
@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());
}
}
}
到此這篇關于詳解Spring AOP自定義可重復注解沒有生效問題的文章就介紹到這了,更多相關Spring AOP注解沒有生效內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringMVC參數(shù)的傳遞之如何接收List數(shù)組類型的數(shù)據(jù)
這篇文章主要介紹了SpringMVC參數(shù)的傳遞之如何接收List數(shù)組類型的數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
MyBatis-Plus與PageHelper依賴的jsqlparser庫沖突
在升級SpringBoot到3.x版本的同時,升級MyBatis-Plus后發(fā)現(xiàn)PageHelper無法使用,原因是MyBatis-Plus和PageHelper都依賴jsqlparser庫,且PageHelper要求特定版本的jsqlparser,解決方法是在項目中排除這兩個庫的jsqlparser依賴,直接引用jsqlparser4.7版本2024-10-10
Java 在PPT中創(chuàng)建散點圖的實現(xiàn)示例
本文將以Java代碼示例展示如何在PPT幻燈片中創(chuàng)建散點圖表。文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11

