@EnableWebMvc 注解的源碼如下
可以看出,真正起作用的是DelegatingWebMvcConfiguration這個類,點進去看一下這個類
這個類繼承了 WebMvcConfigurationSupport 這個類,點開這個類
public class WebMvcConfigurationSupport implements ApplicationContextAware, ServletContextAware {
protected void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
}
protected void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
}
protected void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
}
protected void addViewControllers(ViewControllerRegistry registry) {
}
....
}
可以看到這個類中都是一些空方法,只是保留了最基本的 MVC 的功能,並不具備其它的擴展功能
並且如果容器中存在 WebMvcConfigurationSupport 這個類,那么 springboot 對於 springmvc 的自動配置將會失效
如下:springboot 對於 mvc 模塊的配置就在 WebMvcAutoConfiguration 這個類中,如果容器中沒有 WebMvcConfigurationSupport 這個類的情況下, springboot 對於 mvc 模塊的自動配置才會生效,而 @EnableWebMvc 注解就是向容器中添加 WebMvcConfigurationSupport 這個類,所以它會導致 springboot 對 mvc 模塊的自動配置失效