springboot自动配置原理

本文深入探讨了SpringBoot的自动配置原理,从配置文件的编写规则出发,详细解析了@EnableAutoConfiguration注解如何引导自动配置类的加载。通过分析spring.factories文件,展示了如何将自动配置类加入到容器中,并以HttpEncodingAutoConfiguration为例,说明了自动配置类如何根据条件生效并注入组件。此外,还强调了SpringBoot的核心思想,即利用默认的自动配置减少手动配置工作,用户只需在配置文件中指定所需属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

自动配置原理:

  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)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值