广告
返回顶部
首页 > 资讯 > 后端开发 > PHP编程 >详解Spring AOP自定义可重复注解没有生效问题
  • 905
分享到

详解Spring AOP自定义可重复注解没有生效问题

SpringAOP注解没有生效SpringAOP可重复注解 2019-04-16 16:04:36 905人浏览 绘本
摘要

目录1. 问题背景2. 不啰嗦,上代码3. 问题排查3.1 是不是切点写得有问题,于是换成如下形式:3.2 是不是使用的地方不是代理对象4. 问题原因1. 问题背景 工作中遇到这样的场景:某个方法需要在不同的业务场景下执行特定的逻辑,该方法

目录
  • 1. 问题背景
  • 2. 不啰嗦,上代码
  • 3. 问题排查
    • 3.1 是不是切点写得有问题,于是换成如下形式:
    • 3.2 是不是使用的地方不是代理对象
  • 4. 问题原因

    1. 问题背景

    工作中遇到这样的场景:某个方法需要在不同的业务场景下执行特定的逻辑,该方法已经上生产,不想改变原来的代码,因此决定用aop做个切面执行逻辑。

    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());
        }
      }
    }

    业务代码加注解

    
      @StartTaskRun(businessType = 5)
      @StartTaskRun(businessType = 6)
      @Override
      @Transactional(rollbackFor = Exception.class)
      public String doCsmsStrategy(Long id) {
        // 业务逻辑
        return userDO.getId().toString();
      }
    

    debug的时候发现,切面的代码没有执行。

    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生成的代理对象,没有问题啊,到底问题在哪里。没办法,面向百度编程吧,还真找到问题解决办法。如下帖子:https://www.jb51.net/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注解没有生效内容请搜索编程界以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程界!

    --结束END--

    本文标题: 详解Spring AOP自定义可重复注解没有生效问题

    本文链接: https://www.lsjlt.com/news/81.html(转载时请注明来源链接)

    有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

    本篇文章演示代码以及资料文档资料下载

    下载Word文档到电脑,方便收藏和打印~

    下载Word文档
    猜你喜欢
    软考高级职称资格查询
    编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
    • 官方手机版

    • 微信公众号

    • 商务合作