Spring原始碼之BeanFactoryPostProcessor(後置處理器)

程式設計師田同學發表於2022-03-01

Spring原始碼之BeanFactoryPostProcessor(後置處理器)。

有點水平的Spring開發人員想必都知道BeanFactoryPostProcessor也就是常說的後置管理器,這是Spirng生命週期中的一個介面,實現這個介面可以在beanFactory初始化前做一些事。

我們熟知的Spring和Mybatis的結合,正是因為Mybatis實現了BeanFactoryPostProcessor,它的重要性不言而喻,深入理解他對於切入Mybatis原始碼有著深刻的意義。

如下圖是簡單的應用:

image-20220301094331881

還是先貼上refresh()的原始碼

	@Override
	public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			// Prepare this context for refreshing.
			//1、重新整理前的準備
			prepareRefresh();

			// Tell the subclass to refresh the internal bean factory.
			//2、將會初始化 BeanFactory、載入 Bean、註冊 Bean
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.
			//3、設定 BeanFactory 的類載入器,新增幾個 BeanPostProcessor,手動註冊幾個特殊的 bean
			prepareBeanFactory(beanFactory);

			try {
				//4、模板方法
				// Allows post-processing of the bean factory in context subclasses.
				postProcessBeanFactory(beanFactory);

				// Invoke factory processors registered as beans in the context.
				//執行BeanFactory後置處理器
				invokeBeanFactoryPostProcessors(beanFactory);

				// 5、Register bean processors that intercept bean creation.
				//註冊bean後置處理器
				registerBeanPostProcessors(beanFactory);

				// Initialize message source for this context.
				//國際化
				initMessageSource();

				// Initialize event multicaster for this context.
				initApplicationEventMulticaster();

				// Initialize other special beans in specific context subclasses.
				//6、模板方法--springboot實現了這個方法
				onRefresh();

				// Check for listener beans and register them.
				//7、註冊監聽器
				registerListeners();

				// Instantiate all remaining (non-lazy-init) singletons.
				//8、完成bean工廠的初始化**方法**********************************************
				finishBeanFactoryInitialization(beanFactory);

				//9、 Last step: publish corresponding event.
				finishRefresh();
			}

			catch (BeansException ex) {
				if (logger.isWarnEnabled()) {
					logger.warn("Exception encountered during context initialization - " +
							"cancelling refresh attempt: " + ex);
				}

				// Destroy already created singletons to avoid dangling resources.
				destroyBeans();

				// Reset 'active' flag.
				cancelRefresh(ex);

				// Propagate exception to caller.
				throw ex;
			}

			finally {
				// Reset common introspection caches in Spring's core, since we
				// might not ever need metadata for singleton beans anymore...
				resetCommonCaches();
			}
		}
	}

benafactory原始碼的處理位於第三個位置。

主要涉及到兩個方法postProcessBeanFactory(beanFactory);

和invokeBeanFactoryPostProcessors(beanFactory);

image-20220301100307555

postProcessBeanFactory(beanFactory)點進去發現是一個空方法,具體的執行在invokeBeanFactoryPostProcessors(beanFactory);中

image-20220301100448919

我們在invokeBeanFactoryPostProcessors方法上打斷點一探究竟。第一次看的時候覺得這是什麼玩意,這麼長,耐住性子一步步的往下看。

方法雖長大概總結一下就是,判斷beanFactory型別,然後將註冊的BeanPostFactory放入、排好順序、執行。

invokeBeanFactoryPostProcessors 方法的內容其實比較少,大部分過程在註釋都已經寫清楚,這邊在稍微總結一下。

整個 invokeBeanFactoryPostProcessors 方法圍繞兩個介面,BeanDefinitionRegistryPostProcessor 和 BeanFactoryPostProcessor,其中 BeanDefinitionRegistryPostProcessor 繼承了 BeanFactoryPostProcessor 。

BeanDefinitionRegistryPostProcessor 主要用來在常規 BeanFactoryPostProcessor 檢測開始之前註冊其他 Bean 定義,說的簡單點,就是 BeanDefinitionRegistryPostProcessor 具有更高的優先順序,執行順序在 BeanFactoryPostProcessor 之前。

具體的過程看註釋吧

	public static void invokeBeanFactoryPostProcessors(
			ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

		// Invoke BeanDefinitionRegistryPostProcessors first, if any.
		Set<String> processedBeans = new HashSet<>();


		// 1.判斷beanFactory是否為BeanDefinitionRegistry,beanFactory為DefaultListableBeanFactory,
		// 而DefaultListableBeanFactory實現了BeanDefinitionRegistry介面,因此這邊為true
		if (beanFactory instanceof BeanDefinitionRegistry) {
			BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
			// 用於存放普通的BeanFactoryPostProcessor
			List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
			// 用於存放BeanDefinitionRegistryPostProcessor
			List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();

			// 2.首先處理入參中的beanFactoryPostProcessors
			// 遍歷所有的beanFactoryPostProcessors, 將BeanDefinitionRegistryPostProcessor和普通BeanFactoryPostProcessor區分開
			for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
				if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
					// 2.1 如果是BeanDefinitionRegistryPostProcessor
					BeanDefinitionRegistryPostProcessor registryProcessor =
							(BeanDefinitionRegistryPostProcessor) postProcessor;
					// 2.1.1 直接執行BeanDefinitionRegistryPostProcessor介面的postProcessBeanDefinitionRegistry方法
					registryProcessor.postProcessBeanDefinitionRegistry(registry);
					// 2.1.2 新增到registryProcessors(用於最後執行postProcessBeanFactory方法)
					registryProcessors.add(registryProcessor);
				}
				else {
					// 2.2 否則,只是普通的BeanFactoryPostProcessor
					// 2.2.1 新增到regularPostProcessors(用於最後執行postProcessBeanFactory方法)
					regularPostProcessors.add(postProcessor);
				}
			}

			// 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.
			// 用於儲存本次要執行的BeanDefinitionRegistryPostProcessor
			List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();

			// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
			// 3.呼叫所有實現PriorityOrdered介面的BeanDefinitionRegistryPostProcessor實現類
			// 3.1 找出所有實現BeanDefinitionRegistryPostProcessor介面的Bean的beanName
			String[] postProcessorNames =
					beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
			// 3.2 遍歷postProcessorNames
			for (String ppName : postProcessorNames) {
				// 3.3 校驗是否實現了PriorityOrdered介面
				if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
					// 3.4 獲取ppName對應的bean例項, 新增到currentRegistryProcessors中,
					currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
					// 3.5 將要被執行的加入processedBeans,避免後續重複執行
					processedBeans.add(ppName);
				}
			}
			// 3.6 進行排序(根據是否實現PriorityOrdered、Ordered介面和order值來排序)
			sortPostProcessors(currentRegistryProcessors, beanFactory);
			// 3.7 新增到registryProcessors(用於最後執行postProcessBeanFactory方法)
			registryProcessors.addAll(currentRegistryProcessors);
			// 3.8 遍歷currentRegistryProcessors, 執行postProcessBeanDefinitionRegistry方法
			invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
			// 3.9 執行完畢後, 清空currentRegistryProcessors
			currentRegistryProcessors.clear();

			// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
			// 4.呼叫所有實現了Ordered介面的BeanDefinitionRegistryPostProcessor實現類(過程跟上面的步驟3基本一樣)
			// 4.1 找出所有實現BeanDefinitionRegistryPostProcessor介面的類, 這邊重複查詢是因為執行完上面的BeanDefinitionRegistryPostProcessor,
			// 可能會新增了其他的BeanDefinitionRegistryPostProcessor, 因此需要重新查詢
			postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
			for (String ppName : postProcessorNames) {
				// 校驗是否實現了Ordered介面,並且還未執行過
				if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
					currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
					processedBeans.add(ppName);
				}
			}
			sortPostProcessors(currentRegistryProcessors, beanFactory);
			registryProcessors.addAll(currentRegistryProcessors);
			// 4.2 遍歷currentRegistryProcessors, 執行postProcessBeanDefinitionRegistry方法
			invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
			currentRegistryProcessors.clear();

			// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
			// 5.最後, 呼叫所有剩下的BeanDefinitionRegistryPostProcessors
			boolean reiterate = true;
			while (reiterate) {
				reiterate = false;
				// 5.1 找出所有實現BeanDefinitionRegistryPostProcessor介面的類
				postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
				for (String ppName : postProcessorNames) {
					// 5.2 跳過已經執行過的
					if (!processedBeans.contains(ppName)) {
						currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
						processedBeans.add(ppName);
						// 5.3 如果有BeanDefinitionRegistryPostProcessor被執行, 則有可能會產生新的BeanDefinitionRegistryPostProcessor,
						// 因此這邊將reiterate賦值為true, 代表需要再迴圈查詢一次
						reiterate = true;
					}
				}
				sortPostProcessors(currentRegistryProcessors, beanFactory);
				registryProcessors.addAll(currentRegistryProcessors);
				// 5.4 遍歷currentRegistryProcessors, 執行postProcessBeanDefinitionRegistry方法
				invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
				currentRegistryProcessors.clear();
			}

			// Now, invoke the postProcessBeanFactory callback of all processors handled so far.
			// 6.呼叫所有BeanDefinitionRegistryPostProcessor的postProcessBeanFactory方法(BeanDefinitionRegistryPostProcessor繼承自BeanFactoryPostProcessor)
			invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
			// 7.最後, 呼叫入參beanFactoryPostProcessors中的普通BeanFactoryPostProcessor的postProcessBeanFactory方法
			invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
		}

		else {
			// Invoke factory processors registered with the context instance.
			invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
		}

		// Do not initialize FactoryBeans here: We need to leave all regular beans
		// uninitialized to let the bean factory post-processors apply to them!
		// 到這裡 , 入參beanFactoryPostProcessors和容器中的所有BeanDefinitionRegistryPostProcessor已經全部處理完畢,
		// 下面開始處理容器中的所有BeanFactoryPostProcessor

		// Do not initialize FactoryBeans here: We need to leave all regular beans
		// uninitialized to let the bean factory post-processors apply to them!
		// 8.找出所有實現BeanFactoryPostProcessor介面的類
		String[] postProcessorNames =
				beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);

		// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
		// Ordered, and the rest.
		// 用於存放實現了PriorityOrdered介面的BeanFactoryPostProcessor
		List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
		// 用於存放實現了Ordered介面的BeanFactoryPostProcessor的beanName
		List<String> orderedPostProcessorNames = new ArrayList<>();
		// 用於存放普通BeanFactoryPostProcessor的beanName
		List<String> nonOrderedPostProcessorNames = new ArrayList<>();
		// 8.1 遍歷postProcessorNames, 將BeanFactoryPostProcessor按實現PriorityOrdered、實現Ordered介面、普通三種區分開
		for (String ppName : postProcessorNames) {
			// 8.2 跳過已經執行過的
			if (processedBeans.contains(ppName)) {
				// skip - already processed in first phase above
			}
			else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
				// 8.3 新增實現了PriorityOrdered介面的BeanFactoryPostProcessor
				priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
			}
			else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
				// 8.4 新增實現了Ordered介面的BeanFactoryPostProcessor的beanName
				orderedPostProcessorNames.add(ppName);
			}
			else {
				// 8.5 新增剩下的普通BeanFactoryPostProcessor的beanName
				nonOrderedPostProcessorNames.add(ppName);
			}
		}

		// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
		// 9.呼叫所有實現PriorityOrdered介面的BeanFactoryPostProcessor
		// 9.1 對priorityOrderedPostProcessors排序
		sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
		// 9.2 遍歷priorityOrderedPostProcessors, 執行postProcessBeanFactory方法
		invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);

		// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
		// 10.呼叫所有實現Ordered介面的BeanFactoryPostProcessor
		List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>();
		for (String postProcessorName : orderedPostProcessorNames) {
			// 10.1 獲取postProcessorName對應的bean例項, 新增到orderedPostProcessors, 準備執行
			orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
		}
		// 10.2 對orderedPostProcessors排序
		sortPostProcessors(orderedPostProcessors, beanFactory);
		// 10.3 遍歷orderedPostProcessors, 執行postProcessBeanFactory方法
		invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);

		// Finally, invoke all other BeanFactoryPostProcessors.
		// 11.呼叫所有剩下的BeanFactoryPostProcessor
		List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
		for (String postProcessorName : nonOrderedPostProcessorNames) {
			// 11.1 獲取postProcessorName對應的bean例項, 新增到nonOrderedPostProcessors, 準備執行
			nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
		}
		// 11.2 遍歷nonOrderedPostProcessors, 執行postProcessBeanFactory方法
		invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);

		// Clear cached merged bean definitions since the post-processors might have
		// modified the original metadata, e.g. replacing placeholders in values...
		// 12.清除後設資料快取(mergedBeanDefinitions、allBeanNamesByType、singletonBeanNamesByType),
		// 因為後處理器可能已經修改了原始後設資料,例如, 替換值中的佔位符...
		beanFactory.clearMetadataCache();
	}

相關文章