About SpringMVC JAVA ANNOTATION

@EnableWebMvchtml

Referencejava

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.web

It also enables the following:spring

  1. Spring 3 style type conversion through a ConversionService instance in addition to the JavaBeans PropertyEditors used for Data Binding.
  2. Support for formatting Number fields using the @NumberFormat annotation through theConversionService.
  3. Support for formatting Date, Calendar, Long, and Joda Time fields using the @DateTimeFormatannotation.
  4. Support for validating @Controller inputs with @Valid, if a JSR-303 Provider is present on the classpath.
  5. HttpMessageConverter support for @RequestBody method parameters and @ResponseBody method return values from @RequestMapping or @ExceptionHandler methods.api

    This is the complete list of HttpMessageConverters set up by mvc:annotation-driven:mvc

    1. ByteArrayHttpMessageConverter converts byte arrays.
    2. StringHttpMessageConverter converts strings.
    3. ResourceHttpMessageConverter converts to/from org.springframework.core.io.Resource for all media types.
    4. SourceHttpMessageConverter converts to/from a javax.xml.transform.Source.
    5. FormHttpMessageConverter converts form data to/from a MultiValueMap<String, String>.
    6. Jaxb2RootElementHttpMessageConverter converts Java objects to/from XML — added if JAXB2 is present on the classpath.
    7. MappingJackson2HttpMessageConverter (or MappingJacksonHttpMessageConverter) converts to/from JSON — added if Jackson 2 (or Jackson) is present on the classpath.
    8. AtomFeedHttpMessageConverter converts Atom feeds — added if Rome is present on the classpath.
    9. RssChannelHttpMessageConverter converts RSS feeds — added if Rome is present on the classpath.

    DOCapp

    Add this annotation to an @Configuration class to have the Spring MVC configuration defined in WebMvcConfigurationSupport imported:ide

     @Configuration
     @EnableWebMvc
     @ComponentScan(basePackageClasses = { MyConfiguration.class })
     public class MyWebConfiguration {
     }

     

    Customize the imported configuration by implementing the WebMvcConfigurer interface or more likely by extending theWebMvcConfigurerAdapter base class and overriding individual methods:this

     

     @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 WebMvcConfigurer do not expose something you need to configure, consider removing the @EnableWebMvcannotation and extending directly from WebMvcConfigurationSupport overriding selected @Bean methods:spa

     

     @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:

Registers these HandlerAdapters:

Registers a HandlerExceptionResolverComposite with this chain of exception resolvers:

Both the RequestMappingHandlerAdapter and the ExceptionHandlerExceptionResolver are configured with default instances of the following by default:

 org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration

A sub-class of 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.

 搞清這四個類的關係。

相關文章
相關標籤/搜索