Background
With Spring 3.0 came mvc name space and along with it came a lot of simplifications in Spring framework. The one that we are going to discuss today is - <mvc:annotation-driven />
This is the simplest tag you can add in your spring configuration file to enable many new features.
This is obvious you spring configuration xml tag. For java equivalent you can add the annotation @EnableWebMvc to one of your @Configuration classes.
The above registers a
This is obvious you spring configuration xml tag. For java equivalent you can add the annotation @EnableWebMvc to one of your @Configuration classes.
@Configuration @EnableWebMvc public class WebConfig { }
The above registers a
- RequestMappingHandlerMapping,
- a RequestMappingHandlerAdapter, and
- an ExceptionHandlerExceptionResolver
What's the difference between <mvc:annotation-driven /> and <context:annotation-config /> in servlet?
With <mvc:annotation-driven /> following features get enabled -
- Configures the Spring 3 Type ConversionService (alternative to PropertyEditors)
- Adds support for formatting Number fields with @NumberFormat
- Adds support for formatting Date, Calendar, and Joda Time fields with @DateTimeFormat, if Joda Time is on the classpath
- Adds support for validating @Controller inputs with @Valid, if a JSR-303 Provider is on the classpath
- Adds support for support for reading and writing XML, if JAXB is on the classpath (HTTP message conversion with @RequestBody/@ResponseBody)
- Adds support for reading and writing JSON, if Jackson is on the classpath (along the same lines as #5)
This is the complete list of HttpMessageConverters set up by mvc:annotation-driven:
- ByteArrayHttpMessageConverter converts byte arrays.
- StringHttpMessageConverter converts strings.
- ResourceHttpMessageConverter converts to/from org.springframework.core.io.Resource for all media types.
- SourceHttpMessageConverter converts to/from a javax.xml.transform.Source.
- FormHttpMessageConverter converts form data to/from a MultiValueMap<String, String>.
- Jaxb2RootElementHttpMessageConverter converts Java objects to/from XML — added if JAXB2 is present and Jackson 2 XML extension is not present on the classpath.
- MappingJackson2HttpMessageConverter converts to/from JSON — added if Jackson 2 is present on the classpath.
- MappingJackson2XmlHttpMessageConverter converts to/from XML — added if Jackson 2 XML extension is present on the classpath.
- AtomFeedHttpMessageConverter converts Atom feeds — added if Rome is present on the classpath.
- RssChannelHttpMessageConverter converts RSS feeds — added if Rome is present on the classpath.
context:annotation-config on the other hand looks for annotations on beans in the same application context it is defined and declares support for all the general annotations like @Autowired, @Resource, @Required, @PostConstruct etc etc.
No comments:
Post a Comment