I'm relatively new to Spring and I'm a little confused about the tag.
After going through the documentation and looking around different posts it seems like the main use of is that it is required by Spring MVC to dispatch requests to @Controllers.
I created a controller with two requestMappings:
@RequestMapping(method=RequestMethod.GET, value="/health")
@RequestMapping(method=RequestMethod.GET, value="/test")
I tested the web app with and without in the servlet.xml and it doesn't seem like any difference was made with being omitted or not. The requests seemed to still reach my controller just fine.
Can anyone explain to me what exactly that tag is used for?
Thanks in advance!
The support for @Controller
and @RequestMapping
is provided by Spring by default. However, by enabling mvc:annotation-driven
you get support for processing the requests that are mapped to annotated controller methods, such as declarative validation, formatting and conversion service. An excerpt from spring's blog that introduced the new config features
It applies sensible defaults based on what is present in your
classpath. Such defaults include:
Another related usefull blog post
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
.
- Support for Message Converters
Finally a more formal definition in the official docs