目录

    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生成的代理对象,没有问题啊,到底问题在哪里。没办法,面向百度编程吧,还真找到问题解决办法。如下帖子:

    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注解没有生效内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!