invokeBeanFactoryPostProcessors浅析

invokeBeanFactoryPostProcessors方法干了哪些事。
首先对 获取的beanFactoryPostProcessors List集合进行遍历看是否实现了BeanDefinitionRegistryPostProcessors接口,实现了则调用postProcessBeanDefinitionRegistry方法逻辑,然后把对应的beanFactoryPostProcessor添加到registryProcessors List集合,没有实现BeanDefinitionRegistryPostProcessors接口的添加到regularPostProcessors List集合中。

for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
	if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
		BeanDefinitionRegistryPostProcessor registryProcessor =
				(BeanDefinitionRegistryPostProcessor) postProcessor;
		registryProcessor.postProcessBeanDefinitionRegistry(registry);
		registryProcessors.add(registryProcessor);
	}
	else {
		regularPostProcessors.add(postProcessor);
	}
}

调用实现了PrioritOrderdered的BeanDefinitionRegistryPostProcessors

String[] postProcessorNames =
		beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
	if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
		currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
		processedBeans.add(ppName);
	}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
currentRegistryProcessors.clear();
  • 获取beanFactory(DefaultListableBeanFactory)中beanDefinitionNames属性中实现了BeanDefinitionRegistryPostProcessors的BeanDefinition的beanDefinition名称集合
  • 遍历上面的beanDefinition名称集合获取实现了PriorityOrdered的beanDefinition名称集合,然后对这些beanDefinition进行bean的创建和bean的初始化
  • 把创建好的bean放到beanFactory的singletonObjects(单例)和registeredSingletons中, 然后把bean添加currentRegistryProcessors List集合中,对List集合进行排序
protected void addSingleton(String beanName, Object singletonObject) {
	synchronized (this.singletonObjects) {
		this.singletonObjects.put(beanName, singletonObject);
		this.singletonFactories.remove(beanName);
		this.earlySingletonObjects.remove(beanName);
		this.registeredSingletons.add(beanName);
	}
}
  • 把排序好的bean集合添加到registryProcessors
  • 执行invokeBeanDefinitionRegistryPostProcessors方法对currentRegistryProcessors中的bean分别调用对BeanDefinitionRegistryPostProcessors接口postProcessBeanDefinitionRegistry方法的实现的逻辑。例如这里就包括ConfigurationClassPostProcessor对标注了@Configuration注解的类的解析获取bean的定义把bean的定义注册到beanFactory相关的属性中
  • 执行完清空currentRegistryProcessors

调用实现了Ordered的BeanDefinitionRegistryPostProcessors

这部分和上面调用实现了PrioritOrderdered的BeanDefinitionRegistryPostProcessors的逻辑基本上类似,只是获取的不再是PrioritOrderdered而是Ordered,但如果没有匹配的类则不执行。

调用其他的BeanDefinitionRegistryPostProcessors

这部分是对实现了PrioritOrderdered和Ordered以外的BeanDefinitionRegistryPostProcessors的处理。他的逻辑和上面的逻辑基本一致,不同的的处理都是在获取符合要求的bean后,对bean本身实现了BeanDefinitionRegistryPostProcessors接口postProcessBeanDefinitionRegistry方法内部的处理逻辑,完成了什么工作。

调用invokeBeanFactoryPostProcessors

  • 对添加到registryProcessors的bean调用实现了BeanDefinitionRegistryPostProcessors的postProcessBeanFactory方法的逻辑。例如:ConfigurationClassPostProcessor则对符合要求的bean进行类增强,把增强后的class设置到bean的定义beanClass属性中
  • regularPostProcessors集合中的对象调用实现了BeanFactoryPostProcessor接口的postProcessBeanFactory方法的逻辑

处理实现了BeanFactoryPostProcessor的bean

对实现了PrioritOrderdered、Ordered、以及其他的bean,进行的创建和初始化,然后执行实现了BeanFactoryPostProcessor接口的postProcessBeanFactory方法逻辑。

清除MetadataCache

把不在beanFactory#alreadyCreated集合中bean,把bean的定义的state属性设置为true
清空beanFactory#mergedBeanDefinitionHolders清空
清空beanFactory#allBeanNamesByType
清空beanFactory#singletonBeanNamesByType

上面只是对invokeBeanFactoryPostProcessors方法简单的记录,真正复杂的逻辑是对实现BeanDefinitionRegistryPostProcessors和BeanFactoryPostProcessor接口的bean的获取以及bean的创建和初始化,到后面对bean实现BeanDefinitionRegistryPostProcessors、BeanFactoryPostProcessor接口方法的调用。在这调用的期间就包括对一些Spring注解的分析,对类的加载以及把bean的定义注册到beanFactory的beanDefinitionMap中等等,还有很多细节,还需细细的梳理。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值