dimanche 19 avril 2015

How to write an Aspect pointcut based on an annotated parameter

I'm having a bit of trouble working out how to create a pointcut that will operate on beans that have a specific annotated parameter. My eventual aim is to validate the value of the parameter before it's processed, but for the moment I just need to create the pointcut.


Consider the following annotation



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


I'd then like to apply this to a number of methods like:



public void method1(@MyAnnotation long i) {}
public void method2(String someThing, @MyAnnotation long i) {}
public void method3(String someThing, @MyAnnotation long i, byte value) {}


So



  • I don't care which class (or package) the methods are in

  • The position of the annotated argument will vary.

  • I do know that annotated value will only apply to a specific type


My pointcut implementation needs to be something along the lines of:



@Before(value = "* *(..) && args(verifyMe)")
public void verifyInvestigationId(long verifyMe) {}


I'm getting a bit confused about exactly what that @Before value needs to be and how to tie in the annotation and its type. At this point it's probably not worth listing the things I've tried!


Update: Based on the advice I've seen in http://ift.tt/1ONk3Kz (and correcting a couple of misunderstandings and a missing space) I've got to the point where the following works:



@Before("execution(public * *(.., @full.path.to.MyAnnotation (*), ..))")
public void beforeMethod(JoinPoint joinPoint) {
System.out.println("At least one of the parameters are annotated with @MyAnnotation");
}


This is almost what I need - all I need to do is pass the value of the annotated argument as an parameter to the method. I can't quite work out the syntax to get Spring to do this (the linked answer does not show this).


Aucun commentaire:

Enregistrer un commentaire