@Retention(value=RUNTIME) @Target(value=TYPE) @Documented @Import(value=DelegatingWebMvcConfiguration.class) public @interface EnableWebMvc
@Configuration
class imports the Spring MVC
configuration from WebMvcConfigurationSupport
, e.g.:
@Configuration @EnableWebMvc @ComponentScan(basePackageClasses = { MyConfiguration.class }) public class MyWebConfiguration { }
To customize the imported configuration, implement the interface
WebMvcConfigurer
or more likely extend the empty method base class
WebMvcConfigurerAdapter
and override individual methods, e.g.:
@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 WebMvcConfigurer
does not expose some advanced setting that
needs to be configured, consider removing the @EnableWebMvc
annotation and extending directly from WebMvcConfigurationSupport
or DelegatingWebMvcConfiguration
, 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 RequestMapingHandlerAdapter } }
WebMvcConfigurer
,
WebMvcConfigurerAdapter
,
WebMvcConfigurationSupport
,
DelegatingWebMvcConfiguration