Hi,
Using AOP, I'm trying to get an annotation set on a bean method during its invocation.
Here my example:
XML Config
Around AdviceCode:<bean id="customerService" class="test.spring.aop.CustomerServiceImpl" /> <bean id="traceAroundMethodBeanAdvice" class="test.spring.aop.TraceAroundMethod" /> <bean id="customerAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="patterns"> <list> <value>.*</value> </list> </property> <property name="advice" ref="traceAroundMethodBeanAdvice" /> </bean> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>*Service</value> </list> </property> <property name="interceptorNames"> <list> <value>customerAdvisor</value> </list> </property> </bean>
I noticed that this code works only if my annotation is set on the bean's interface and not the implementation.Code:public class TraceAroundMethod implements MethodInterceptor { public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("TraceAroundMethod start..."); try { MyAnnotation a = methodInvocation.getMethod().getAnnotation(MyAnnotation.class);//problem here return methodInvocation.proceed(); } finally { System.out.println("TraceAroundMethod end."); } } }
In another words, if I put the annotation on a CustomerServiceImpl method, this codewill always return null.Code:methodInvocation.getMethod().getAnnotation(MyAnnotation.class)
To make it work, I have to put my annotation on the interface.
Is this normal please? Is there a way to put the annotation on the implementation pz?
thx


Reply With Quote