Announcement Announcement Module
Collapse
No announcement yet.
Pointcut on handleRequest of a SimpleFormController Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    I figured that the thread topic matched my question closely enough therefor i just added it here rather than creating a new one

    Ok, so I tried your suggestion and it doesn't work.

    Also: I've never encountered that '+' notation, am I right in saying that it basically means in this case : 'target any interface extending the Controller interface' ?

    Comment


    • #17
      Also: I've never encountered that '+' notation, am I right in saying that it basically means in this case : 'target any interface extending the Controller interface' ?
      Extending and implementing basically it means and any subclasses.

      Which suggestion dit you try just adding the + or also the execution pointcut?

      Comment


      • #18
        I tried both without success:

        Code:
        execution(* org.springframework.web.servlet.mvc.Controller+.*(..))
        
        target(org.springframework.web.servlet.mvc.Controller+)"
        I have 3 other aop:config blocks that work fine (one of them with auto-proxy=true), could it be that this is conflicting somehow ?

        Comment


        • #19
          At least I would try reducing the number of aop:config blocks to just one. It might be that they are conflicting, it is stated that it should work, however I've heard/read more problems with multiple aop:config blocks.

          You might want to enable debugging for the aop packages, this can give you some information why your pointcut is failing.

          Comment


          • #20
            Right, so i merged all aop-config blocks in one, no joy.

            Then i removed all other aspects leaving me just with

            Code:
            <aop:config proxy-target-class="false">
              <aop:aspect id="beforeRequestMethodExecution" ref="webRequestAuditor">
                <aop:before pointcut="execution(* org.springframework.web.servlet.mvc.Controller+.*(..))" method="beforeWebRequest" />
              </aop:aspect>
            </aop:config>
            
            <bean id="webRequestAuditor" class="package.WebRequestAuditLogger" />
            and the bean being defined as

            Code:
            public class WebRequestAuditLogger {
            
                public void beforeWebRequest() {
                    System.out.println("@@@@@@@@@@@@@@@");
                }
            }
            but still it's not working.

            I then removed acegi from my config thinking it could have perhaps interfered somehow with the pointcut (don't ask me how but i'm getting paranoid here), and still no luck.

            I have enabled all debugging but see no mention of spring trying to attempt to match a pointcut during the webrequest. In fact, there is no logging from any aop class at all during request handling, which makes me think the aspect is not being taken into consideration at all.

            <sigh>

            Any ideas where i can start stepping through the code to see where this is failing ?

            Comment


            • #21
              Hmm strange I created a little testcase which works perfectly.

              Interceptor
              Code:
              package biz.deinum.interceptor.web;
              
              public class WebInterceptor {
                  
                  public void beforeRequest() {
                      System.out.println("beforeRequest");
                  }
                  
                  public void afterRequest() {
                      System.out.println("afterRequest");
                      
                  }
              }
              Spring Test Case
              Code:
              package biz.deinum.interceptor.web;
              
              import org.springframework.mock.web.MockHttpServletRequest;
              import org.springframework.mock.web.MockHttpServletResponse;
              import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
              import org.springframework.web.servlet.ModelAndView;
              import org.springframework.web.servlet.mvc.Controller;
              
              public class WebInterceptorTests extends AbstractDependencyInjectionSpringContextTests {
                  
                  private Controller dummyController;
                  
                  public WebInterceptorTests() {
                      super("WebInterceptorTests");
                      setAutowireMode(AUTOWIRE_BY_NAME);
                  }
                  
                  @Override
                  protected String[] getConfigLocations() {
                      return new String[] {"biz/deinum/interceptor/web/applicationContext.xml"};
                  }
                  
                  public void testInterceptionGet() throws Exception {
                      MockHttpServletRequest request = new MockHttpServletRequest();
                      request.setMethod("GET");
                      MockHttpServletResponse response = new MockHttpServletResponse();
                      ModelAndView mav = dummyController.handleRequest(request, response);
                      String name = mav.getViewName();
                      assertEquals("form", name);
                  }
              
                  public void testInterceptionPost() throws Exception {
                      MockHttpServletRequest request = new MockHttpServletRequest();
                      request.setMethod("POST");
                      MockHttpServletResponse response = new MockHttpServletResponse();
                      ModelAndView mav = dummyController.handleRequest(request, response);
                      String name = mav.getViewName();
                      assertEquals("success", name);
                  }
              
                  
                  public void setDummyController(final Controller controller) {
                      this.dummyController=controller;
                  }
              }
              And the configuration

              Code:
              <?xml version="1.0" encoding="UTF-8"?>
              <beans xmlns="http://www.springframework.org/schema/beans"
              			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  		xmlns:aop="http://www.springframework.org/schema/aop"
                  		xmlns:tx="http://www.springframework.org/schema/tx"          
                          xsi:schemaLocation="
                              http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
                  			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
              	
              	<!-- Transactions -->
              	<aop:config proxy-target-class="false">
               		<aop:pointcut id="webRequest" expression="execution(* org.springframework.web.servlet.mvc.Controller+.*(..))" />
               		<aop:aspect id="webRequestAspect" ref="webRequestInterceptor">
               			<aop:before pointcut-ref="webRequest" method="beforeRequest"/>
               			<aop:after pointcut-ref="webRequest" method="afterRequest"/>
               		</aop:aspect>
              	</aop:config>
                  
                  <bean id="webRequestInterceptor" class="biz.deinum.interceptor.web.WebInterceptor"/>
                  
                  <bean id="dummyController" class="org.springframework.web.servlet.mvc.SimpleFormController">
                  	<property name="commandClass" value="java.lang.Object"/>
                  	<property name="formView" value="form"/>
                  	<property name="successView" value="success"/>
                  </bean>    
              </beans>
              Which results in this output (amongs the spring debug stuff)

              Code:
              11:27:48.390 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'webRequestInterceptor'
              beforeRequest
              11:27:48.406 [main] DEBUG o.s.w.s.mvc.SimpleFormController - Displaying new form
              11:27:48.406 [main] DEBUG o.s.w.s.mvc.SimpleFormController - Creating new command of class [java.lang.Object]
              11:27:48.422 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'webRequestInterceptor'
              afterRequest
              11:27:48.422 [main] DEBUG o.s.beans.CachedIntrospectionResults - Using cached introspection results for class [biz.deinum.interceptor.web.WebInterceptorTests]
              11:27:48.422 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'dummyController'
              11:27:48.422 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Added autowiring by name from bean name 'biz.deinum.interceptor.web.WebInterceptorTests' via property 'dummyController' to bean named 'dummyController'
              11:27:48.422 [main] DEBUG o.s.beans.BeanWrapperImpl - About to invoke write method [public void biz.deinum.interceptor.web.WebInterceptorTests.setDummyController(org.springframework.web.servlet.mvc.Controller)] on object of class [biz.deinum.interceptor.web.WebInterceptorTests]
              11:27:48.422 [main] DEBUG o.s.beans.BeanWrapperImpl - Invoked write method [public void biz.deinum.interceptor.web.WebInterceptorTests.setDummyController(org.springframework.web.servlet.mvc.Controller)] with value of type [org.springframework.web.servlet.mvc.Controller]
              11:27:48.422 [main] DEBUG o.s.core.CollectionFactory - Creating [java.util.LinkedHashMap]
              11:27:48.422 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'webRequestInterceptor'
              beforeRequest
              11:27:48.422 [main] DEBUG o.s.w.s.mvc.SimpleFormController - Creating new command of class [java.lang.Object]
              11:27:48.437 [main] DEBUG o.s.beans.CachedIntrospectionResults - Getting BeanInfo for class [java.lang.Object]
              11:27:48.437 [main] DEBUG o.s.beans.CachedIntrospectionResults - Caching PropertyDescriptors for class [java.lang.Object]
              11:27:48.437 [main] DEBUG o.s.beans.CachedIntrospectionResults - Found bean property 'class' of type [java.lang.Class]
              11:27:48.437 [main] DEBUG o.s.beans.CachedIntrospectionResults - Not strongly caching class [java.lang.Object] because it is not cache-safe
              11:27:48.437 [main] DEBUG o.s.w.s.mvc.SimpleFormController - No errors -> processing submit
              11:27:48.437 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'webRequestInterceptor'
              afterRequest
              11:27:48.437 [Thread-1] INFO  o.s.c.s.ClassPathXmlApplicationContext - Closing application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=2864404]
              The only thing which is different is that you probably have your aop config in a different file then the controllers, however this shouldn't make a difference because aop config isn't per container.

              Where and when is the aop part loaded? Before or after the *-servlet.xml? Or are you doing an import in those files?

              Edit: Just tested different files doesn't make a difference, will try something with parent ApplicationContext
              Last edited by Marten Deinum; Jan 11th, 2007, 05:33 AM.

              Comment


              • #22
                I have the following structure

                WEB-INF/applicationContext.xml
                --> imports application-core.xml, which in turn imports aop.xml with pointcut definitions
                WEB-INF/dispatcherServlet.xml
                --> import spring controllers and url handler configuration mappings

                Perhaps, during aop init, spring isn't seeing the controller configurations of the dispatcherServlet and therefore not creating the necessary proxies during init of the applicationContext ?
                My other aop configs work, then again they are declared under application-core.xml.

                Comment


                • #23
                  Try loading your AOP config instead of importing it. It is just a guess.

                  Comment


                  • #24
                    I removed all configuration from applicationContext.xml and moved it to the configuration of the dispatcher servlet, and now it works (woopeeee!, i guess)

                    So now i'm wondering if this is how spring works by design ? Or is it a bug or currently unsupported feature ?

                    I'll see about coming up with an isolated testcase to put into Jira over the weekend, shouldn't be too hard.

                    oh and thanks for your help Marten, much appreciated!
                    Last edited by heymjo; Jan 11th, 2007, 07:02 AM. Reason: (misspelled Marten)

                    Comment


                    • #25
                      I guess it probably was some ordering issue in loading the beans.

                      Comment


                      • #26
                        Were you able configure AOP with multiaction controllers?. I'd like to add some logging aspects for some multi action controllers and no luck yet.

                        I'm using jdk 1.4.2, so no annotation support.

                        Any help would be great!. Thanks!

                        Vali
                        Last edited by valysivec27; Mar 2nd, 2007, 03:43 AM.

                        Comment


                        • #27
                          You cannot with the Spring AOP intercept calls then handleRequest on the Controller. That is the only method which is called externally, all the other method calls are internal calls and are not going to be intercepted by Spring.

                          Search the forum, for other solutions.

                          Comment


                          • #28
                            AOP Config vs Annotation

                            Originally posted by mdeinum View Post
                            First of all you are trying to use 2 mechanisms at once. The annotation based (aspectj-autoproxy) and the configuration based AOP (aop:config). Mixing them isn't a good idea, use one or the other.
                            Why it isn't a good idea? Can you explain please? I have already observed somehow some strange behavior when using aspectj-autoproxy mecchanism (I'm already using aop config and it works fine) and I'm interested to understand the cause....

                            Comment


                            • #29
                              If I remember correctly auto-proxing=true will force proxy instantiation via cglib library.

                              Hope that helps,
                              Valy

                              Comment

                              Working...
                              X