代码之家  ›  专栏  ›  技术社区  ›  Bibek Shakya Dev. Joel

方法参数aspectj在spring boot中不起作用

  •  0
  • Bibek Shakya Dev. Joel  · 技术社区  · 6 年前

    注释代码

    @Target(ElementType.PARAMETER)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Validation {}
    

    Aop代码

    @Aspect
    @Component
    public class ValidatorAOP {
        @Before("valditionAnnotation()")
        public void validate(final JoinPoint jp) throws Throwable {
            Validator object = (Validator) jp.getTarget();
            object.validator();
        }
        @Pointcut("@annotation(Validation)")
        public void valditionAnnotation() {
        }
    }
    

    public TrackingId createNewOrder(@Validation Order newOrder)

    1 回复  |  直到 6 年前
        1
  •  1
  •   kriegaex    6 年前

    这是在任何带有参数注释的方法之前调用的 @Validation ,我想这就是你想要的:

    @Before("execution(* *(.., @Validation (*), ..)) && args(.., toVerify)")
    public void validate(final JoinPoint joinPoint, final Object toVerify) {
    
    }
    

    如果不需要参数值,只需删除 && ... 部分和方法论证。

    @Before("execution(* *(.., @Validation (*), ..)) && args(.., toVerify)")
      |          |     | |  |     |         |   |    |   |    |      |
      1          2     3 4  5     6         7   8    9   10   11     12
    
    1. 电话咨询 之前 连接点
    2. 匹配方法
    3. 方法可见性 ,匹配任何(公共、私人等)
    4. ,匹配任何
    5. 如果您的参数不是唯一的参数,那么它可以位于其他参数之间(不带注释) @验证
    6. 你在找什么
    7. 注释的参数
    8. 见第5条。
    9. 组合表达式
    10. 提供论点 按照你的方法
    11. 正如第5条和第8条所述,这可能不是第一个论点
    12. 参数名称 ,与方法签名中的相同( Object toVerify )