• SpringBoot2.0---------------5、SpringBoot自动配置原理


    1.引导加载自动配置类

    @SpringBootApplication注解是Springboot项目的核心注解,用于告诉SpringBoot这是一个SpringBoot应用;该注解可看作由三个注解组成:@SpringBootConfiguratin、@EnableAutoConfiguration、@ComponentScan,@SpringBootApplication注解程序截取如下:

    @SpringBootConfiguration
    @EnableAutoConfiguration
    @ComponentScan(
        excludeFilters = {@Filter(
        type = FilterType.CUSTOM,
        classes = {TypeExcludeFilter.class}
    ), @Filter(
        type = FilterType.CUSTOM,
        classes = {AutoConfigurationExcludeFilter.class}
    )}
    )
    public @interface SpringBootApplication {}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    1)第一个合成注解:@SpringBootConfiguration注解由@Configuration和一些其他注解共同构成
    @Configuration注解:表示当前是一个配置类,即@SpringBootApplication注解也表示当前的类也可以作为一个配置类。

    2)第二个合成注解:@ComponentScan注解功能为包扫描,指定扫描路径,即将那些路径中的组件添加到容器中

    3)第三个合成注解:@EnableAutoConfiguration注解,该注解本身也是一个合成注解@AutoConfigurationPackage+@Import。其主要合成的注解如下:

    @AutoConfigurationPackage
    @Import({AutoConfigurationImportSelector.class})
    public @interface EnableAutoConfiguration {
    
    • 1
    • 2
    • 3

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

  • 相关阅读:
    基于STM32的串口通信详解
    函数声明、定义
    MySQL 优化,查询的原理,索引的使用,如何优化查询
    SQL小技巧之替换、统计表字段存储数据字节大小。
    『Nacos』 入门教程
    鸿蒙ArkTS声明式开发:跨平台支持列表【Z序控制】 通用属性
    c++读取文件操作和写入文件
    处方识别 易语言代码
    mysql笔记
    【优化算法】一种新的数据聚类启发式优化方法——黑洞算法(基于Matlab代码实现)
  • 原文地址:https://blog.csdn.net/lyy_sss/article/details/126129996