SpringBoot(2.4.0)自动配置原理(源码)

一、从@SpringBootApplication讲起

  1. 源码
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
    .....
}

出现3个重要注解:

  • @SpringBootConfiguration
  • @EnableAutoConfiguration
  • @ComponentScan

二、@SpringBootConfiguration(标注当前类是配置类)

为什么@SpringBootApplication注解里没有包含@Configuration,实际上是在@SpringBootConfiguration里面。

  1. 源码:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
    @AliasFor(
        annotation = Configuration.class
    )
    boolean proxyBeanMethods() default true;
}

@SpringBootConfiguration继承自@Configuration,二者功能也一致,标注当前类是配置类,
并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到spring容器中,并且实例名就是方法名。实际上如果我们使用如下的Springboot启动类,整个SpringBoot应用依然可以与之前的启动类功能对等。

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

三、@EnableAutoConfiguration(根据类路径下的jar包依赖为当前项目进行自动配置)

  1. 若看过Spring框架的源码,是否记得spring框架提供的各种名字为@Enable开头的Annotation定义?
    比如@EnableScheduling,@EnableCaching,@EnableMBeanExport等@EnableAutoConfiguration的理念和"做事方式"其实一脉相承,借助@Import的支持,收集和注册特定场景相关的bean定义:
  • @EnableScheduling是通过@Import将Spring调度框架相关的bean定义都加载到Ioc容器中。
  • @EnableMBeanExport是通过@Import将JMX相关的bean定义加载到Ioc容器
  • @EnableAutoConfiguration也是借助@Import的帮助,将所有符合自动配置条件的bean定义加载到Ioc容器,仅此而已
  1. @EnableAutoConfiguration源码
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
    String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";

    Class<?>[] exclude() default {};

    String[] excludeName() default {};
}

@AutoConfigurationPackage,@Import({AutoConfigurationImportSelector.class})又得好好研究

  1. @AutoConfigurationPackage(自动配置包?指定默认包规则)
  • 它指定了默认的包规则
  • 源码:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
//利用Registrar给容器中导入一系列组件
//将指定的一个包下的所有组件导入进来,也即是MainApplication 所在包下。
public @interface AutoConfigurationPackage {
    String[] basePackages() default {};
    Class<?>[] basePackageClasses() default {};
}

那就要看看Registrar这个类了:
里面这段代码,就是批量注册:

 static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {
        Registrar() {
        }
        public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
        //这里就是利用元注解,获取到元注解所作用在的类(其实就是main方法,如下图)的包名
        //然后,将其包名下的所有使用了@Configuration注解的类一并加载进来
            AutoConfigurationPackages.register(registry, (String[])(new AutoConfigurationPackages.PackageImports(metadata)).getPackageNames().toArray(new String[0]));
        }
        public Set<Object> determineImports(AnnotationMetadata metadata) {
            return Collections.singleton(new AutoConfigurationPackages.PackageImports(metadata));
        }
    }

在这里插入图片描述
在这里插入图片描述

  1. @Import({AutoConfigurationImportSelector.class})
  • AutoConfigurationImportSelector.class有一个 selectImports方法
  //该方法就是规定要导入哪些包
  public String[] selectImports(AnnotationMetadata annotationMetadata) {
        if (!this.isEnabled(annotationMetadata)) {
            return NO_IMPORTS;
        } else {
            AutoConfigurationImportSelector.AutoConfigurationEntry autoConfigurationEntry = this.getAutoConfigurationEntry(annotationMetadata);
            return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
        }
    }

可以看到,核心就是getAutoConfigurationEntry方法,进去看一下:

在这里插入图片描述
我们又发现,configurations的获得是依赖getCandidateConfigurations方法的,进去看看,就可以看到自动配置的幕后英雄:SpringFactoriesLoader
在这里插入图片描述

自动配置的幕后英雄:SpringFactoriesLoader
在这里插入图片描述

SpringFactoriesLoader的主要功能就是从指定的配置文件META/spring.factories加载配置,spring.factories是一个典型的java properties文件,配置格式为Key-Value形式,只不过Key和Value都是Java类型的完整类名。
进入loadFactoryNames()方法,就发现loadFactoryNames()读取了ClassPath下面的 META-INF/spring.factories 文件。
在这里插入图片描述

@EnableAutoConfiguration的场景中,它更多是提供一种配置查找的功能支持,即根据@EnableAutoConfiguration的完整类名org.springframework.boot.autoconfigure.EnableAutoConfiguration作为查找的key,获取对应的一组@Configuration类

在这里插入图片描述

注意,从springboot2.7.0之后,已经修改了加载方式,springboot3之后,也更换了

  • 总结:
  • 1、利用getAutoConfigurationEntry(annotationMetadata);给容器中批量导入一些组件
  • 2、调用List configurations = getCandidateConfigurations(annotationMetadata, attributes)获取到所有需要导入到容器中的配置类
  • 3、利用工厂加载 Map<String, List> loadSpringFactories(@Nullable ClassLoader classLoader);得到所有的组件
  • 4、从META-INF/spring.factories位置来加载一个文件。默认扫描我们当前系统里面所有META-INF/spring.factories位置的文件
    spring-boot-autoconfigure-2.4.0.RELEASE.jar包里面也有META-INF/spring.factories
  • 在spring-boot-autoconfigure-2.4.0.RELEASE.jar包里的META-INF/spring.factories文件,写死了spring-boot一启动就要给容器中加载的所有配置类,但是你我肯定都知道,不会一下子加载那么多,那么就是另一个内容了:按需加载

  • 开启按需加载
    SpringBoot自动配置是需要满足相应的条件才会自动配置,因此SpringBoot的自动配置大量应用了条件注解ConditionalOnXXX。如下图:
    在这里插入图片描述
    上面这些注解会被使用在各个组件上面,然后会根据环境决定是否导入各个组件,实现按需导入,源码分析

四、@ComponentScan (自动扫描并加载符合条件的组件或者bean定义)

@ComponentScan的功能其实就是自动扫描并加载符合条件的组件或bean定义,最终将这些bean定义加载到容器中。我们可以通过basePackages等属性指定@ComponentScan自动扫描的范围,如果不指定,则默认Spring框架实现从声明@ComponentScan所在类的package进行扫描,默认情况下是不指定的,所以SpringBoot的启动类最好放在root package下。

五、SpringBootApplication注解中4个方法

@SpringBootApplication不仅包括上面的三个重要注解,还包含有4个方法:
在这里插入图片描述

  • Class<?>[] exclude() default {}; 根据Class来排除特定的类加入Spring容器,传入参数是class类型
  • String[] excludeName() default {}; 根据Class name排除特定的类加入spring容器,传入参数是class的全类名字符串数组
  • String[] scanBasePackages() default {};指定扫描包,参数是包名的字符串数组
  • Class<?>[] scanBasePackageClasses() default {};指定扫描包,参数是Class类型数组

六、修改组件默认配置

  1. SpringBoot默认会在底层配好所有的组件。但是如果用户自己配置了以用户的优先

    平时我们都是直接再写一个bean注入到容器的
    在这里插入图片描述

其实直接修改配置文件就好了。
原理如下:源码以HttpEncodingAutoConfiguration为例:

@Configuration(
    proxyBeanMethods = false
)
@EnableConfigurationProperties({ServerProperties.class})
@ConditionalOnWebApplication(
    type = Type.SERVLET
)
@ConditionalOnClass({CharacterEncodingFilter.class})
@ConditionalOnProperty(
    prefix = "server.servlet.encoding",
    value = {"enabled"},
    matchIfMissing = true
)
public class HttpEncodingAutoConfiguration {
    private final Encoding properties;

    public HttpEncodingAutoConfiguration(ServerProperties properties) {
        this.properties = properties.getServlet().getEncoding();
    }
    
    @Bean
    @ConditionalOnMissingBean
    public CharacterEncodingFilter characterEncodingFilter() {
        CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
        filter.setEncoding(this.properties.getCharset().name());
        filter.setForceRequestEncoding(this.properties.shouldForce(org.springframework.boot.web.servlet.server.Encoding.Type.REQUEST));
        filter.setForceResponseEncoding(this.properties.shouldForce(org.springframework.boot.web.servlet.server.Encoding.Type.RESPONSE));
        return filter;
    }
 }

@EnableConfigurationProperties({ServerProperties.class})可知,该类已经与配置文件绑定了,所以,如果你改变filter.setEncoding(this.properties.getCharset().name());只需要溯源改变server.servlet.encoding.charset的值就可以了

  1. 总结:

• SpringBoot先加载所有的自动配置类 xxxxxAutoConfiguration
• 每个自动配置类按照条件进行生效,默认都会绑定配置文件指定的值。xxxxProperties里面拿。 xxxProperties和配置文件进行了绑定
• 生效的配置类就会给容器中装配很多组件
• 只要容器中有这些组件,相当于这些功能就有了
• 定制化配置
• 用户直接自己@Bean替换底层的组件
• 用户去看这个组件是获取的配置文件什么值就去修改。
xxxxxAutoConfiguration —> 加载组件 —> xxxxProperties里面拿值 ----> application.properties

请添加图片描述

参考文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值