spring boot自动装配原理

news/2024/10/17 17:23:43/

在启动类上的SpringBootApplication复合注解中@EnableAutoConfiguration注解是自动装配的主要配置注解。

@EnableAutoConfiguration里面有两个主要注解:

  • @AutoConfigurationPackage    默认扫描启动类同包及其子包的类的bean进行注册
  • @Import({AutoConfigurationImportSelector.class})  自动装配的主要实现

@Import注解的使用参考https://blog.csdn.net/weixin_45453628/article/details/124234317

这里自动装配使用的是 @Import一个类 该类实现了ImportSelector 重写selectImports方法该方法返回了String[]数组的对象,数组里面的类都会注入到spring容器当中

 

进入到AutoConfigurationImportSelector类中发现它实现了DeferredImportSelector接口,DeferredImportSelector又继承了ImportSelector接口,所以AutoConfigurationImportSelector间接实现了ImportSelector。

DeferredImportSelector 是延迟导入

 在AutoConfigurationImportSelector搜索selectImports实现方法时会发现有两个,其中一个是实现ImportSelector的selectImports方法,还有一个是实现DeferredImportSelector的selectImports方法。

那他会调用哪个?

经过我的调试发现 默认调用public Iterable<Entry> selectImports(),也就是实现DeferredImportSelector的

DeferredImportSelector#Group的方法调用循序是process  -> selectImports

 process方法主要是通过getAutoConfigurationEntry方法获取autoConfigurationEntry自动装配的实体

getAutoConfigurationEntry里调用getCandidateConfigurations方法,在调用SpringFactoriesLoader.loadFactoryNames()

loadFactoryNames再调用 loadSpringFactories来加载spring.factories文件

classLoader.getResources("META-INF/spring.factories");

 

private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) {Enumeration urls = classLoader.getResources("META-INF/spring.factories");while(urls.hasMoreElements()) {URL url = (URL)urls.nextElement();UrlResource resource = new UrlResource(url);Properties properties = PropertiesLoaderUtils.loadProperties(resource);Iterator var6 = properties.entrySet().iterator();while(var6.hasNext()) {Entry<?, ?> entry = (Entry)var6.next();String factoryTypeName = ((String)entry.getKey()).trim();String[] factoryImplementationNames = StringUtils.commaDelimitedListToStringArray((String)entry.getValue());}}}}

loadSpringFactories方法中会遍历所有包的类路径下的META-INF/spring.factories找出自动配置的配置类的全路径名

获取的全部全路径名要经过条件筛选getCandidateConfigurations获得可以用的全路径名,将他们封装成AutoConfigurationEntry

在selectImports还要再排除一遍和排序

 

 最后返回经过一系列操作注册bean


http://www.ppmy.cn/news/581140.html

相关文章

Git进阶系列 | 8. 用Reflog恢复丢失的提交

Git是最流行的代码版本控制系统&#xff0c;这一系列文章介绍了一些Git的高阶使用方式&#xff0c;从而帮助我们可以更好的利用Git的能力。本系列一共8篇文章&#xff0c;这是最后一篇。原文&#xff1a;Using the Reflog to Restore Lost Commits[1] “Reflog”是Git不太为人所…

网页版“植物大战僵尸”

http://www.lonelystar.org/pvz/Index.asp

网址合集

网址合集 软件下载网站 懒得勤快 脑袋瓜子 软件学堂 python 官网 posted on 2019-08-25 19:44 TADF 阅读( ...) 评论( ...) 编辑 收藏 转载于:https://www.cnblogs.com/huchenggui/p/11409032.html

css绘制的彩虹网页样式

下载地址 css绘制的彩虹网页样式 dd:

彩虹岛……

姐姐叫我玩额……法师是帅呀…… 个张是发魔法前&#xff0c;发魔法的时候截图没截好。

彩虹天堂

彩虹天堂&#xff01;

大天使之剑游戏超详细图文架设教程

想体验传奇游戏霸服的快乐吗&#xff1f;想体验满级VIP的尊贵吗&#xff1f;想体验一刀99999的爽快吗&#xff1f;各种极品装备、翅膀、宠物通通给你&#xff0c;就在大天使之剑&#xff01; 本文讲解大天使之剑H5游戏的架设教程&#xff0c;想研究H5游戏如何实现&#xff0c;体…

web前端大一实训 HTML+CSS+JavaScript王者荣耀(60页) web课程设计网页规划与设计 HTML期末大作业 HTML网页设计结课作业

&#x1f389;精彩专栏推荐&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb; ✍️ 作者简介: 一个热爱把逻辑思维转变为代码的技术博主 &#x1f482; 作者主页: 【主页——&#x1f680;获取更多优质源码】 &#x1f393; web前端期末大作业…