The annotations based MVC was introduced to the framework in Spring 2.5. This model enables the developer to reuse any POJO as a controller and is very flexible with the handler signatures. The old controller hierarchy is deprecated as of Spring 3.0. It would be removed completely from the distribution in one of the future releases.
<mvc:annotation-driven> tag should be added to your Web Application context XML. This tag defaults the basic components required for delegating the requests to your Controllers.
If this tag is not added to the XML, then you will have to manually define the beans for components like HandlerAdapter, HandlerMapping, Binding Initializer, Request Message converters, etc. This tag helps registering the following components.
<mvc:annotation-driven> tag should be added to your Web Application context XML. This tag defaults the basic components required for delegating the requests to your Controllers.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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">
<mvc:annotation-driven />
</beans>
If this tag is not added to the XML, then you will have to manually define the beans for components like HandlerAdapter, HandlerMapping, Binding Initializer, Request Message converters, etc. This tag helps registering the following components.
- DefaultAnnotationHandlerMapping - This is a HandlerMapping implementation which maps the HTTP requests to the handler methods defined using the @RequestMapping annotation.
- AnnotationMethodHandlerAdapter - It is responsible for scanning the controllers to identify methods (and parameters) annotated with @MVC annotations. It scans and caches handler methods annotated with @RequestMapping. Also handles the @RequestParam, @ModelAttribute, @SessionAttributes and @InitBinder annotations.
- ConfigurableWebBindingInitializer - The initializer for the Web Data Binder. Helps in declaratively configuring the Web Binder with validators, conversion services, property editors, etc.
- LocalValidatorFactoryBean - Implements the validator interface and enables JSR303 validation. This is injected into ConfigurableWebBindingInitializer.
- FormattingConversionServiceFactoryBean - A conversion factory that returns conversion services for basic objects like date and numbers. This factory is again injected into ConfigurableWebBindingInitializer.
- Message Converters
- ByteArrayHttpMessageConverter - A HTTP request message converter that reads a HTTP message body and returns a byte stream. It can also read a byte stream and construct a response body. Used for receiving and sending documents like PDF, XLS, etc.
- StringHttpMessageConverter - A HTTP request message converter that reads a plain text request body and binds it to a String object. And vice-versa with response.
- FormHttpMessageConverter - A HTTP request message converter that reads a form encoded request body and binds it to a form Binding object.
- SourceHttpMessageConverter - A HTTP request converter that converts a XML message body to/from Binding Object.
<beans xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="0">
</property></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="validator" ref="validator">
</property></bean>
</property>
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<bean class="org.springframework.http.converter.FormHttpMessageConverter">
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter">
</bean></bean></bean></bean></list>
</property>
</bean>
<bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" id="validator">
<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean" id="conversion-service">
</bean></bean></beans>