Ringo777
Ringo777

Reputation: 97

A spring aop pointcut can filter for the combination of an annotation and a return type?

@Pointcut(Execution(@com.annotions.MyAnnotation void *(..))

Would this be a valid pointcut, if I want only methods to be advised with the @MyAnnotation and have the return type void?

Upvotes: 0

Views: 577

Answers (1)

kriegaex
kriegaex

Reputation: 67457

No, your pointcut would be invalid for several reasons, mostly because you cannot have tested it in a real application and just posted pseudo code here instead:

  1. The @Pointcut annotation argument needs to be enclosed in double quotes because it is a String.
  2. There is one closing parenthesis ) missing.
  3. The keyword execution must be written in lower-case characters, yours starts with an upper-case one.

Other than that, i.e. if you write the pointcut like this, it would be correct:

@Pointcut("execution(@com.annotions.MyAnnotation void *(..))")

As an alternative, you can separate the two requirements into distinct pointcut specifiers like this:

@Pointcut("execution(void *(..)) && @annotation(com.annotions.MyAnnotation)")

In this case it might look a tad lengthy, but if your execution pointcut is more complex, it is nice to separate the annotation pointcut from it so as to make the overall pointcut more readable.

I think it could be helpful for you to read the Spring AOP and AspectJ manuals. Good luck and don't give up, it is going to get easier if you use AOP syntax more often.

Upvotes: 2

Related Questions