I'm trying to do method annotation based interception using AOP alliance's MethodInterceptor. An AOP-alliance approach is a core requirement, so going a purely AspectJ route isn't an option.
Here's my setup:
Interface & Classes
Code:
public class MyAdvisor implements MethodInterceptor {
// implementation reads annotation and executes
...
}
public class ServiceImpl implements Service {
@Override
public SomeObject nameOfAnnotatedMethod(String args) throws CustomException {
...
}
}
public interface Service {
@MyAnnotation(...)
public SomeObject nameOfAnnotatedMethod(String args) throws CustomException;
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
...
}
Spring Config:
Code:
<aop:config>
<aop:pointcut id="myPointcut"
expression="execution(* nameOfAnnotatedMethod(..))"/>
<aop:advisor id="advisor" advice-ref="myAdvisor"
pointcut-ref="myPointcut" />
</aop:config>
<bean id="myAdvisor" class="package.MyAdvisor">
...
</bean>
First off the expression "execution(* nameOfAnnotatedMethod(..))" works. nameOfAnnotatedMethod(...) is intercepted and MyAdvisor can see the annotation information. So far, so good. But my goal is to NOT specify method name(s) and instead just intercept any method annotated with @MyAnnotation (that's the beauty of annotations right?).
However, I can't seem to come up with any pointcut expression which does this. I've tried the following ones which all compile at runtime (ie: Spring doesn't complain that they are invalid), but the interception never occurs with any:
- "execution(@package.MyAnnotation * nameOfAnnotatedMethod(..))"
- "execution(@package.MyAnnotation * *(..))"
- "execution(@package.MyAnnotation * *.*(..))"
What expression can I use to do this? Or is there some fundamental AOP-alliance limitation that prevents this from being possible (which would be confusing since Spring just uses AspectJ to evaluate the expression regardless of the advice engine, right?)?
EDIT:
I forgot to mention dependencies:
- spring-aop:3.1.2.RELEASE (using 3.1.2.RELEASE across the board for spring)
- cglib:2.2.2
- aspectjweaver:1.7.0
- aopalliance:1.0