SpringBoot2.x入门到项目实战课程系列(第五章)( 八 )
原理:
1、自定义WebMvcConfigurer自动配置时会导入;
导入EnableWebMvcConfiguration.class@Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class})@EnableConfigurationProperties({WebMvcProperties.class, ResourceProperties.class})@Order(0)public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer,ResourceLoaderAware {2、EnableWebMvcConfiguration 继承了 DelegatingWebMvcConfiguration
@Configurationpublic static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration {3、分析 DelegatingWebMvcConfiguration,会将所有web配置组件加到WebMvcConfigurerComposite中
@Configurationpublic class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport { //存储所有的mvc配置类组件 private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite(); @Autowired( required = false ) public void setConfigurers(List4、保留原来的配置类 , 也添加了新的配置类 , 所有的WebMvcConfigurer都会一起起作用
5、效果:SpringMVC的自动配置和我们的扩展配置都会起作用;
5.7 全面控制 SpringMVC如果你想全面控制SpringMVC(SpringBoot对SpringMVC的自动配置都废弃), 在自定义的Web配置类上添加@Configuration 和 @EnableWebMvc 注解 。
/*** @Auther: Dragon Wen*/@EnableWebMvc@Configurationpublic class MySpringMvcConfigurer implements WebMvcConfigurer{ @Override public void addViewControllers(ViewControllerRegistry registry) {// super.addViewControllers(registry);//发送 /mengxuegu 请求来到 success.htmlregistry.addViewController("/dragonwen").setViewName("success"); }}原理: 为什么添加 @EnableWebMvc 自动配置就失效了?
1、@EnableWebMvc 的核心
@Import(DelegatingWebMvcConfiguration.class)public @interface EnableWebMvc {2、先记住继承了WebMvcConfigurationSupport类
@Configurationpublic class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {3、而在 WebMvcAutoConfiguration 上使用了@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
容器中没有这个组件的时候 , 这个自动配置类才生效
容器中没有这个组件的时候 , 这个自动配置类才生效@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})@AutoConfigureOrder(-2147483638)@AutoConfigureAfter({DispatcherServletAutoConfiguration.class,ValidationAutoConfiguration.class})public class WebMvcAutoConfiguration {**而 @ConditionalOnMissingBean 表示的是没有WebMvcConfigurationSupport这个组件,****WebMvcAutoConfiguration自动配置类才会生效.**4、相反 @EnableWebMvc 将 WebMvcConfigurationSupport 组件导入进来, 使得
WebMvcAutoConfiguration就失效了
推荐阅读
- 徐福记联手JDL京东物流向数智化转型,首次落地智慧园区项目
- 苹果两款新iPad齐曝光:性能提高、入门款更轻薄、售价便宜
- 运动计数开发项目的对抗赛:飞算全自动软件工程平台碾压传统模式
- RHEL 9提升了x86_64处理器的入门要求
- 江北新区企业院士工作站技术攻关项目立项数位居南京第一
- 入门HiFi享好声,这几款耳机绝对值得入手
- 赞!盐城高新区4个项目入选省级项目立项!
- 威海高新区2项目获2020年度山东省重点研发计划(重大科技创新工程)立项支持
- 浙江公布首批“互联网+”医疗服务价格项目!适用各级公立医疗机构
- 168天封顶!投资56亿重点AI项目将在上海竣工,提供世界领先算力支撑
