1

I have all the jars in the Spring 3 framework on my classpath and I wanted to add Spring 3 mvc to my app config. Originally, I had the following XML.

<?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:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd


    <context:annotation-config/>
    <bean class="com.apppackage.app.config.AppContextConfig" />

    <!-- Autoscan for @Controller type controllers -->
    <context:component-scan base-package="com.apppackage.app.controller" /> 

That is just a snippet of the relevant information. My app worked fine with the above XML, but then I added Spring 3 MVC in the config with the following changes:

<?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:util="http://www.springframework.org/schema/util"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:annotation-config/>

    <mvc:annotation-driven />
    <bean class="com.apppackage.app.config.AppContextConfig" />

    <!-- Autoscan for @Controller type controllers -->
    <context:component-scan base-package="com.apppackage.app.controller" />

Now, I have problems all over my application. Spring does not seem to be Autowiring beans that it was before. I was also getting the following error on my controllers:

No adapter for handler [com.apppackage.app.controller.login.LoginController@274b9691]: Does your handler implement a supported interface like Controller?
3
  • 1
    What does it mean "not wiring up resources"? Commented Nov 15, 2010 at 18:21
  • Sorry, I updated that statement. Hopefully it is clear now. Commented Nov 15, 2010 at 18:27
  • The error mentions LoginController. So show us LoginController. Commented Nov 15, 2010 at 18:40

4 Answers 4

3

In addition to skaffman's answer: you can enable old-style controllers without removing <mvc:annotation-driven> by declaring old-style handler mappings and handler adapters manually:

<bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean class = "org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

Explanation:

When DispatcherServlet can't find HandlerMappings and HandlerAdapters declared in the context, it registers the default mappings (BeanNameUrlHandlerMapping and DefaultAnnotationHandlerMapping) and adapters (HttpRequestHandlerAdapter, SimpleControllerHandlerAdapter and AnnotationMethodHandlerAdapter), so in a simple case everything works without explicit configuration. However, if you declare some mappings or adapters explicitly, default are not applied, therefore if you need other mappings and adapters, you have to declare them explicitly too.

Now, <mvc:annotation-driven> declares DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter explicitly, effectively disabling other default mappings and adapters, so you need to declare them manually.

Sign up to request clarification or add additional context in comments.

Comments

2

My working snipplet for Spring 3 MVC

 <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
                               http://www.springframework.org/schema/beans/spring-beans.xsd
                               http://www.springframework.org/schema/mvc
                               http://www.springframework.org/schema/mvc/spring-mvc.xsd
                               http://www.springframework.org/schema/context
                               http://www.springframework.org/schema/context/spring-context-3.0.xsd
                               http://www.springframework.org/schema/tx
                               http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

1 Comment

Hi gennad, do you mind explaining what this is doing exactly by making an edit to your answer? How does this beans header help? Thank you!
1

When you add <mvc:annotation-driven /> to your context, you're effectively disabling support for the old-style Controller type hierarchy.

The error messages suggests to me that LoginController is not an annotated controller, but is a subtype of the Controller interface.

If you don't want to refactor LoginController, then remove <mvc:annotation-driven /> from your context. Unless you're using JSR-303 validation or JSON serialization, you don't need it anyway

Comments

1

Inspired by axtavt's answer, I removed this from my mvc config and that got me past the "No adapter for handler .." error:

<bean id="annotationMapper" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.