Announcement Announcement Module
Collapse
No announcement yet.
can't intercept beans annotated methods Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • can't intercept beans annotated methods

    Hi,

    Using AOP, I'm trying to get an annotation set on a bean method during its invocation.

    Here my example:
    XML Config
    Code:
      <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>
    Around Advice
    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.");
        }
      }
    }
    I noticed that this code works only if my annotation is set on the bean's interface and not the implementation.

    In another words, if I put the annotation on a CustomerServiceImpl method, this code
    Code:
    methodInvocation.getMethod().getAnnotation(MyAnnotation.class)
    will always return null.

    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
Working...
X