Springboot @EnableWebMvc 注解


@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 模塊的自動配置失效

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM