AutoConfiguration自动配置原理

本文探讨了Spring Boot的AutoConfiguration自动配置原理,包括它如何根据项目依赖进行条件配置,以及配置文件中的属性如何通过xxxProperties类进行封装。
部署运行你感兴趣的模型镜像

AutoConfiguration自动配置原理

官网配置文件指导

https://docs.spring.io/spring-boot/docs/2.2.6.RELEASE/reference/html/appendix-application-properties.html#common-application-properties
原理:
1)SpringBoot启动时加载主配置类,来气自动配置功能 @EnableAutoConfiguration
2) @EnableAutoConfiguration 利用AutoConfigurationImportSelector.class 给容器中导入组件   selectImports{}方法的内容
List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);获取候选配置
SpringFactoriesLoader.loadFactoryNames()
扫描所有jar包类路径下 "META-INF/spring.factories"
把扫描的这些文件内容包装成Properties对象
从properties中获取到EnableAutoConfiguration.class  类名对应的值,然后把他们添加在容器中。
将类路径下  "META-INF/spring.factories"  里面配置的所有EnableAutoConfiguration.class 的值加入容器中
用他们来做自动配置
3)每一个自动配置类自动配置
以HttpEncodingAutoConfiguration为例解释自动配置原理
@Configuration(    //配置
    proxyBeanMethods = false
)
@EnableConfigurationProperties({HttpProperties.class})//启动,加入到ioc容器中
@ConditionalOnWebApplication(  //底层@condition注解
    type = Type.SERVLET
)
@ConditionalOnClass({CharacterEncodingFilter.class})
@ConditionalOnProperty( 
    prefix = "spring.http.encoding",
    value = {"enabled"},
    matchIfMissing = true
)
public class HttpEncodingAutoConfiguration {s({HttpProperties.class})//启动
@ConditionalOnWebApplication(
    type = Type.SERVLET
) 
@ConditionalOnClass({CharacterEncodingFilter.class})
@ConditionalOnProperty(
    prefix = "spring.http.encoding",
    value = {"enabled"},
    matchIfMissing = true
)
public class HttpEncodingAutoConfiguration {
    private final Encoding properties;//他已经和springBoot的配置文件映射了
    //只有一个有参构造器,从容器中拿
    public HttpEncodingAutoConfiguration(HttpProperties properties) {
    this.properties = properties.getEncoding();
}
根据当前不同的条件判断,决定这个配置类是否生效?
一旦生效,这个配置类添加组件到容器中,这些组件属性是从对应的properties 类中获取的,这些类又是和配置文件绑定的。
@Bean  //给容器添加一个组件,值需要从properties中获取
@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;
}

5)、所有在配置文件中能配置的属性都是在xxxProperties类中封装着

@ConfigurationProperties( //从配置文件中获取指定值和Bean的属性进行绑定
    prefix = "spring.http"
)
public class HttpProperties {
精髓:
  1)SpringBoot启动时会加载大量的自动配置类
  2)默认写好的配置类,是否满足要求
  3)没有配置的,需要自己配置
  4)给容器中自动添加组件时候,会从properties 中获取某些属性,可以在配置文件中指定这些属性
  xxxxxAutoConfiguration:自动配置类;给容器添加组件
  xxxproperties :封装配置文件中相关属性;
@conditional派生注解(spring注解版原生的@conditional作用)
作用:必须是@conditional指定的条件成立,才给容器中添加组件,配置里面的所有内容才生效
自动配置类在一定的条件下才生效
通过启用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)
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)

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值