注释类型 EnableWebMvc
@Retention(RUNTIME) @Target(TYPE) @Documented @Import(DelegatingWebMvcConfiguration.class) public @interface EnableWebMvc
Adding this annotation to an@Configurationclass imports the Spring MVC configuration fromWebMvcConfigurationSupport, e.g.:@Configuration @EnableWebMvc @ComponentScan(basePackageClasses = MyConfiguration.class) public class MyConfiguration { }To customize the imported configuration, implement the interface
WebMvcConfigurerand override individual methods, e.g.:@Configuration @EnableWebMvc @ComponentScan(basePackageClasses = MyConfiguration.class) public class MyConfiguration implements WebMvcConfigurer { @Override public void addFormatters(FormatterRegistry formatterRegistry) { formatterRegistry.addConverter(new MyConverter()); } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(new MyHttpMessageConverter()); } }Note: only one
@Configurationclass may have the@EnableWebMvcannotation to import the Spring Web MVC configuration. There can however be multiple@Configurationclasses implementingWebMvcConfigurerin order to customize the provided configuration.If
WebMvcConfigurerdoes not expose some more advanced setting that needs to be configured consider removing the@EnableWebMvcannotation and extending directly fromWebMvcConfigurationSupportorDelegatingWebMvcConfiguration, e.g.:@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 RequestMappingHandlerAdapter } }- 从以下版本开始:
- 3.1
- 作者:
- Dave Syer, Rossen Stoyanchev
- 另请参阅:
WebMvcConfigurer,WebMvcConfigurationSupport,DelegatingWebMvcConfiguration