stevebot
stevebot

Reputation: 24005

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

Upvotes: 5

Views: 5074

Answers (3)

three-cups
three-cups

Reputation: 4385

Use

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

This will allow you to use your @RequestMapping annotated handlers along with your XML configured handlers. This was taken from the API docs:

If you define custom HandlerMapping beans in your DispatcherServlet context, you need to add a DefaultAnnotationHandlerMapping bean explicitly, since custom HandlerMapping beans replace the default mapping strategies.

I would not use the namespace directives (e.g. mvc:annotation-driven) when attempting to mix and match Spring MVC XML config and Annotation config. In fact, I try and avoid namespace directives all together. They are not well documented, and always less powerful than simply defining the beans directly in your XML config. Also, namespace directives tend to promise a lot ("just put this in your XML config and everything will work") but do not deliver because it's not always clear how they work and this makes debugger very difficult.

Here's an example of what I'm talking about in the above paragraph. Say you want to use some interceptors on your annotation configured controllers. This is pretty easy as long as you want to use the same interceptors for every controller. But if you want to use different interceptors for different controllers (when using annotation-based configuration), you cannot do this.

Upvotes: 2

Andrew White
Andrew White

Reputation: 53496

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.

Upvotes: 3

xxtommoxx
xxtommoxx

Reputation: 309

Have you tried

<context:annotation-config />

Upvotes: 0

Related Questions