SpringBoot自动配置原理(每一步都有说明哦)

SpringBoot自动配置原理

在SpringBoot启动主配置类时,@SpringBootApplication 注解发挥作用进行自动配置。

在这里插入图片描述
@SpringBootApplication 注解中,比较重要的有如下两个注解,实际发挥自动配置作用作用的是 @EnableAutoConfiguration 注解
在这里插入图片描述
而在@EnableAutoConfiguration 中,@Import导入了一个自动配置选择器类 AutoConfigurationImportSelector。
在这里插入图片描述
进入这个自动配置选择器类,可以发现在AutoConfigurationImportSelector 类中有一个selectImports 方法,方法中调用 getAutoConfigurationEntry() 获取配置信息条目。
在这里插入图片描述
这个方法里面的 调用getCandidateConfigurations() 获取候选的配置。
在这里插入图片描述
进入getCandidateConfigurations()方法,就可以发现明显的痕迹了:

protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
        Assert.notEmpty(configurations, 
        "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
        return configurations;
    }

在这里插入图片描述
进入SpringFactoriesLoader下的静态方法loadFactoryNames()
最明显的部分来了:
在这里插入图片描述
这个META-INF/spring.factories便是包含自动配置信息的文件在如下的依赖包里面
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200907211122611.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwODQyNzU2,size_16,color_FFFFFF,t_70#pic_center
spring.factories文件里面包含了SpringBoot所需的所有配置类的全限定类名:
在这里插入图片描述
EnableAutoConfiguration 属性下的值所代表的配置自动都加入到容器中,不再需要我们像刚学习 Spring 时,
在 xml 配置文件中一个个去配置。极大地提高了开发效率。–这也是SpringBoot的精髓之一。

这里我们可以进去比较熟悉的DataSource相关的AutoConfiguration
在这里插入图片描述
在这里插入图片描述
进入DataSourceProperties类瞧一瞧,可以发现又一个我们非常熟悉的注解:
在这里插入图片描述
在这里插入图片描述
这也是SpringBoot配置的精髓所在。

下面是我的手稿:字迹潦草见谅
在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值