Announcement Announcement Module
Collapse
No announcement yet.
After Advice & Spring Controllers Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • After Advice & Spring Controllers

    I am attempting to use an AfterReturningAdvice on the formBackingObject method of SimpleFormController controllers. However, the only method invoking the afterReturning method is "handleRequest". Below is my xml. Anyone know what is going on???
    Thanks!
    Kris

    <bean name="formBackingInterceptor" class="com.idm.ctm.interceptor.FormBackingIntercep tor">
    </bean>

    <bean id="controllerAutoProxyCreator" class="org.springframework.aop.framework.autoproxy .BeanNameAutoProxyCreator">
    <property name="beanNames">
    <value>*Controller</value>
    </property>
    <property name="interceptorNames">
    <value>formBackingAdvisor</value>
    </property>
    </bean>

    <bean id="formBackingAdvisor" class="org.springframework.aop.support.RegexpMetho dPointcutAdvisor">
    <property name="advice">
    <ref bean="formBackingInterceptor" />
    </property>
    <property name="patterns">
    <list>
    <value>.*</value>
    </list>
    </property>
    </bean>


    <bean id="myController" class="myController">
    <property name="formView"><value>/view</value></property>
    <property name="successView"><value>/view</value></property>
    <property name="commandName"><value>command</value></property>
    <property name="commandClass"><value>myView</value></property>
    </bean>

  • #2
    I'm having the exact same problem...

    I wanted to use MethodSecurityInterception in a class which extends MultiActionController.
    That didn't seem to work, and i tried to isolate the problem by adding a TestAdvice to the interceptor list, all that TestAdvice does is log the method call.

    The only methods that got intercepted were handleRequestInternal and getLastModified... The methods i wanted to protect/intercept (my own methods that return a ModelAndView ) were not intercepted at all...

    Using the TestAdvice on any other bean yields the expected results, every method gets intercepted...

    Hopefully anyone can tell me why, cause i can't tell.... but i do want to know

    Comment


    • #3
      Interceptors work on the proxies of the object. The only method called on the proxy is handleRequest as specified in the Controller interface. All calls to the other methods are internal calls and hence are not being intercepted.

      If you want to intercept those calls you will have to revert to something like AspectJ which can do that for you.

      Comment


      • #4
        Not sure i follow you, sure the proxy is the one intercepting method calls..
        But the calls to the proxied class are the ones being intercepted...
        I'll try to clarify my issue:
        Part of the config xml:
        Code:
        <bean id="controller" 
            class="org.springframework.aop.framework.ProxyFactoryBean">
            <property name="target">
            	<bean class="tmg.webtools.tool.Controller">
        			<property name="methodNameResolver" ref="toolControllerResolver"/>
        			<property name="test" ref="test"/>
        		</bean>
            </property>
            <property name="interceptorNames">
                <list>
                    <value>testAdvice</value>
                </list>
            </property>
            <property name="proxyTargetClass"><value>true</value></property>
        </bean>
        
        <bean id="test" 
            class="org.springframework.aop.framework.ProxyFactoryBean">
            <property name="target">
            	<bean class="tmg.webtools.tool.Test"/>
            </property>
            <property name="interceptorNames">
                <list>
                    <value>testAdvice</value>
                </list>
            </property>
            <property name="proxyTargetClass"><value>true</value></property>
        </bean>
        
        
        <bean id="testAdvice" class="tmg.webtools.tool.TestAdvice"/>
        
        <bean id="toolControllerResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
        	<property name="mappings">
        	<props>  
        		<prop key="/tool">test</prop>
        		<prop key="/tool2">doSomething</prop>
        		<prop key="/tool3">doSomeOtherThing</prop>
        	</props>
        	</property>
        </bean>
        The advice class:
        Code:
        public class TestAdvice implements MethodBeforeAdvice {
        	public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
        		System.out.println(arg0.getName() + " called on " + arg2.toString() + " intercepted by TestAdvice");
        	}
        }
        The controller bean:
        Code:
        public class Controller extends MultiActionController {
        	private Test test;
        	
        	public ModelAndView test(HttpServletRequest request, HttpServletResponse response) {
        		HashMap context = new HashMap();
        		return new ModelAndView("toolView","ctx",context);	
        	}
        
        	public ModelAndView doSomething(HttpServletRequest request, HttpServletResponse response) {
        		HashMap context = new HashMap();
        		return new ModelAndView("toolView2","ctx",context);	
        	}
        
        	public ModelAndView doSomeOtherThing(HttpServletRequest request, HttpServletResponse response) {
        		HashMap context = new HashMap(); 
        		return new ModelAndView("toolView3","ctx",context);
        		test.methodOne();
        		test.methodTwo();
        		test.methodThree();
        	}
        	
        	public void setTest(Test test) {
        		this.test = test;
        	}
        }
        And the test bean:
        Code:
        public class Test {
        
        	public void methodOne() {}
        
        	public void methodTwo() {}
        
        	public void methodThree() {}
        }
        Some lines from server.log:
        Code:
        2006-09-20 12:12:37,740 INFO  [STDOUT] getLastModified called on tmg.webtools.tool.Controller@868c6d intercepted by TestAdvice
        2006-09-20 12:12:37,740 INFO  [STDOUT] handleRequestInternal called on tmg.webtools.tool.Controller@868c6d intercepted by TestAdvice
        2006-09-20 12:12:41,309 INFO  [STDOUT] getLastModified called on tmg.webtools.tool.Controller@868c6d intercepted by TestAdvice
        2006-09-20 12:12:41,309 INFO  [STDOUT] handleRequestInternal called on tmg.webtools.tool.Controller@868c6d intercepted by TestAdvice
        2006-09-20 12:12:50,473 INFO  [STDOUT] getLastModified called on tmg.webtools.tool.Controller@868c6d intercepted by TestAdvice
        2006-09-20 12:12:50,473 INFO  [STDOUT] handleRequestInternal called on tmg.webtools.tool.Controller@868c6d intercepted by TestAdvice
        2006-09-20 12:12:50,473 INFO  [STDOUT] methodOne called on tmg.webtools.tool.Test@1d35bf2 intercepted by TestAdvice
        2006-09-20 12:12:50,473 INFO  [STDOUT] methodTwo called on tmg.webtools.tool.Test@1d35bf2 intercepted by TestAdvice
        2006-09-20 12:12:50,473 INFO  [STDOUT] methodThree called on tmg.webtools.tool.Test@1d35bf2 intercepted by TestAdvice
        The server log lines show that TestAdvice intercepts every call to a method in Test (which is wrapped in a ProxyFactoryBean), but the same TestAdvice does not
        intercept every call to a method in Controller(which is also wrapped in a ProxyFactoryBean), the only ones intercepted there are handleRequestInternal and getLastModified
        For instance, the three methods that return a ModelAndView (test, doSomething and doSomeOtherThing) never get intercepted...

        Comment


        • #5
          As expected...

          The calls to methodOne etc. are being called on the proxy, which you yourself created and injected into the Controller.

          However the methodcalls to test, doSomething, doSomeOtherThing are being called internally and not on a proxy. The only 2 methods being called on the proxy are [b]getLastModified[b] and handleRequestInternal.

          The handleRequestInternal handles the given method call and calls the invokeNamedMethod which in turn calls the configured method by looking it up with the handlerMapping you defined. Then that method is also called internally and not on the proxy.

          Here is a partial sequence diagram for calling the test method
          http://img178.imageshack.us/img178/8273/callshu0.th.gif

          As you can see, the handleRequestInternally and getLastModified are the ones being intercepted. After that the method calls are just internal and not being intercepted.
          Last edited by Marten Deinum; Sep 20th, 2006, 08:32 AM.

          Comment


          • #6
            Oh damnit, of course....

            That could have ben (should have been, even ) obvious.

            Thanks for the clarification.

            Comment


            • #7
              LOL just noticed your reply after I changed the message to include an image .

              Comment

              Working...
              X