8.1 定制化的常见方式
方式一:修改配置文件
方式二:XxxCustomizer 定制化器
方式三:编写自定义的配置类 xxxConfiguration + @Bean替换、增加容器中默认组件、视图解析器
Web应用 编写一个配置类实现 WebMvcConfigurer 即可定制化web功能;+ @Bean给容器中再扩展一些组件
@Configuration
public class MyWebConfig implements WebMvcConfigurer
-
@EnableWebMvc + WebMvcConfigurer ——> @Bean 可以全面接管SpringMVC,所有规则全部自己重新配置; 实现定制和扩展功能
-
- 原理
- 1、WebMvcAutoConfiguration 默认的SpringMVC的自动配置功能类。静态资源、欢迎页…
- 2、一旦使用 @EnableWebMvc 注解会 @Import(DelegatingWebMvcConfiguration.class)
- 3、DelegatingWebMvcConfiguration 的作用,只保证SpringMVC最基本的使用功能
-
-
- 把所有系统中的 WebMvcConfigurer 拿过来。所有功能的定制都是这些 WebMvcConfigurer 合起来一起生效
- 自动配置了一些非常底层的组件。RequestMappingHandlerMapping等这些组件依赖的组件都是从容器中获取
- public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport
-
-
- 4、WebMvcAutoConfiguration 里面的配置要能生效必须满足此条件:@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
- 5、@EnableWebMvc 导致了 WebMvcAutoConfiguration 没有生效。
8.2 原理分析套路
场景starter –> xxxxAutoConfiguration --> @Bean导入xxx组件 --> 绑定xxxProperties --> 绑定配置文件项