您的位置:首页 > 编程语言 > Java开发

springboot自动配置原理

2019-07-22 18:04 453 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/VInllor/article/details/96867973

配置文件到底能写什么?怎么写?自动配置原理

配置文件能配置的属性参照

自动配置原理:

  1. springboot启动时加载主配置类,开启了自动配置功能@EnableAutoConfiguration
  2. @EnableAutoConfiguration作用:
  • 利用AutoConfigurationImportSelector给容器中导入组件?
  • 查看selectImports()的内容
  • List configurations = this.getCandidateConfigurations(annotationMetadata, attributes);获取候选的配置
SpringFactoriesLoader.loadFactoryNames
扫描所有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,\
......

每一个这样的xxxAutoConfiguration类都是容器中的一个组件,都加入到容器中;用他们来做自动配置;

  1. 每一个自动配置类进行自动配置功能
  2. 解释自动配置原理
    eg: HttpEncodingAutoConfiguration(Http编码自动配置)
@Configuration   //表示这是一个配置类,类似以前编写的配置文件,也可以给容器中添加组件

// 启动ConfigurationProperties功能	将配置文件中对应的值和HttpEncodingProperties绑定起来
@EnableConfigurationProperties({HttpProperties.class}) 
// spring底层@Conditional,根据不同的条件,如果满足指定的条件,
//整个配置类里面的配置就会生效;判断当前应用是否为web应用,如果是当前配置类生效
@ConditionalOnWebApplication(
type = Type.SERVLET
)
//判断当前项目有没有这个类
// CharacterEncodingFilter:springmvc 中用来乱码解决的过滤器
@ConditionalOnClass({CharacterEncodingFilter.class})
//判断配置文件中是否存在某个配置  spring.http.encoding.enabled如果不存在,判断也成立
// 配置文件中不配置spring.http.encoding.enabled=true ,也是默认生效
@ConditionalOnProperty(
prefix = "spring.http.encoding",
value = {"enabled"},
matchIfMissing = true
)
public class HttpEncodingAutoConfiguration {
   // 已经和springboot的配置文件映射
   private final Encoding properties;
 
 //只有一个参数构造器的情况下,参数的值就会从容器中那=拿
public HttpEncodingAutoConfiguration(HttpProperties properties) {
this.properties = properties.getEncoding();
}
// 向容器中添加组件
     @Bean
 @ConditionalOnMissingBean
public CharacterEncodingFilter characterEncodingFilter() {
CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
filter.setEncoding(this.properties.getCharset().name());
filter.setForceRequestEncoding(this.properties.shouldForce(org.springframework.boot.autoconfigure.http.HttpProperties.Encoding.Type.REQUEST));
filter.setForceResponseEncoding(this.properties.shouldForce(org.springframework.boot.autoconfigure.http.HttpProperties.Encoding.Type.RESPONSE));
return filter;
}

根据当前不同的条件,决定这个配置类是否生效
一旦生效,这个配置类就会给容器中添加各种组件,这些组件的属性是从对应的properties类中获取的,这些类里面的属性又是和配置文件绑定的.

  1. 所有在配置文件中能配置的属性都在xxxxProperties类中封装着;配置文件可以配置什么,就可以参照该功能对应的属性类
//从配置文件中获取指定的值和bean的属性进行绑定
@ConfigurationProperties(
prefix = "spring.http"
)
public class HttpProperties {

6) springboot 精髓

  • springboot 启动会加载大量的配置类
  • 看springboot默认写好的自动配置类是否有我们需要的功能;
  • 在看自动配置类中都配置了哪些组件(只要我们要的组件有就不需要配置了)
  • 给容器中自动配置类添加组件时,会从properties类中获取某些属性,我们就可以在配置文件中指定这些属性的值

xxxAutoConfiguration  自动配置类
给容器中添加组件
xxxProperties封装配置文件中相关属性

  1. 查看生效和没有生效的自动配置类

在主配置类中配置 debug=true,运行项目,在控制台查看

============================
CONDITIONS EVALUATION REPORT
============================

Positive matches:(生效的自动配置类)
-----------------

CodecsAutoConfiguration matched:
- @ConditionalOnClass found required class 'org.springframework.http.codec.CodecConfigurer' (OnClassCondition)

CodecsAutoConfiguration.JacksonCodecConfiguration matched:
- @ConditionalOnClass found required class 'com.fasterxml.jackson.databind.ObjectMapper' (OnClassCondition)

CodecsAutoConfiguration.JacksonCodecConfiguration#jacksonCodecCustomizer matched:
- @ConditionalOnBean (types: com.fasterxml.jackson.databind.ObjectMapper; SearchStrategy: all) found bean 'jacksonObjectMapper' (OnBeanCondition)

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:
Did not match:
- @ConditionalOnClass did not find required class 'org.aspectj.lang.annotation.Aspect' (OnClassCondition)

ArtemisAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: