09、SpringBoot 源码分析 - 自动配置深度分析二

refresh和自动配置大致流程

在这里插入图片描述

DeferredImportSelectorHandler的process处理

前面说到这里:
在这里插入图片描述
其实第一次进去的时候是放进集合里的:
在这里插入图片描述
因为这个deferredImportSelectors成员变量初始化的时候就创建了:


private static class DeferredImportSelectorHolder {

  @Nullable
  private List<DeferredImportSelectorHolder> deferredImportSelectors = new ArrayList<>();
}

放进去之后就执行其他,最后都解析完到这里,就是前面说的DeferredImportSelector类型的会等其他解析完,最后处理:
在这里插入图片描述
这里就是处理逻辑,其实就是创建DeferredImportSelectorGroupingHandler,然后把前面放进去的DeferredImportSelectorHolder都拿出来,都注册到里面去,就是前面讲的分组注册,最后处理,处理完之后还要创建deferredImportSelectors,这里就是避免迭代器在迭代的时候还要新的元素添加进来,会出问题。

public void process() {

			List<DeferredImportSelectorHolder> deferredImports = this.deferredImportSelectors;
			this.deferredImportSelectors = null;//设置为空,如果有新的就会直接注册处理
			try {
   
     
				if (deferredImports != null) {
   
     //如果有存在DeferredImportSelector
					DeferredImportSelectorGroupingHandler handler = new DeferredImportSelectorGroupingHandler();
					deferredImports.sort(DEFERRED_IMPORT_COMPARATOR);
					deferredImports.forEach(handler::register);//迭代注册到DeferredImportSelectorGroupingHandler
					handler.processGroupImports();//处理
				}
			}
			finally {
   
     
				this.deferredImportSelectors = new ArrayList<>();//处理完了再创建
			}
		}

processGroupImports处理

为什么要这么分组,这里就体现出来了,看他怎么处理的,遍历所有的ImportSelector组,获取所有import的实体Entry,其实这个时候已经把要自动配置的类找到了,然后迭代去进行递归processImports处理。

public void processGroupImports() {
   
     
			for (DeferredImportSelectorGrouping grouping : this.groupings.values()) {
   
     
				Predicate<String> exclusionFilter = grouping.getCandidateFilter();
				grouping.getImports().forEach(entry -> {
   
     //每一个自动配置类都要进行processImports处理
					ConfigurationClass configurationClass = this.configurationClasses.get(entry.getMetadata());
					try {
   
     
						processImports(configurationClass, asSourceClass(configurationClass, exclusionFilter),
								Collections.singleton(asSourceClass(entry.getImportClassName(), exclusionFilter)),
								exclusionFilter, false);
					}
					catch (BeanDefinitionStoreException ex) {
   
     
						throw ex;
					}
					catch (Throwable ex) {
   
     
						throw new BeanDefinitionStoreException(
								"Failed to process import candidates for configuration class [" +
										configurationClass.getMetadata().getClassName() + "]", ex);
					}
				});
			}
		}

DeferredImportSelectorGrouping的处理getImports

这里要开始处理了,其实是要组group对象来处理对应的DeferredImportSelector集合,最后在返回Iterable<Group.Entry>实体迭代器,就是已经把自动配置的类名集合封装成迭代器了。

		public Iterable<Group.Entry> getImports() {
   
     
			for (DeferredImportSelectorHolder deferredImport : this.deferredImports) {
   
     
				this.group.process(deferredImport.getConfigurationClass().getMetadata(),
						deferredImport.getImportSelector());
			}
			return this.group.selectImports();
		}

AutoConfigurationGroup的处理getImports

其实这里关键的是AutoConfigurationImportSelectorgetAutoConfigurationEntry获取AutoConfigurationEntry,这里面封装着自动配置类名字集合,后面只是放到对应的映射里,方便用。

	@Override
		public void process(AnnotationMetadata annotationMetadata, DeferredImportSelector deferredImportSelector) {
   
     
			...
			AutoConfigurationEntry autoConfigurationEntry = ((AutoConfigurationImportSelector) deferredImportSelector)
					.getAutoConfigurationEntry(getAutoConfigurationMetadata(), annotationMetadata);//获取自动装配实体
			this.autoConfigurationEntries.add(autoConfigurationEntry);
			for (String importClassName : autoConfigurationEntry.getConfigurations()) {
   
     
				this.entries.putIfAbsent(importClassName, annotationMetadata);
			}//建立配置类名和注解属性的映射
		}

在这里插入图片描述
接下去我们主要要看AutoConfigurationImportSelector这个类啦。

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一名技术极客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值