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

本文介绍了在Spring AOP中遇到的一个问题,即当方法上使用了多个可重复注解时,切面无法正常拦截。作者通过逐步排查,发现AOP切点表达式不适用于处理可重复注解的情况。最终,通过修改切点表达式,将可重复注解的包装类型加入切点,成功解决了问题。
摘要由CSDN通过智能技术生成

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.cnblogs.com/wulm/p/14386372.html

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值