03 BeanFactoryPostProcessor的注册与执行

2022-10-07 12:19:35

由于md文档部分语法不兼容,建议访问作者网站查阅文章:wlizhi.cc

spring源码系列文章,示例代码的中文注释,均是 copy 自https://gitee.com/wlizhi/spring-framework

链接中源码是作者从 github 下载,并以自身理解对核心流程及主要节点做了详细的中文注释。


1 BeanFactoryPostProcessor

BeanFactoryPostProcessor中只有一个方法postProcessBeanFactory,实现了这个接口的类,会优先于其他常规的类实例化,并调用postProcessBeanFactory方法。

@FunctionalInterfacepublicinterfaceBeanFactoryPostProcessor{voidpostProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)throws BeansException;}

2 BeanDefinitionRegistryPostProcessor

BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子接口,同样的,它会优先与其他常规的类,提前实例化、加入到IOC容器,并会调用postProcessBeanDefinitionRegistry()方法,再调用postProcessBeanFactory()方法。

publicinterfaceBeanDefinitionRegistryPostProcessorextendsBeanFactoryPostProcessor{voidpostProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)throws BeansException;}

3 调用逻辑

  1. 首先搜集从BeanFactory中后去所有BeanDefinitionRegistryPostProcessor类型的beanName。
  2. 筛选出实现了PriorityOrdered接口的(筛选过程中会调用getBean()方法),并对其进行排序,然后进行遍历调用postProcessBeanDefinitionRegistry()。
  3. 筛选出实现了Ordered接口的,同上,进行getBean(),并排序,然后进行遍历调用postProcessBeanDefinitionRegistry()。
  4. 对没有实现PriorityOrdered接口且没有实现Ordered接口的BeanDefinitionRegistryPostProcessor进行排序、调用postProcessBeanDefinitionRegistry()。
  5. 上面的每一步调用postProcessBeanDefinitionRegistry()之前,会将其实力缓存到一个名为registryProcessors的List集合中,在以上步骤执行完,会调用BeanDefinitionRegistryPostProcessor.postProcessBeanFactory()方法
  6. 对没有实现BeanDefinitionRegistryPostProcessor但实现了BeanFactoryPostProcessor的类进行搜集。
  7. 分别筛选出实现了PriorityOrdered接口、Ordered接口、没有实现接口的类,对其进行排序、getBean()操作,调用postProcessBeanFactory()方法。

从源码中,我们可以很清晰的看到执行流程。源码逻辑如下:

finalclassPostProcessorRegistrationDelegate{publicstaticvoidinvokeBeanFactoryPostProcessors(
			ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors){// 这里存储了已经调用过的BeanFactoryPostProcessor。// BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子接口。// 先调用BeanDefinitionRegistryPostProcessor的方法,然后是BeanFactoryPostProcessor// Invoke BeanDefinitionRegistryPostProcessors first, if any.
		Set<String> processedBeans=newHashSet<>();if(beanFactoryinstanceofBeanDefinitionRegistry){
			BeanDefinitionRegistry registry=(BeanDefinitionRegistry) beanFactory;// 这里面装的是实现了BeanFactoryPostProcessor接口的。
			List<BeanFactoryPostProcessor> regularPostProcessors=newArrayList<>();// 这里面装的是实现了BeanDefinitionRegistryPostProcessor接口的。
			List<BeanDefinitionRegistryPostProcessor> registryProcessors=newArrayList<>();// 对参数中传入的beanFactoryPostProcessors优先执行BeanDefinitionRegistryPostProcessor的方法,然后添加到registryProcessors中。for(BeanFactoryPostProcessor postProcessor: beanFactoryPostProcessors){if(postProcessorinstanceofBeanDefinitionRegistryPostProcessor){
					BeanDefinitionRegistryPostProcessor registryProcessor=(BeanDefinitionRegistryPostProcessor) postProcessor;
					registryProcessor.postProcessBeanDefinitionRegistry(registry);
					registryProcessors.add(registryProcessor);}else{
					regularPostProcessors.add(postProcessor);}}// 记录当前注册的BeanDefinitionRegistryPostProcessor// Do not initialize FactoryBeans here: We need to leave all regular beans// uninitialized to let the bean factory post-processors apply to them!// Separate between BeanDefinitionRegistryPostProcessors that implement// PriorityOrdered, Ordered, and the rest.
			List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors=newArrayList<>();// 从beanFactory中的beanDefinitionNames中获取所有的beanName。将实现了PriorityOrdered接口部分,放到处理过的bean容器中。// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
			String[] postProcessorNames=
					beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class,true,false);for(String ppName: postProcessorNames){if(beanFactory.isTypeMatch(ppName, PriorityOrdered.class)){// 这里进行了getBean操作。类的实例化就是通过此方法进行。
					currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
					processedBeans.add(ppName);}}// 对上面搜集到的BeanDefinitionRegistryPostProcessor进行排序、执行BeanDefinitionRegistryPostProcessor的方法sortPostProcessors(currentRegistryProcessors, beanFactory);
			registryProcessors.addAll(currentRegistryProcessors);invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
			currentRegistryProcessors.clear();// 搜集实现了Ordered接口的BeanDefinitionRegistryPostProcessor,执行同上的逻辑。// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
			postProcessorNames= beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class,true,false);for(String ppName: postProcessorNames){if(!processedBeans.contains(ppName)&& beanFactory.isTypeMatch(ppName, Ordered.class)){
					currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
					processedBeans.add(ppName);}}// 排序、执行postProcessBeanDefinitionRegistry()方法sortPostProcessors(currentRegistryProcessors, beanFactory);
			registryProcessors.addAll(currentRegistryProcessors);invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
			currentRegistryProcessors.clear();// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.boolean reiterate=true;while(reiterate){
				reiterate=false;
				postProcessorNames= beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class,true,false);for(String ppName: postProcessorNames){if(!processedBeans.contains(ppName)){
						currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
						processedBeans.add(ppName);
						reiterate=true;}}sortPostProcessors(currentRegistryProcessors, beanFactory);
				registryProcessors.addAll(currentRegistryProcessors);invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
				currentRegistryProcessors.clear();}// 执行postProcessBeanFactory方法。先执行实现了BeanDefinitionRegistryPostProcessor接口的,// 再执行实现了BeanFactoryPostProcessor的。// Now, invoke the postProcessBeanFactory callback of all processors handled so far.invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);}else{// 第一次进来会走到这里,因为参数beanFactoryPostProcessors是空的,实际上这里什么都没做。// Invoke factory processors registered with the context instance.invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);}// 从BeanFactory的BeanDefinitionNames中获取所有的BeanFactoryPostProcessor对应的名称// Do not initialize FactoryBeans here: We need to leave all regular beans// uninitialized to let the bean factory post-processors apply to them!
		String[] postProcessorNames=
				beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class,true,false);// 对实现了PriorityOrdered、Ordered、和未实现接口的类分开处理。// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,// Ordered, and the rest.
		List<BeanFactoryPostProcessor> priorityOrderedPostProcessors=newArrayList<>();
		List<String> orderedPostProcessorNames=newArrayList<>();
		List<String> nonOrderedPostProcessorNames=newArrayList<>();for(String ppName: postProcessorNames){if(processedBeans.contains(ppName)){// skip - already processed in first phase above}elseif(beanFactory.isTypeMatch(ppName, PriorityOrdered.class)){
				priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));}elseif(beanFactory.isTypeMatch(ppName, Ordered.class)){
				orderedPostProcessorNames.add(ppName);}else{
				nonOrderedPostProcessorNames.add(ppName);}}// 首先执行实现了PriorityOrdered的BeanFactoryPostProcessors// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.sortPostProcessors(priorityOrderedPostProcessors, beanFactory);invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);// 执行实现了Ordered接口的BeanFactoryPostProcessors// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
		List<BeanFactoryPostProcessor> orderedPostProcessors=newArrayList<>();for(String postProcessorName: orderedPostProcessorNames){
			orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));}sortPostProcessors(orderedPostProcessors, beanFactory);invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);// 执行未实现接口的BeanFactoryPostProcessor// Finally, invoke all other BeanFactoryPostProcessors.
		List<BeanFactoryPostProcessor> nonOrderedPostProcessors=newArrayList<>();for(String postProcessorName: nonOrderedPostProcessorNames){
			nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));}invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);// Clear cached merged bean definitions since the post-processors might have// modified the original metadata, e.g. replacing placeholders in values...
		beanFactory.clearMetadataCache();}}
  • 作者:暗夜零星
  • 原文链接:https://blog.csdn.net/weixin_42067503/article/details/110684189
    更新时间:2022-10-07 12:19:35