这篇文章主要为大家展示了“Spring AOP自定义可重复注解没有生效的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Spring AOP自定义可重复注解没有生效的示例分析”这篇文章吧。
工作中遇到这样的场景:某个方法需要在不同的业务场景下执行特定的逻辑,该方法已经上生产,不想改变原来的代码,因此决定用AOP做个切面执行逻辑。
以下为核心代码:
定义注解:
@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());
}
}
}
业务代码加注解
@StartTaskRun(businessType = 5)
@StartTaskRun(businessType = 6)
@Override
@Transactional(rollbackFor = Exception.class)
public String doCsmsStrategy(Long id) {
// 业务逻辑
return userDO.getId().toString();
}
debug的时候发现,切面的代码没有执行。
@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配置不对的问题
打断点吧,如下:
是使用cglib生成的代理对象,没有问题啊,到底问题在哪里。没办法,面向百度编程吧,还真找到问题解决办法。如下帖子:https://www.yisu.com/article/220762.htm
对于可重复注解,如果方法上用多个可重复注解,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自定义可重复注解没有生效的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。