Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does <mvc:annotation-driven /> do?

Tags:

spring-mvc

I use filenames in my REST API (example: GET http://xxx/api/myImage.jpg) problem is @PathVariable dropped ".jpg". this problems already asked few times in here and answered. but not worked to me.

so I searched then found at the

https://jira.springsource.org/browse/SPR-6524

"... is simply not supposed to be combined with manual DefaultAnnotationHandlerMapping instances; this is designed as an either-or choice at present, quite similar to and ."

"mvc namespace are make simplifed configurations".

Real question is mvc what does do? and changed?

I found my self these things..

  1. intercepter configuration changed. (mvc namspace required in bean configuation)
  2. useDefaultSuffixPattern is not working.
  3. adds JSON message converter. if jackson library is available
  4. @PathVariable arguments are auto added to model

Any others?

Thanks in advance!

like image 925
Kisong Kim Avatar asked Nov 17 '11 05:11

Kisong Kim


3 Answers

The mvc:annotationDriven tag essentially sets you your Spring context to allow for dispatching requests to Controllers.

The tag will configure two beans DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter.

You can find more information from the spring documents:

http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html

like image 163
Roy Kachouh Avatar answered Sep 23 '22 05:09

Roy Kachouh


Before I provide certain points let me clear up the answer provided by Roy is not accurate. You don't have to provide mvc:annotation-driven tag to instantiate default beans. This tag can be used Spring 3.0+ to enable new feature introduced from Spring 3.0

(Do not use it if you want backward compatibility, especially if you are using old controller based classes like MultiActionController, SimpleFormController)

Now lets come to what this tag actually does -

Prior to Spring 3.1 default beans used where

  1. DefaultAnnotationHandlerMapping
  2. AnnotationMethodHandlerAdapter
  3. AnnotationMethodHandlerExceptionResolver

These are deprecated in Spring 3.1 and if you use above mentioned tag it will be replaced by -

  1. RequestMappingHandlerMapping
  2. RequestMappingHandlerAdapter
  3. ExceptionHandlerExceptionResolver

DefaultAnnotationHandlerMapping decided which controller to use and the AnnotationMethodHandlerAdapter selected the actual method that handled the request. RequestMappingHandlerMapping does both the tasks. Therefore the request is directly mapped right to the method.

There are other infrastructure beans that are instantiated by these tag (chained in addition to defaults) like - MappedInterceptor, ConfigurableWebBindingInitializer, SessionFlashManager, ContentNegociationManager etc. I am not going to explain these :) as they each are long answers themselves, so google it for more info.

PS : And yes Spring 3.1+ automatically expose @PathVariables to the model. Also you have mvc:interceptors tag. But I think it is not related to <mvc:annotation-driven />. I would highly recommend read - http://spring.io/blog/2009/12/21/mvc-simplifications-in-spring-3-0/

like image 31
Aniket Thakur Avatar answered Sep 21 '22 05:09

Aniket Thakur


To enable MVC Java config add the annotation @EnableWebMvc to one of your @Configuration classes:

@Configuration
@EnableWebMvc
public class WebConfig {

}

To achieve the same in XML use the mvc:annotation-driven element in your DispatcherServlet context (or in your root context if you have no DispatcherServlet context defined):

<?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.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven/>
</beans>

The above registers a RequestMappingHandlerMapping, a RequestMappingHandlerAdapter, and an ExceptionHandlerExceptionResolver (among others) in support of processing requests with annotated controller methods using annotations such as @RequestMapping, @ExceptionHandler, and others.

For details refer the below link :

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config

like image 36
Ashis Jena Avatar answered Sep 22 '22 05:09

Ashis Jena