一.问题的引入
配置文件到底能写什么?怎么写?自动配置原理?
配置文件能配置的属性参照:https://docs.spring.io/spring-boot/docs/2.2.1.RELEASE/reference/htmlsingle/#appendix
官方文档详细的介绍了每一种能配置的属性以及解释,不过这样看起来特别麻烦。
二.自动配置原理
1)、SpringBoot启动的时候加载主配置类,开启了自动配置功能@EnableAutoConfigration
2)、@EnableAutoConfigration作用:
利用AutoConfigurationImportSelector给容器中导入一些组件?
可以查看selectImports方法的内容:
//获取候选的配置 Listconfigurations = getCandidateConfigurations(annotationMetadata, attributes); List configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader()); // 扫描所有jar包类路径下的META-INF/spring.factories // 把扫描到的这些文件的内容包装成properties对象 // 从properties中获取到EnableAutoConfiguration.class类(类名)对应的值,然后把他们添加在容器中
将类路径下META-INF/spring.factories里面配置的所有EnableAutoConfiguration的值添加到了容器中
#Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\ org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\ org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\ org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\ org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\ org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\ org.springframework.boot.autoconfigure.cloud.CloudServiceConnectorsAutoConfiguration,\ org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\ //这里只截取一小部分
每一个这样的xxxAutoConfiguration类都是容器一个组件,都加入到容器中,用他们来做自动配置
3)、每一个自动配置类进行自动配置功能
4)、以HttpEncodingAutoConfiguration 为例解释自动配置原理
@Configuration(proxyBeanMethods = false)//表示这是一个配置类,类似于我们自己写的配置文件,给容器中添加组件 @EnableConfigurationProperties(HttpProperties.class) //启用指定类的ConfigurationProperties功能;将配置文件中对应的值和HttpEncodingProperties绑定起来; //并把HttpEncodingProperties加入到IOC容器中 @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) //Spring底层@Conditional注解,根据不同的条件,如果满足指定的条件,整个配置类配置类里面的配置才会生效; //判断当前应用是否是web应用,如果是,当前配置类生效 @ConditionalOnClass(CharacterEncodingFilter.class) //判断当前项目有没有CharacterEncodingFilter这个类;这个CharacterEncodingFilter是SpringMvc中进行乱码解决的过滤器 @ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true) //判断配置文件中是否存在某个配置,spring.http.encoding.enabled;如果不存在,判断也是成立的; //即使配置文件中不配置spring.http.encoding.enabled这个属性,也是默认生效的; public class HttpEncodingAutoConfiguration { //它已经和SpringBoot的配置文件映射了 private final HttpProperties.Encoding properties; //只有一个有参构造器的情况下,参数的值就会从容器中拿 public HttpEncodingAutoConfiguration(HttpProperties properties) { this.properties = properties.getEncoding(); } @Bean//给容器中添加一个组件,这个组件的某些值需要从properties中获取 @ConditionalOnMissingBean public CharacterEncodingFilter characterEncodingFilter() { CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter(); filter.setEncoding(this.properties.getCharset().name()); filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST)); filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE)); return filter; }
根据当前不同的条件判断,决定这个配置类是否生效?
一旦这个配置类生效;这个配置类就会给容器中添加各种组件;这些组件的属性都是从对于的properties类中获取的,而这些类中的每一个属性又是和配置文件绑定的;
5)、所有在配置文件中能配置的属性都是在xxxProperties类中封装着;配置文件能配置什么就可以参照某个功能对应的这个属性类
@ConfigurationProperties(prefix = "spring.http")//从配置文件中获取指定的值和bean的属性进行绑定 public class HttpProperties { /** * Whether logging of (potentially sensitive) request details at DEBUG and TRACE level * is allowed. */ private boolean logRequestDetails;
精髓:
1)、SpringBoot启动会加载大量的自动配置类
2)、我们看我们需要的功能有没有SpringBoot默认写好的自动配置类;
3)、我们再来看这个自动配置类中到底配置了哪些组件;(只要我们要用的组件有,我们就不需要再来配置了)
4)、给容器中自动配置类添加组件的时候,会从properties类中获取某些属性。我们就可以在配置文件中指定这些属性的值;
活用:比如随便找一个xxxAutoConfiguration类
自动配置能够起作用的原理:
1、@Conditional派生注解(Spring注解版原生的@Conditional作用)
作用:必须是@Conditional指定的条件成立,才给容器中添加组件,配置配里面的所有内容才生效;
@Conditional扩展注解 | 作用(判断是否满足当前指定条件) |
---|---|
@ConditionalOnJava | 系统的java版本是否符合要求 |
@ConditionalOnBean | 容器中存在指定Bean |
@ConditionalOnMissingBean | 容器中不存在指定Bean |
@ConditionalOnExpression | 满足SpEL表达式指定 |
@ConditionalOnClass | 系统中有指定的类 |
@ConditionalOnMissingClass | 系统中没有指定的类 |
@ConditionalOnSingleCandidate | 容器中只有一个指定的Bean,或者这个Bean是首选Bean |
@ConditionalOnProperty | 系统中指定的属性是否有指定的值 |
@ConditionalOnResource | 类路径下是否存在指定资源文件 |
@ConditionalOnWebApplication | 当前是web环境 |
@ConditionalOnNotWebApplication | 当前不是web环境 |
@ConditionalOnJndi | JNDI存在指定项 |
自动配置类必须在一定的条件下才能生效;
我们怎么知道哪些自动配置类生效?
我们可以通过启用 debug=true属性;来让控制台打印自动配置报告,这样我们就可以很方便的知道哪些自动配置类生效;
下面是控制台的打印片段
============================
CONDITIONS EVALUATION REPORT
============================
Positive matches:
AopAutoConfiguration matched:
- @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition)
AopAutoConfiguration.ClassProxyingConfiguration matched:
- @ConditionalOnMissingClass did not find unwanted class ‘org.aspectj.weaver.Advice’ (OnClassCondition)
- @ConditionalOnProperty (spring.aop.proxy-target-class=true) matched (OnPropertyCondition)
DispatcherServletAutoConfiguration matched:
- @ConditionalOnClass found required class ‘org.springframework.web.servlet.DispatcherServlet’ (OnClassCondition)
- found ‘session’ scope (OnWebApplicationCondition)
Negative matches:
ActiveMQAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class ‘javax.jms.ConnectionFactory’ (OnClassCondition)
AopAutoConfiguration.AspectJAutoProxyingConfiguration:
Did not match:
- @ConditionalOnClass did not find required class ‘org.aspectj.weaver.Advice’ (OnClassCondition)