Spring Boot自动配置原理

  • @AutoConfigurationPackage
    • @Import


      @SpringBootApplication

      https://www.cuizb.top/myblog/static/image/SpringBoot_autoConfig_1.jpg

      @SpringBootApplication注解由@SpringBootConfiguration、@ComponentScan、@EnableAutoConfiguration三个注解组成

      https://www.cuizb.top/myblog/static/image/SpringBoot_autoConfig_2.jpg

      • @SpringBootConfiguration:我们点进去以后可以发现底层是Configuration注解,说白了就是支持JavaConfig的方式来进行配置(使用Configuration配置类等同于XML文件)。
      • @EnableAutoConfiguration:开启自动配置功能
      • @ComponentScan扫描注解,默认是扫描当前类下的package。将@Controller/@Service/@Component/@Repository等注解加载到IOC容器中。

      所以SpringBoot入口类可以写成:

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

      @EnableAutoConfiguration

      我们知道SpringBoot可以帮我们减少很多的配置,也肯定听过“约定大于配置”这么一句话,那SpringBoot是怎么做的呢?其实靠的就是@EnableAutoConfiguration注解。

      简单来说,这个注解可以帮助我们自动载入应用程序所需要的所有默认配置

      https://www.cuizb.top/myblog/static/image/SpringBoot_autoConfig_3.jpg

      • @AutoConfigurationPackage:自动配置包
      • @Import:给IOC容器导入组件

      @AutoConfigurationPackage

      https://www.cuizb.top/myblog/static/image/SpringBoot_autoConfig_4.jpg

      图中所示依靠@Import注解。点进去看重要的代码是:

      @Override
      public void registerBeanDefinitions(AnnotationMetadata metadata,
              BeanDefinitionRegistry registry) {
          register(registry, new PackageImport(metadata).getPackageName());
      }
      

      默认的情况下就是将:主配置类(@SpringBootApplication)的所在包及其子包里边的组件扫描到Spring容器中。

      注意:

      • @Controller/@Service/@Component/@Repository这些注解是由ComponentScan来扫描并加载的。
      • 诸如 @Entity 此类的注解是由@AutoConfigurationPackage扫描并加载

      @Import

      AutoConfigurationImportSelector.class

      https://www.cuizb.top/myblog/static/image/SpringBoot_autoConfig_5.jpg

      https://www.cuizb.top/myblog/static/image/SpringBoot_autoConfig_6.jpg

      此时只看到了SpringFactoriesLoader.loadFactoryNames()方法加载

      https://www.cuizb.top/myblog/static/image/SpringBoot_autoConfig_7.jpg

      此时可以知道:

      • FACTORIES_RESOURCE_LOCATION的值是META-INF/spring.factories
      • Spring启动的时候会扫描所有jar路径下的META-INF/spring.factories,将其文件包装成Properties对象
      • 从Properties对象获取到key值为EnableAutoConfiguration的数据,然后添加到容器里边。

      https://www.cuizb.top/myblog/static/image/SpringBoot_autoConfig_8.jpg

      https://www.cuizb.top/myblog/static/image/SpringBoot_autoConfig_9.jpg

      总结

      @SpringBootApplication等同于下面三个注解:

      • @SpringBootConfiguration
      • @EnableAutoConfiguration
      • @ComponentScan

      其中@EnableAutoConfiguration是关键(启用自动配置)该注解又通过 @Import 注解导入了AutoConfigurationImportSelector类,在该类中加载 META-INF/spring.factories 的配置信息。然后筛选出以 EnableAutoConfiguration 为 key 的数据,加载到 IOC 容器中,实现自动配置功能!

      https://www.cuizb.top/myblog/static/image/SpringBoot_autoConfig_10.jpg

      https://www.cuizb.top/myblog/static/image/SpringBoot_autoConfig_11.jpg

      在这里插入图片描述
      JVM内存泄漏和内存溢出的原因
      JVM常用监控工具解释以及使用
      Redis 常见面试题(一)
      ClickHouse之MaterializeMySQL引擎(十)
      三种实现分布式锁的实现与区别
      线程池的理解以及使用

      号外!号外!

      最近面试BAT,整理一份面试资料,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。想获取吗?如果你想提升自己,并且想和优秀的人一起进步,感兴趣的朋友,可以在扫码关注下方公众号。资料在公众号里静静的躺着呢。。。

      • 喜欢就收藏
      • 认同就点赞
      • 支持就关注
      • 疑问就评论

      一键四连,你的offer也四连

      ————————————————————————————————

      本文作者:Java技术债务
      原文链接:https://www.cuizb.top/myblog/article/1644763339
      版权声明: 本博客所有文章除特别声明外,均采用 CC BY 3.0 CN协议进行许可。转载请署名作者且注明文章出处。

      posted @   Java技术债务  阅读(38)  评论(0编辑  收藏  举报
      相关博文:
      阅读排行:
      · 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
      · Manus爆火,是硬核还是营销?
      · 终于写完轮子一部分:tcp代理 了,记录一下
      · 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
      · 单元测试从入门到精通
      点击右上角即可分享
      微信分享提示