Spring @EnableWebMvc

来源:互联网 发布:易娱网络太古汇39楼 编辑:程序博客网 时间:2024/06/11 23:23

org.springframework.web.servlet.config.annotation 
Annotation Type EnableWebMvc


@Retention(value=RUNTIME)@Target(value=TYPE)@Documented@Import(value=DelegatingWebMvcConfiguration.class)public @interface EnableWebMvc

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

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

Customize the imported configuration by implementing the WebMvcConfigurer interface or more likely by extending the WebMvcConfigurerAdapter base 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 WebMvcConfigurer do not expose something you need to configure, consider removing the @EnableWebMvc annotation and extending directly from WebMvcConfigurationSupport overriding selected @Bean methods:

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

Since:
3.1
Author:
Dave Syer, Rossen Stoyanchev
See Also:
WebMvcConfigurerWebMvcConfigurerAdapter
0 0
原创粉丝点击