Announcement Announcement Module
Collapse
No announcement yet.
How to apply advice to interface and implementation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to apply advice to interface and implementation

    Hello,

    I would like to apply the around advice (MethodInteceptor) to both the interface and the implementation and I am not sure of how to configure the xml. I was expecting the output of generateName method.

    output:
    class com.attempt4.TestImpl.getName enter
    class com.attempt4.TestImpl.getName exit
    main name=My Name

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
       <bean id="advice" class="com.attempt4.TimedLoggingAroundAdvice" />
       
       <bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
          <property name="advice">
             <ref local="advice" />
          </property>
          <property name="pointcut">
             <bean class="org.springframework.aop.support.JdkRegexpMethodPointcut">
                <property name="pattern">
                   <value>.*</value>
                </property>
             </bean>
          </property>
       </bean>
    
       <bean id="testImpl" class="com.attempt4.TestImpl" />
       
       <bean id="test" class="org.springframework.aop.framework.ProxyFactoryBean">
          <property name="proxyInterfaces">
             <value>com.attempt4.Test</value>
          </property>
          <property name="target">
             <ref local="testImpl"/>
          </property>
          <property name="interceptorNames">
             <list>
                <value>advisor</value>
             </list>
          </property>
       </bean>
    
    </beans>
    the java sources:
    Code:
    public class TimedLoggingAroundAdvice implements MethodInterceptor &#123;
       public Object invoke&#40;MethodInvocation invocation&#41; throws Throwable &#123;
          Method m = invocation.getMethod&#40;&#41;;
          Object target = invocation.getThis&#40;&#41;;
    
          System.out.println&#40;target.getClass&#40;&#41; + "." + m.getName&#40;&#41; + " enter"&#41;;
          
          // invoke method
          Object returnValue = invocation.proceed&#40;&#41;;
          
          System.out.println&#40;target.getClass&#40;&#41; + "." + m.getName&#40;&#41; + " exit"&#41;;
          
          return returnValue;
       &#125;
    &#125;
    
    public interface Test &#123;
    
       public String getName&#40;&#41;;
    &#125;
    
    public class TestImpl implements Test &#123;
    
       public String getName&#40;&#41; &#123;
          return generateName&#40;&#41;;
       &#125;
    
       public String generateName&#40;&#41; &#123;
          return "My Name";
       &#125;
       
       public static void main &#40;String&#91;&#93; args&#41; &#123;
          String&#91;&#93; contextLocations = new String&#91;&#93; &#123; "test.xml" &#125;;
          ApplicationContext context = new ClassPathXmlApplicationContext&#40;contextLocations&#41;;
          
          Test test = &#40;Test&#41; context.getBean&#40;"test"&#41;;
          
          System.out.println&#40;"main name=" + test.getName&#40;&#41;&#41;;
       &#125;
    &#125;

  • #2
    I would like to apply the around advice (MethodInteceptor) to both the interface and the implementation
    Not sure what you mean. It applies the advice around your implementation according to your interface and your defined pointcut.

    I tried your code (pretty much cut and paste) and it worked as expected (same as your expected output).

    I was expecting the output of generateName method.
    Can you elaborate?

    Comment


    • #3
      Maybe he means why did't the output also show:

      class com.attempt4.TestImpl.generateName enter
      class com.attempt4.TestImpl.generateName exit


      I think its because that method is not part of the interface. How would that method also be intercepted? It could be done of course using cglib, but how is it declared, to do both?

      Comment


      • #4
        It could be done of course using cglib
        Because the call is internal to the class, the CGLIB proxy won't be able to intercept this either.

        Comment


        • #5
          I suspect to intercept enter/exit from the implementation example above, I'll have to use a 3rd party AOP like AspectJ or AspectWerks.

          Our project was hoping for some Spring "magic" here so we would not have to step outside Spring to help us debug/log our implementation classes.

          In the future plans to integrate more AOP in Spring, will implementation classes be 'interceptable' like interfaces?

          Comment


          • #6
            Originally posted by jbetancourt
            Maybe he means why did't the output also show:

            class com.attempt4.TestImpl.generateName enter
            class com.attempt4.TestImpl.generateName exit
            Exactly.

            Comment

            Working...
            X