简体   繁体   中英

method parameter aspectj is not working spring boot

I have created an annotation with name Validation and inject on method parameter and I have been using aspect before invocation to validate my object. It is not working

Annotation code

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

Aop Code

@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() {
    }
}

Using annotation as public TrackingId createNewOrder(@Validation Order newOrder)

This is called before any method that has argument annotated with @Validation , I think that's what you wanted:

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

}

If you don't want the parameter value then just remove the && ... part and method argument.

@Before("execution(* *(.., @Validation (*), ..)) && args(.., toVerify)")
  |          |     | |  |     |         |   |    |   |    |      |
  1          2     3 4  5     6         7   8    9   10   11     12
  1. advice called before a join point
  2. matching method execution join point
  3. method visibility , matching any (public, private etc.)
  4. method name , matching any
  5. in case your argument is not the only one, it can be between other arguments (not annotated with @Validation )
  6. the annotation you are looking for
  7. types of arguments to the annotation
  8. see 5.
  9. to combine expressions
  10. making the argument available to your method
  11. as in 5 and 8, it may not be the first argument
  12. name of the argument , same as in method signature ( Object toVerify )

Be careful if you have methods with multiple parameters, some annotated, some not, and other combinations - not sure it's gonna always work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM