Spring AOP pointcut for annotated argument Spring AOP pointcut for annotated argument spring spring

Spring AOP pointcut for annotated argument


On selecting your arguments :

@Before("execution(* *(@CustomAnnotation (*)))")public void advice() {System.out.println("hello");}

ref : http://forum.springsource.org/archive/index.php/t-61308.html

On getting the annotation param :

MethodSignature signature = (MethodSignature) joinPoint.getSignature();Method method = signature.getMethod();Annotation[][] methodAnnotations = method.getParameterAnnotations();

Will get you the annotations which you can iterate and use instanceof to find your bound annotation. I know thats hacky but afaik this is the only way supported currently.


To match 1..N annotated arguments regardless of their position use

Edit 2020-12-3: Simplified version from @sgflt

@Before("execution(* *(.., @CustomAnnotation (*), ..))")public void advice(int arg1, @CustomAnnotation("val") Object arg2) { ... }


Just to complete the last answer:

@Before(value="execution(* *(@CustomAnnotation (*), ..)) && args(input, ..)")public void inspect(JoinPoint jp, Object input) {        LOGGER.info(">>> inspecting "+input+" on "+jp.getTarget()+", "+jp.getSignature());}

will match a method where (one of) the argument of the method is annotated with @CustomAnnotation, for example:

service.doJob(@CustomAnnotation Pojo arg0, String arg1);

In contrast to

@Before(value="execution(* *(@CustomAnnotation *, ..)) && args(input, ..)")public void inspect(JoinPoint jp, Object input) {            LOGGER.info(">>> inspecting "+input+" on "+jp.getTarget()+", "+jp.getSignature());    }

which will match methods where (one of) the argument's class is annotated with @CustomAnnotation, for example:

service.doJob(AnnotatedPojo arg0, String arg1);

where the pojo is declared as follows:

@CustomAnnotationpublic class AnnotatedPojo {}

All the difference lies in the @CustomAnnotation (*) vs. @CustomAnnotation * in the pointcut declaration.