Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling Spring MVC annotations while maintaining pre-existing XML mappings

I work on an application that was developed under Spring 2.5 using XML mappings. Recently, we upgraded our JARS to Spring 3.0 and also added in some component scanning in an attempt to use Spring 3.0's new MVC features, while still maintaining our existing XML mappings.

However, if we were to add the following to enable sping mvc annotations

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- The following is the new XML configuration that we have tried to add -->
    <mvc:annotation-driven/>

    <!-- ... old XML mappings -->

</beans>

Then, Spring only looks for Controllers annotated with @Controller and our old XML mappings seem to get ignored. Is the only solution to this problem, that we need to update all our old XML mappings to be annotations based (a daunting task) or is there some other solution?

One example of our old XML mappings is as follows:

<bean id="loginController" class="com.app.controller.login.LoginController">
        <property name="loginService" ref="loginService"/>
</bean>

The LoginController extends SimpleFormController. Many other of our old controllers either extend SimpleFormController or MultiActionController.

And this is how our Controllers are mapped.

     <bean id="urlMapping"
          class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/login">loginController</prop>
...
like image 442
stevebot Avatar asked Feb 26 '11 18:02

stevebot


1 Answers

I am pretty sure it's one or the other for Controller mapping but the annotation based features such as @RequestParam should still work if you have the following bean setup...

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

I can't remember if I have successfully mixed-and-matched what you have described though so please comment back with any feedback.

I have my doubts that you can define URL mappings handlers in both the XML and Annotations since only one mapper can be used by servlet context and it would mean that your annotation and XML mapping would have to line up exactly.

like image 70
Andrew White Avatar answered Sep 24 '22 15:09

Andrew White