springaopspring-aoppointcut

Spring AOP pointcut for annotated argument


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?


Solution

  • 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.