Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring AOP - pointcut for every method with an annotation

Tags:

I am trying to define a pointcut, that would catch every method that is annotated with (i.e.) @CatchThis. This is my own annotation.

Moreover, I'd like to have access to the first argument of the method, which will be of Long type. There may be other arguments too, but I don't care about them.


EDIT

This is what I have right now. What I don't know is how to pass the first parameter of the method annotated with @CatchThis.

@Aspect  public class MyAspect {     @Pointcut(value = "execution(public * *(..))")     public void anyPublicMethod() {     }      @Around("anyPublicMethod() && @annotation(catchThis)")     public Object logAction(ProceedingJoinPoint pjp, CatchThis catchThis) throws Throwable {         return pjp.proceed();     } } 
like image 517
emesx Avatar asked Oct 19 '11 07:10

emesx


People also ask

How do you specify a single pointcut for multiple packages?

You can use boolean operators: expression="execution(* package1. *. *(..))

How do you write a pointcut expression?

This pointcut matches any method that starts with find and has only one parameter of type Long. If we want to match a method with any number of parameters, but still having the fist parameter of type Long, we can use the following expression: @Pointcut("execution(* *.. find*(Long,..))")

How do you enable AOP using annotations?

To create an aspect, you need to apply @Aspect annotation on your aspect class and register it in applicationContext. xml file. This is how you register your aspect into context. Remember to add AOP support first into your context with “<aop:aspectj-autoproxy />“.


1 Answers

Something like this should do:

@Aspect public class MyAspect{      @Pointcut(value="execution(public * *(..))")     public void anyPublicMethod() {     }      @Around("anyPublicMethod() && @annotation(catchThis) && args(.., Long ,..)")     public Object logAction(         ProceedingJoinPoint pjp, CatchThis catchThis, Long long)         throws Throwable {          return pjp.proceed();     } } 
like image 152
Sean Patrick Floyd Avatar answered Oct 10 '22 20:10

Sean Patrick Floyd