2

I have a custom annotation as follows:

@Inherited @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface MyCustomAnnotation { } 

In Spring XML configuration I have the following:

 <aop:pointcut id="fooPointcut" expression="@annotation(com.foo.blah.MyCustomAnnotation)"/> 

This will only match on method annotations. How do I tweak the spel to also capture type/class annotations?

1 Answer 1

3

Use @within(com.foo.blah.MyCustomAnnotation) to

limit matching to join points within types that have the given annotation

A combined pointcut expression would become:

@annotation(com.foo.blah.MyCustomAnnotation) || @within(com.foo.blah.MyCustomAnnotation) 

See Join Point Matching based on Annotations in the AspectJ 5 Developer's Notebook for further reference. Also note, that Spring's AOP doesn't support full AspectJ pointcuts, only a limited subset.

Also note that @annotation(com.foo.blah.MyCustomAnnotation) in AspectJ would match

all join points where the subject of the join point has the given annotation

meaning that it would match method-execution as well as method-call. In Spring AOP it only matches method-execution though, but it's better to write pointcut expressions that are valid in a broader scope as well, so don't forget to use an execution(...) pointcut too to restrict the pointcut.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the detailed response - this works perfectly.
I'm glad to hear that! Mind accepting and upvoting my answer then? :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.