为什么springboot中引入WebMvcConfigurationSupport后SpringMvc的自动配置失效了

SpringBoot的官方文档中,可以看到, 如果想保持Spring Boot MVC原本的配置(自动配置)并且又想增加自己的配置,那么add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc

 

 

当使用@EnableWebMvc,在这个类中可以看到

可以看到EnableWebMvc这个类里通过import引入 @Configuration 注解的类——DelegatingWebMvcConfiguration

来到DelegatingWebMvcConfiguration这个类里可以看到如下:

 

 

 DelegatingWebMvcConfiguration这个类又继承了 WebMvcConfigurationSupport这个类。

 

接着来到通过Ctrl+N查找并进到WebMvcAutoConfiguration这个类

 

在WebMvcAutoConfiguration这个类里看到有:

@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)这个注解的意义是,当没有WebMvcConfigurationSupport这个类的时候,WebMvcAutoConfiguration(也就是SpringMvc的自动配置类)才会生效
综上,可以得出为什么引入WebMvcConfigurationSupport这个类后SpringMvc的自动配置就会失效!

当然,如果你的需求是全部自己写SpringMvc的配置,那就要使用@EnableWebMvc来使它的自动配置失效了。

 

 

原文地址:https://www.cnblogs.com/Guhongying/p/10859486.html