Spring AOP pointcut for annotated argument

mogronalol picture mogronalol · Apr 20, 2012 · Viewed 15.6k times · Source

Say I have a method like so:

public void method(@CustomAnnotation("value") String argument)

Is there a pointcut expression that could select all methods with arguments annotated with @CustomAnnotation? If so is there a way I could get access go the "value" argument?

Answer

MikePatel picture MikePatel · Apr 24, 2012

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.