stevebot
stevebot

Reputation: 24035

Problem using Spring 3 MVC

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?

Upvotes: 1

Views: 3188

Answers (4)

mkvalsvik
mkvalsvik

Reputation: 21

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">

Upvotes: 1

axtavt
axtavt

Reputation: 242786

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.

Upvotes: 3

skaffman
skaffman

Reputation: 403581

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

Upvotes: 1

gennad
gennad

Reputation: 5585

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">

Upvotes: 2

Related Questions