2023-09-16
原文作者:王伟王胖胖 原文地址: https://blog.csdn.net/wangwei19871103/article/details/105012594

执行剩下的BeanFactoryPostProcessor的后置处理器

首先先按PriorityOrderedOrdered和一般的BeanFactoryPostProcessor没处理过的处理器分成三个集合,然后按顺序分别进行处理,这里面除了我们自定义的,就是内部的EventListenerMethodProcessor了。

    String[] postProcessorNames =
    				beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
    		//三种优先级区分
    		// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
    		// Ordered, and the rest.
    		List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
    		List<String> orderedPostProcessorNames = new ArrayList<>();
    		List<String> nonOrderedPostProcessorNames = new ArrayList<>();
    		for (String ppName : postProcessorNames) {
    			if (processedBeans.contains(ppName)) {//处理过的就不处理
    				// skip - already processed in first phase above
    			}
    			else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
    				priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
    			}
    			else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
    				orderedPostProcessorNames.add(ppName);
    			}
    			else {
    				nonOrderedPostProcessorNames.add(ppName);
    			}
    		}
    		//先处理priorityOrderedPostProcessors排序和回调
    		// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
    		sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
    		invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
    		//再处理orderedPostProcessors排序和回调
    		// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
    		List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
    		for (String postProcessorName : orderedPostProcessorNames) {
    			orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
    		}
    		sortPostProcessors(orderedPostProcessors, beanFactory);
    		invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
    		//最后是剩下的
    		// Finally, invoke all other BeanFactoryPostProcessors.
    		List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
    		for (String postProcessorName : nonOrderedPostProcessorNames) {
    			nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
    		}
    		invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);

EventListenerMethodProcessor的postProcessBeanFactory

bean名字中查找并创建EventListenerFactory类型的对象返回,然后保存到eventListenerFactories 中,其实就是创建事件监听工厂,后面可以创建事件监听器。

    	@Override
    	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    		this.beanFactory = beanFactory;
    
    		Map<String, EventListenerFactory> beans = beanFactory.getBeansOfType(EventListenerFactory.class, false, false);
    		List<EventListenerFactory> factories = new ArrayList<>(beans.values());
    		AnnotationAwareOrderComparator.sort(factories);
    		this.eventListenerFactories = factories;
    	}

DefaultListableBeanFactory的clearMetadataCache进行元数据缓存清除

主要是判断是否已经创建了,没创建的话BeanDefinitionstale=true,创建的就不处理了。最后把allBeanNamesByTypesingletonBeanNamesByType的缓存清除。

    	@Override
    	public void clearMetadataCache() {
    		super.clearMetadataCache();
    		clearByTypeCache();
    	}
    
    	public void clearMetadataCache() {
    		this.mergedBeanDefinitions.forEach((beanName, bd) -> {
    			if (!isBeanEligibleForMetadataCaching(beanName)) {
    				bd.stale = true;
    			}
    		});
    	}
    	private void clearByTypeCache() {
    		this.allBeanNamesByType.clear();
    		this.singletonBeanNamesByType.clear();
    	}

AbstractApplicationContext的invokeBeanFactoryPostProcessors

最后还要判断下是否要加入LoadTimeWeaverAwareProcessor处理器,这个暂时不重要。

    	protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
    		PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
    
    		// Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
    		// (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
    		if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
    			beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
    			beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
    		}
    	}

总结

至此调用BeanFactory的后置处理器全部完成了,我们来总结下,除去自定义的处理器,主要还是ConfigurationClassPostProcessor在处理,解析配置类,加载bean定义,配置类可能会进行CGLIB动态代理增强,最后把剩下的BeanFactoryPostProcessor处理器处理完:

202309162311558101.png
后面就要进行BeanPostProcessor后置处理器注册啦,这个就是用来参与bean创建的过程啦。

registerBeanPostProcessors注册bean后置处理器

其实这个里面跟invokeBeanFactoryPostProcessors差不多,都是有优先级别处理的,然后会进行排序注册,最后就是这样的:

202309162311562862.png

好了,今天就到这里了,希望对学习理解有帮助,大神看见勿喷,仅为自己的学习理解,能力有限,请多包涵。

阅读全文