45.定制化原理-SpringBoot定制化组件的几种方式(小结)

修改配置文件

xxxxxCustomizer

编写自定义的配置类 xxxConfiguration + @Bean替换、增加容器中默认组件,视图解析器

Web应用 编写一个配置类实现 WebMvcConfigurer 即可定制化web功能 + @Bean给容器中再扩展一些组件

@Configuration
public class AdminWebConfig implements WebMvcConfigurer{
}

@EnableWebMvc + WebMvcConfigurer — @Bean 可以全面接管SpringMVC,所有规则全部自己重新配置; 实现定制和扩展功能(高级功能,初学者退避三舍)。
原理:
WebMvcAutoConfiguration默认的SpringMVC的自动配置功能类,如静态资源、欢迎页等。
一旦使用 @EnableWebMvc ,会@Import(DelegatingWebMvcConfiguration.class)。
DelegatingWebMvcConfiguration的作用,只保证SpringMVC最基本的使用
把所有系统中的WebMvcConfigurer拿过来,所有功能的定制都是这些WebMvcConfigurer合起来一起生效。
自动配置了一些非常底层的组件,如RequestMappingHandlerMapping,这些组件依赖的组件都是从容器中获取如。
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport。
WebMvcAutoConfiguration里面的配置要能生效必须 @ConditionalOnMissingBean(WebMvcConfigurationSupport.class)。
@EnableWebMvc 导致了WebMvcAutoConfiguration 没有生效。
原理分析套路
场景starter - xxxxAutoConfiguration - 导入xxx组件 - 绑定xxxProperties - 绑定配置文件项。

 

posted @ 2022-08-09 14:55  随遇而安==  阅读(105)  评论(0编辑  收藏  举报