About SpringMVC JAVA ANNOTATION
@EnableWebMvc
Reference
The above registers a RequestMappingHandlerMapping, a RequestMappingHandlerAdapter, and anExceptionHandlerExceptionResolver (among others) in support of processing requests with annotated controller methods using annotations such as @RequestMapping, @ExceptionHandler, and others.
It also enables the following:
- Spring 3 style type conversion through a ConversionService instance in addition to the JavaBeans PropertyEditors used for Data Binding.
- Support for formatting Number fields using the
@NumberFormatannotation through theConversionService. - Support for formatting Date, Calendar, Long, and Joda Time fields using the
@DateTimeFormatannotation. - Support for validating @Controller inputs with
@Valid, if a JSR-303 Provider is present on the classpath. -
HttpMessageConverter support for
@RequestBodymethod parameters and@ResponseBodymethod return values from@RequestMappingor@ExceptionHandlermethods.This is the complete list of HttpMessageConverters set up by mvc:annotation-driven:
ByteArrayHttpMessageConverterconverts byte arrays.StringHttpMessageConverterconverts strings.ResourceHttpMessageConverterconverts to/fromorg.springframework.core.io.Resourcefor all media types.SourceHttpMessageConverterconverts to/from ajavax.xml.transform.Source.FormHttpMessageConverterconverts form data to/from aMultiValueMap<String, String>.Jaxb2RootElementHttpMessageConverterconverts Java objects to/from XML — added if JAXB2 is present on the classpath.MappingJackson2HttpMessageConverter(orMappingJacksonHttpMessageConverter) converts to/from JSON — added if Jackson 2 (or Jackson) is present on the classpath.AtomFeedHttpMessageConverterconverts Atom feeds — added if Rome is present on the classpath.RssChannelHttpMessageConverterconverts RSS feeds — added if Rome is present on the classpath.
DOC
Add this annotation to an
@Configurationclass to have the Spring MVC configuration defined inWebMvcConfigurationSupportimported:@Configuration @EnableWebMvc @ComponentScan(basePackageClasses = { MyConfiguration.class }) public class MyWebConfiguration { }Customize the imported configuration by implementing the
WebMvcConfigurerinterface or more likely by extending theWebMvcConfigurerAdapterbase class and overriding individual methods:@Configuration @EnableWebMvc @ComponentScan(basePackageClasses = { MyConfiguration.class }) public class MyConfiguration extends WebMvcConfigurerAdapter { @Override public void addFormatters(FormatterRegistry formatterRegistry) { formatterRegistry.addConverter(new MyConverter()); } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(new MyHttpMessageConverter()); } // More overridden methods ... }If the customization options of
WebMvcConfigurerdo not expose something you need to configure, consider removing the@EnableWebMvcannotation and extending directly fromWebMvcConfigurationSupportoverriding selected@Beanmethods:@Configuration @ComponentScan(basePackageClasses = { MyConfiguration.class }) public class MyConfiguration extends WebMvcConfigurationSupport { @Override public void addFormatters(FormatterRegistry formatterRegistry) { formatterRegistry.addConverter(new MyConverter()); } @Bean public RequestMappingHandlerAdapter requestMappingHandlerAdapter() { // Create or delegate to "super" to create and // customize properties of RequestMapingHandlerAdapter } }
org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport
This is the main class providing the configuration behind the MVC Java config. It is typically imported by adding @EnableWebMvc to an application @Configuration class. An alternative more advanced option is to extend directly from this class and override methods as necessary remembering to add @Configuration to the subclass and @Bean to overridden @Bean methods. For more details see the Javadoc of@EnableWebMvc.
This class registers the following HandlerMappings:
RequestMappingHandlerMappingordered at 0 for mapping requests to annotated controller methods.HandlerMappingordered at 1 to map URL paths directly to view names.BeanNameUrlHandlerMappingordered at 2 to map URL paths to controller bean names.HandlerMappingordered atInteger.MAX_VALUE-1to serve static resource requests.HandlerMappingordered atInteger.MAX_VALUEto forward requests to the default servlet.
Registers these HandlerAdapters:
RequestMappingHandlerAdapterfor processing requests with annotated controller methods.HttpRequestHandlerAdapterfor processing requests withHttpRequestHandlers.SimpleControllerHandlerAdapterfor processing requests with interface-basedControllers.
Registers a HandlerExceptionResolverComposite with this chain of exception resolvers:
ExceptionHandlerExceptionResolverfor handling exceptions through @ExceptionHandlermethods.ResponseStatusExceptionResolverfor exceptions annotated with @ResponseStatus.DefaultHandlerExceptionResolverfor resolving known Spring exception types
Both the RequestMappingHandlerAdapter and the ExceptionHandlerExceptionResolver are configured with default instances of the following by default:
- A
ContentNegotiationManager - A
DefaultFormattingConversionService - A
LocalValidatorFactoryBeanif a JSR-303 implementation is available on the classpath - A range of
HttpMessageConverters depending on the 3rd party libraries available on the classpath.
org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration
WebMvcConfigurationSupport that detects and delegates to all beans of type WebMvcConfigurer allowing them to customize the configuration provided by WebMvcConfigurationSupport. This is the class actually imported by @EnableWebMvc.org.springframework.web.servlet.config.annotation.WebMvcConfigurer
Defines callback methods to customize the Java-based configuration for Spring MVC enabled via @EnableWebMvc.
@EnableWebMvc-annotated configuration classes may implement this interface to be called back and given a chance to customize the default configuration. Consider extending WebMvcConfigurerAdapter, which provides a stub implementation of all interface methods.
org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
An implementation of WebMvcConfigurer with empty methods allowing sub-classes to override only the methods they're interested in.
搞清这四个类的关系。
浙公网安备 33010602011771号