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(List
4、保留原来的配置类 , 也添加了新的配置类 , 所有的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就失效了
- 固态硬盘入门手册,一看就精通的电脑硬件教程
- SpringBoot2.x入门到项目实战课程系列(第二章)
- 「精选」网易严选质量数仓建设(二)—质量数仓项目建设及管理
- 「OpenCV入门教程」线性滤波方框滤波、均值滤波与高斯滤波
- Go 实战项目推荐:Gin 实现的工单系统
- Python爬虫入门第一课:如何解析网页
- 更新了!深入浅出图解Git,入门到精通(保姆级教程)第三篇
- 宁夏举办第35届青少年科技创新大赛机器人竞赛项目
- 入门到老手全覆盖,锐龙CPU给3D创作加速
- 昔日i7沦为i3,英特尔十代i3或成入门游戏CPU最优选