在WebMvcAutoConfiguration的源码中,类注释上有@ConditionalOnMissingBean({WebMvcConfigurationSupport.class}),我们知道容器中只要存在WebMvcConfigurationSupport.class类的实例,那么这个webmvc自动配置就会失效
@Configuration
@ConditionalOnWebApplication
@**ConditionalOnClass**({Servlet.class, DispatcherServlet.class, WebMvcConfigurerAdapter.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {
但是在WebMvcAutoConfiguration这个类中有这样一段代码,加粗部分,导入了EnableWebMvcConfiguration.class这个类,我们点进EnableWebMvcConfiguration.class这个类,发现它是继承DelegatingWebMvcConfiguration这个类,同时这个类继承至DelegatingWebMvcConfiguration,而这个DelegatingWebMvcConfiguration继承至**WebMvcConfigurationSupport**,这里为什么不会导致默认的webmvc自动配置失效。而我们添加上@EnableWebMvc这个注解,也是因为引入WebMvcConfigurationSupport这个类实例才导致自动配置失效的。
@Configuration
@**Import**({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class})
@EnableConfigurationProperties({WebMvcProperties.class, ResourceProperties.class})
public static class WebMvcAutoConfigurationAdapter extends WebMvcConfigurerAdapter {
@Configuration
public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration {
@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {