Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC and Spring AOP configuration problems Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC and Spring AOP configuration problems

    I tried to switch from old version 3.0.5.RELEASE to new version of spring 3.2.3.RELEASE and got a problem which is nowhere described properly.

    Source codes are attached. (It is simple maven web project. Also I lived intellij idea files in archive with simple jatty Runner. Url is http://localhost:8080/welcome/)

    I have a simple interfaces and implementations for:

    1. Controller
    Code:
    @RequestMapping("/welcome")
    public interface HelloController {
    
        @RequestMapping(method = RequestMethod.GET)
    	public String printWelcome(
                @RequestParam(value = "redirectUrl", required = false) String redirectUrl,
                ModelMap model);
    }
    
    @Controller
    public class HelloControllerImpl implements HelloController {
        @Autowired
        private TestComponent testComponent;
    
        private static final Logger LOGGER = LoggerFactory.getLogger(HelloControllerImpl.class);
    
        public String printWelcome(String redirectUrl, ModelMap model) {
            testComponent.test();
            LOGGER.info("RedirectUrl = " + redirectUrl);
            model.addAttribute("message", "Spring 3 MVC Hello World");
            return "hello";
        }
    }
    2. Business component
    Code:
    public interface TestComponent {
        void test();
    }
    
    @Component
    public class TestComponentImpl implements TestComponent {
        private static final Logger LOGGER = LoggerFactory.getLogger(TestComponentImpl.class);
    
        @Override
        public void test() {
            LOGGER.info("TestComponent was called!");
        }
    }
    3. Some test aspect

    Code:
    public class TestAspect implements MethodBeforeAdvice {
        private static final Logger LOGGER = LoggerFactory.getLogger(TestAspect.class);
    
        @Override
        public void before(Method method, Object[] objects, Object o) throws Throwable {
           LOGGER.info("TestAspect was called!");
        }
    }
    4. Configuration for aspect aop-config.xml
    Code:
        <bean id="testAdvice"
              class="com.common.aspect.TestAspect" />
    
        <aop:aspectj-autoproxy/>
    
        <aop:config>
            <aop:advisor advice-ref="testAdvice"
                         pointcut="execution(* com.common.controller.*Controller.*(..))
                         "/>
            <!--&amp;&amp; execution(* com.common.bean.*Component.*(..))"-->
            <aop:advisor advice-ref="testAdvice"
                         pointcut="execution(* com.common.bean.*Component.*(..))
                         "/>
    
        </aop:config>
    5. Spring configuration mvc-dispatcher-servlet.xml
    Code:
    <context:property-placeholder location="classpath:/app.properties"/>
    
        <import resource="aop-config.xml"/>
    
        <context:component-scan base-package="com.common.controller" />
        <context:component-scan base-package="com.common.bean.impl" />
    	
        <bean
    		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix">
    			<value>/WEB-INF/pages/</value>
    		</property>
    		<property name="suffix">
    			<value>.jsp</value>
    		</property>
        </bean>
    In this configuration everything is working perfect (Seems in this case spring uses default configuration - DefaultAnnotationHandlerMapping).

    Problems happen when I try to add this:
    Code:
    <mvc:annotation-driven />
    If i put this line upper context:component-scan lines in mvc-dispatcher-servlet.xml,
    Code:
     <context:property-placeholder location="classpath:/app.properties"/>
    
        <import resource="aop-config.xml"/>
    
        <mvc:annotation-driven />
    
           <context:component-scan base-package="com.common.controller" />
           <context:component-scan base-package="com.common.bean.impl" />    
    
    	<bean
    		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix">
    			<value>/WEB-INF/pages/</value>
    		</property>
    		<property name="suffix">
    			<value>.jsp</value>
    		</property>
    	</bean>
    I am getting the following exception when trying to call url.
    object is not an instance of declaring class
    HandlerMethod details:
    Controller [$Proxy10]
    Method [public java.lang.String com.common.controller.impl.HelloControllerImpl.pri ntWelcome(java.lang.String,org.springframework.ui. ModelMap)]
    Resolved arguments:
    [0] [null]
    [1] [type=org.springframework.validation.support.Bindin gAwareModelMap] [value={}]

    Caused by:

    java.lang.IllegalArgumentException: object is not an instance of declaring class
    HandlerMethod details:
    Controller [$Proxy10]
    Method [public java.lang.String com.common.controller.impl.HelloControllerImpl.pri ntWelcome(java.lang.String,org.springframework.ui. ModelMap)]
    Resolved arguments:
    [0] [null]
    [1] [type=org.springframework.validation.support.Bindin gAwareModelMap] [value={}]

    at org.springframework.web.method.support.InvocableHa ndlerMethod.invoke(InvocableHandlerMethod.java:223 )
    at org.springframework.web.method.support.InvocableHa ndlerMethod.invokeForRequest(InvocableHandlerMetho d.java:132)
    at org.springframework.web.servlet.mvc.method.annotat ion.ServletInvocableHandlerMethod.invokeAndHandle( ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotat ion.RequestMappingHandlerAdapter.invokeHandleMetho d(RequestMappingHandlerAdapter.java:745)
    at org.springframework.web.servlet.mvc.method.annotat ion.RequestMappingHandlerAdapter.handleInternal(Re questMappingHandlerAdapter.java:686)
    at org.springframework.web.servlet.mvc.method.Abstrac tHandlerMethodAdapter.handle(AbstractHandlerMethod Adapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:827)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:707)
    at org.springframework.web.servlet.FrameworkServlet.s ervice(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:820)
    at org.mortbay.jetty.servlet.ServletHolder.handle(Ser vletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler.handle(Se rvletHandler.java:401)
    at org.mortbay.jetty.security.SecurityHandler.handle( SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(Se ssionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(Co ntextHandler.java:766)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebA ppContext.java:450)
    at org.mortbay.jetty.handler.ContextHandlerCollection .handle(ContextHandlerCollection.java:230)
    at org.mortbay.jetty.handler.HandlerCollection.handle (HandlerCollection.java:114)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(Ha ndlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:322)
    at org.mortbay.jetty.HttpConnection.handleRequest(Htt pConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.he aderComplete(HttpConnection.java:928)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser. java:549)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpPa rser.java:212)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnec tion.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(Selec tChannelEndPoint.java:410)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run (QueuedThreadPool.java:582)
    Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.web.method.support.InvocableHa ndlerMethod.invoke(InvocableHandlerMethod.java:219 )
    ... 29 more
    This can be fixed if I put <mvc:annotation-driven /> under context:component-scan lines, but for me it is really strange, cause Spring 3.0.5.RELEASE doesn't have such kind of limitations of mvc:annotation-driven position in spring config. (Tried several versions of spring and it seems this bug came from 3.1 version)

    Also this problem can be fixed by adding proxy-target-class="true" to aop:config and cglib libs in classPath.
    (http://static.springsource.org/sprin...requestmapping note under 15.3.2). In this case you are define to use CGLIB proxies instead JDK dynamic proxies, which is default for spring.

    Could anyone check this issue and maybe report a bug? (if it is a real bug)
Working...
X