invokeBeanFactoryPostProcessors的理解-創(chuàng)新互聯(lián)

invokeBeanFactoryPostProcessors的理解

Spring中有兩個非常重要的擴(kuò)展點:

我們提供的服務(wù)有:網(wǎng)站設(shè)計制作、網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、東鄉(xiāng)ssl等。為上千余家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的東鄉(xiāng)網(wǎng)站制作公司
  • BeanFactoryPostProcessor
  • BeanPostProcessor
    其中第一個是可以對BeanDefinition注冊時進(jìn)行擴(kuò)展,而第二個是對spring中IOC容器中的對象進(jìn)行實例化的時候進(jìn)行擴(kuò)展。
    今天主要談一下對BeanFactoryPostProcessor的幾點理解:
BeanFactoryPostProcessor
  • 這是個重要的接口,其還有一個子接口:BeanDefinitionRegistryPostProcessor:
    在這里插入圖片描述
  • 在spring容器的啟動過程,對所有組件的注冊主要就是通過對這兩個接口的處理來完成的。
  • spring啟動時,最主要的方法就是refresh方法:
public void refresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {	// Prepare this context for refreshing.
			prepareRefresh();

			// Tell the subclass to refresh the internal bean factory.
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.
			prepareBeanFactory(beanFactory);

			try {		// Allows post-processing of the bean factory in context subclasses.
				postProcessBeanFactory(beanFactory);

				// Invoke factory processors registered as beans in the context.
				invokeBeanFactoryPostProcessors(beanFactory);

				// Register bean processors that intercept bean creation.
				registerBeanPostProcessors(beanFactory);

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

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

				// Initialize other special beans in specific context subclasses.
				onRefresh();

				// Check for listener beans and register them.
				registerListeners();

				// Instantiate all remaining (non-lazy-init) singletons.
				finishBeanFactoryInitialization(beanFactory);

				// 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();
			}
		}
	}
  • 其中,invokeBeanFactoryPostProcessors這個方法,就是處理容器中所有bean的注冊過程
  • invokeBeanFactoryPostProcessors這個方法具體如下:
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()));
		}
	}
  • 這個方法,其實只有第一條語句中的invokeBeanFactoryPostProcessors方法,是重要且對容器bean注冊有效
  • 這個方法傳入的參數(shù)第一個是工廠,第二個參數(shù)是BeanFactoryPostProcessor的list
  • 正常啟動的時候,如果我們不做處理,那么第二個參數(shù)就是一個空的list
  • 我們也可以在代碼中進(jìn)行BeanFactoryPostProcessor的注入
  • spring容器啟動到這里時,會自動注冊5個bean:
    在這里插入圖片描述

上述圖片中:

  1. 第一個ConfigurationClassPostProcessor,實現(xiàn)的是BeanDefinitionRegistryPostProcessor接口,是最主要的掃描和注冊
  2. 第三個EventListenerMethodProcessor,實現(xiàn)BeanFactoryPostProcessor接口,與事件相關(guān)的處理組件
  3. 第四個和第五個實現(xiàn)BeanPostProcessor接口,參與對象創(chuàng)建過程中的注入等處理
  • 在invokeBeanFactoryPostProcessors方法中,spring會通過所有BeanFactoryPostProcessor的功能,來掃描并注冊所有的bean,從方法名稱來看,其作用就是調(diào)用所有的BeanFactoryPostProcessor,

其主要處理流程如下:

  1. 對傳入的BeanFactoryPostProcessor進(jìn)行處理,添加進(jìn)等待運行的列表中
    在這里插入圖片描述
  2. 對實現(xiàn)了BeanDefinitionRegistryPostProcessor以及PriorityOrdered接口的組件進(jìn)行實例化,并調(diào)用BeanDefinitionRegistryPostProcessor接口方法,此時,就是調(diào)用ConfigurationClassPostProcessor這個組件,來進(jìn)行容器中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)) {currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
					processedBeans.add(ppName);
				}
			}
			sortPostProcessors(currentRegistryProcessors, beanFactory);
			registryProcessors.addAll(currentRegistryProcessors);
			invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
			currentRegistryProcessors.clear();
  1. 對實現(xiàn)了BeanDefinitionRegistryPostProcessor以及Ordered接口的組件進(jìn)行實例化,并調(diào)用相應(yīng)接口方法
// 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);
				}
			}
			sortPostProcessors(currentRegistryProcessors, beanFactory);
			registryProcessors.addAll(currentRegistryProcessors);
			invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
			currentRegistryProcessors.clear();
  1. 對只實現(xiàn)了BeanDefinitionRegistryPostProcessor接口的組件進(jìn)行實例化,并調(diào)用相應(yīng)接口方法
// 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();
			}
  1. 對實現(xiàn)了BeanDefinitionRegistryPostProcessor的組件,調(diào)用其父接口BeanFactoryPostProcessor的方法
// Now, invoke the postProcessBeanFactory callback of all processors handled so far.
			invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
			invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
  1. 對實現(xiàn)了BeanFactoryPostProcessor以及PriorityOrdered接口的組件,調(diào)用相應(yīng)接口中的方法
// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
		sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
		invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
  1. 對實現(xiàn)了BeanFactoryPostProcessor以及Ordered接口的組件進(jìn)行實例化,并調(diào)用相應(yīng)接口方法
// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
		ListorderedPostProcessors = new ArrayList<>();
		for (String postProcessorName : orderedPostProcessorNames) {	orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
		}
		sortPostProcessors(orderedPostProcessors, beanFactory);
		invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
  1. 對只實現(xiàn)了BeanFactoryPostProcessor接口的組件進(jìn)行實例化,并調(diào)用相應(yīng)接口方法
// Finally, invoke all other BeanFactoryPostProcessors.
		ListnonOrderedPostProcessors = new ArrayList<>();
		for (String postProcessorName : nonOrderedPostProcessorNames) {	nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
		}
		invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);

總結(jié):

  • 此方法主要作用,就是按照順序分別調(diào)用實現(xiàn)了BeanDefinitionRegistryPostProcessor接口的組件以及實現(xiàn)了BeanFactoryPostProcessor接口的組件;其中最主要的組件就是ConfigurationClassPostProcessor,其實現(xiàn)的是子接口,實現(xiàn)的方法功能,就是對容器中進(jìn)行beanDefinition的注冊。

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

新聞名稱:invokeBeanFactoryPostProcessors的理解-創(chuàng)新互聯(lián)
新聞來源:http://bm7419.com/article32/dicjpc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站移動網(wǎng)站建設(shè)、搜索引擎優(yōu)化、做網(wǎng)站網(wǎng)站建設(shè)、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)