Announcement Announcement Module
Collapse
No announcement yet.
My Interceptor does not intercept! Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • My Interceptor does not intercept!

    Hello,

    I have the following class:

    Code:
    package br.com.ivia.aop;
    
    import org.aopalliance.intercept.MethodInterceptor;
    import org.aopalliance.intercept.MethodInvocation;
    
    public class HistoryInterceptor implements MethodInterceptor {
      
        public Object invoke(MethodInvocation methodInvocation) 
                                        throws Throwable {
     
               System.out.println("Bingo!");
               Object retVal = methodInvocation.proceed();
               return retVal;
          }
    }
    And the following configuration in my WEB-INF\applicationContext.xml:

    Code:
    <beans>
        ...
        <bean id="myBuzzServiceTarget" 
                  class="br.com.ivia.service.impl.BuzzServiceImpl">
            <property name="dao">
                <ref bean="buzzDAO"/>
            </property>
        </bean>
    
        <bean id="myHistoryInterceptor" 
                  class="br.com.ivia.aop.HistoryInterceptor" />
    
        <bean id="myBuzzService" 
               class="org.springframework.aop.framework.ProxyFactoryBean" >
            <property name="proxyInterfaces">
                <value>br.com.ivia.service.BuzzService</value>
            </property>
            <property name="target">
                <ref local="myBuzzServiceTarget" />
            </property>
            <property name="interceptorNames">
                <list>
                  <value>myHistoryInterceptor</value>
                </list>
            </property>
        </bean>
    </beans>
    Where BuzzServiceImpl implements the interface BuzzService.

    When I started my web app, everything goes right in Tomcat.

    Then, when I call any business method from the BuzzService object I don't get any message from myHistoryInterceptor.

    What should I do to make the invoke() method from HistoryInterceptor has been executed?

    Thanks a lot!

  • #2
    This code looks OK at first glance. Can you post more of your code?

    To quickly test if the interceptor is working, try writing a test case like:
    Code:
    public static void main&#40;String&#91;&#93; args&#41; &#123;
      XmlBeanFactory ctx = new XmlBeanFactory&#40;new ClassPathResource&#40;"applicationContext.xml", Test.class&#41;&#41;;
      BuzzService buzzService = &#40;BuzzService&#41; ctx.getBean&#40;"myBuzzService"&#41;;
      String result = buzzService.doSomething&#40;10&#41;;
    &#125;

    Comment


    • #3
      Hello,

      Sorry, but... how can I test the AOP Intercptor in a web application?

      There is only one configuration for the Spring ContextLoader in web.xml

      Code:
      <web-app ...>
          ...
          <listener>
              <listener-class>
                  org.springframework.web.context.ContextLoaderListener
              </listener-class>
           </listener>
          ...
      </web-app>
      Have I must added any other configuration in web.xml or applicationContext.xml?

      There is another point. I'm using Struts framework too.

      Thanks for your help!
      (and sorry about my poor English) :-(

      Comment


      • #4
        Hello katentim,

        For your information, I did the test using the code that you sent and... it worked!!!

        But, it doesn't work when a method from my business object is called from my Struts DispatchAction.

        Thanks.

        Comment


        • #5
          Regis,

          Can you post the code used to inject/or lookup the service in your Struts action please.

          Regards,

          Rob

          Comment


          • #6
            Hello Rod,

            Oops... Injection/lookup? Sorry, but there is only a setService() method for Spring to set the bean.

            This is my Action:

            Code:
            package br.com.ivia.web;
            
            import java.util.List;
            import javax.servlet.http.*;
            import org.apache.struts.action.*;
            import org.apache.struts.actions.DispatchAction;
            import org.apache.struts.validator.DynaValidatorForm;
            import br.com.ivia.service.BuzzService;
            
            public class BuzzAction
                extends DispatchAction &#123;
            
                private BuzzService service;
            
                public void setService&#40;BuzzService service&#41; &#123;
                    this.service = service;
                &#125;
            
                public ActionForward select&#40;ActionMapping actionMapping,
                                            ActionForm actionForm,
                                            HttpServletRequest request,
                                            HttpServletResponse response&#41; &#123;
            
                    List buzz = service.selectAll&#40;&#41;;
                    servletRequest.setAttribute&#40;"buzzList", buzz&#41;;
            
                    return actionMapping.findForward&#40;"showBuzz"&#41;;
                &#125;
               
                ...
            &#125;
            Thanks!

            Comment


            • #7
              Regis,

              In that code you can check that the service object you are receiving from Spring is the AOP proxy using AopUtils.isAopProxy(). If it is you can cast it to Advised and then query the advisors using getAdvisors() to make sure that your interceptor is in there.

              If you are not getting an AOP proxy, make sure you have injected the correct bean into Struts.

              See the JavaDocs for Advised and Advisor for more details. You may find it useful to drop in a breakpoint in your Action and check out the service object in the debugger.

              Rob

              Comment


              • #8
                Hello Rob,

                Yikes! Finally, my Interceptor worked!

                The problem was in the action-servlet.xml. I put the correct ref bean for the ProxyFactoryBean.

                Code:
                <!-- action-servlet.xml -->
                <beans>
                    <bean name="/buzz" class="br.com.ivia.web.BuzzAction" >
                        <property name="service">
                            <!-- This is a real ProxyFactoryBean -->
                            <ref bean="myBuzzService"/>
                        </property>
                    </bean>
                    ...
                </beans>
                But, every method from BuzzService will be intercepted. How can I filter the methods that I'm interested to intercept?

                Thanks a lot!

                Comment


                • #9
                  First add a methodPointcut advisor bean to your context:

                  Code:
                  	<bean id="yourMethodPointcut" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
                  		<property name="advice"><ref bean="myHistoryInterceptor"/></property>
                  		<property name="mappedName"><value>yourMethodName</value></property>
                  	</bean>
                  Now in your myBuzzService bean, don't have the interceptorNames property point directly to myHistoryInterceptor, instead point to the new "yourMethodPointcut" bean. I think that should work.

                  Regards,
                  Erik

                  Comment


                  • #10
                    Hello Erik,

                    I added the MethodPointcutAdvisor bean as you said, but no method was intercepted by HistoryInterceptor.

                    Code:
                    <beans>
                        ...
                        <bean id="myHistoryPointcut" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor" >
                            <property name="advice">
                                <ref bean="myHistoryInterceptor" />
                            </property>
                            <property name="mappedNames">
                                <list>
                                    <value>insert</value>
                                    <value>update</value>
                                    <value>remove</value>
                                </list>
                            </property>
                        </bean>
                    
                        <bean id="myBuzzService" class="org.springframework.aop.framework.ProxyFactoryBean" >
                            <property name="proxyInterfaces">
                                <list>
                                    <value>br.com.ivia.service.BuzzService</value>
                                </list>
                            </property>
                            <property name="target">
                                <ref local="myBuzzServiceTarget" />
                            </property>
                            <property name="interceptorNames">
                                <list>
                                  <value>myHistoryPointcut</value>
                                </list>
                            </property>
                        </bean>
                    </beans>
                    I tried to use patterns in the mappedNames, like:

                    Code:
                    ...
                    <property name="mappedNames"> 
                        <list> 
                            <value>*.insert*</value> 
                            <value>*.update*</value> 
                            <value>*.remove*</value> 
                        </list> 
                    </property>
                    ...
                    But didn't get any different result.

                    I could check the name of the method in HistoryInterceptor, but I think it's so ugly. :?

                    Thanks for help.[/code]

                    Comment


                    • #11
                      Regis,

                      Pointcuts and their uses are covered in detail in chapter 5 of the Spring reference manual. The information in there should help you to get up and running with this.

                      Rob

                      Comment


                      • #12
                        Hello Rob,

                        Ok. I'll check the reference.

                        I should have done it before. Sorry.

                        Thanks for help.

                        Comment


                        • #13
                          Interceptor is not getting invoked....

                          Hi

                          My webContext.xml looks like :

                          <!-- ========================= BUSINESS DELEGATE DEFINITIONS ========================= -->

                          <bean id="riDelegate"
                          class="com.standardchartered.cr.ri.delegate.impl.R IDelegateLocalImpl">
                          <property name="service" ref="riService" />
                          </bean>

                          <bean id="infDelegate"
                          class="com.standardchartered.cr.inf.delegate.impl. InfDelegateLocalImpl">
                          <property name="service" ref="infService" />
                          </bean>

                          <!-- ========================= STRUTS ACTION DEFINITIONS ========================= -->

                          <bean id="personAction" scope="prototype"
                          class="com.standardchartered.cr.ri.action.PersonAc tion">
                          <constructor-arg ref="riDelegate" />
                          <constructor-arg ref="infDelegate" />
                          </bean>



                          <!-- ========================= FOR CACHING ========================= -->

                          <bean id="appCacheManager" class="net.sf.ehcache.CacheManager">
                          <!-- constructor-arg index="0" type="java.net.URL"
                          value="classpath:/WEB-INF/ehcache.xml" /-->
                          </bean>


                          <aop:config>
                          <aopointcut id="getPersonByCriteriaPointcut"
                          expression="execution(* *..RIDelegateLocalImpl.getPersonByCriteria())" />
                          <aop:advisor id="methodCachingAdvisor"
                          advice-ref="methodCachingAdvice"
                          pointcut-ref="getPersonByCriteriaPointcut" />
                          </aop:config>


                          <bean id="methodCachingAdvice"
                          class="com.standardchartered.cr.ri.caching.interce ptor.MethodCachingInterceptor">
                          <property name="cacheManager" ref="appCacheManager" />
                          </bean>


                          <!-- =========================CACHING ENDS ========================= -->

                          the ehcache.xml is:

                          <ehcache>
                          <diskStore path="java.io.tmpdir" />
                          <defaultCache maxElementsInMemory="10" eternal="false"
                          timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />

                          <cache name="getPersonByCriteria" maxElementsInMemory="150" eternal="false"
                          overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="86400" />

                          </ehcache>


                          But my MethodCachingInterceptor class is not getting called.

                          What could be the problem ? This is pretty urgent, if u can help...

                          Thanks in advance.....

                          Comment


                          • #14
                            I would take a look at the examples in the Spring reference manual, most of the time the issues related to interceptors are either internal method calls, the AOP expression not being correct or you are proxying interfaces and the AOP expression references the class instead.
                            http://www.springframework.org/docs/...l#aop-proxying
                            Last edited by karldmoore; Aug 29th, 2007, 10:02 AM.

                            Comment

                            Working...
                            X