三者区别:@EnableWebMvc WebMvcConfigurationSupport WebMvcConfigurerAdapter
结论: @EnableWebMvc 就是 WebMvcConfigurationSupport,但是 如果想个性化重写某些方法,请 继承 WebMvcConfigurerAdapter 重写即可。如果还是满足不了,请删除 @EnableWebMvc 后 继承 WebMvcConfigurationSupport 重写方法。
@EnableWebMvc annotation is used there's no need to extend directly WebMvcConfigurationSupport, and you should just extend WebMvcConfigurerAdapter

官网给出的解释 If the customization options ofWebMvcConfigurerdo not expose something you need to configure, consider removing the @EnableWebMvc annotation and extending directly fromWebMvcConfigurationSupportoverriding 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 } }

本文详细介绍了Spring MVC中@EnableWebMvc、WebMvcConfigurationSupport和WebMvcConfigurerAdapter的区别。@EnableWebMvc用于启用Spring MVC配置,而WebMvcConfigurationSupport是其实现类。若需要自定义配置,可以继承WebMvcConfigurerAdapter并重写所需方法。如果默认配置仍不满足需求,可以去掉@EnableWebMvc,直接继承WebMvcConfigurationSupport来完全定制配置。

被折叠的 条评论
为什么被折叠?



