成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

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

invokeBeanFactoryPostProcessors的理解

Spring中有兩個(gè)非常重要的擴(kuò)展點(diǎn):

我們提供的服務(wù)有:網(wǎng)站設(shè)計(jì)制作、網(wǎng)站建設(shè)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、東鄉(xiāng)ssl等。為上千余家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的東鄉(xiāng)網(wǎng)站制作公司
  • BeanFactoryPostProcessor
  • BeanPostProcessor
    其中第一個(gè)是可以對(duì)BeanDefinition注冊(cè)時(shí)進(jìn)行擴(kuò)展,而第二個(gè)是對(duì)spring中IOC容器中的對(duì)象進(jìn)行實(shí)例化的時(shí)候進(jìn)行擴(kuò)展。
    今天主要談一下對(duì)BeanFactoryPostProcessor的幾點(diǎn)理解:
BeanFactoryPostProcessor
  • 這是個(gè)重要的接口,其還有一個(gè)子接口:BeanDefinitionRegistryPostProcessor:
    在這里插入圖片描述
  • 在spring容器的啟動(dòng)過(guò)程,對(duì)所有組件的注冊(cè)主要就是通過(guò)對(duì)這兩個(gè)接口的處理來(lái)完成的。
  • spring啟動(dòng)時(shí),最主要的方法就是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這個(gè)方法,就是處理容器中所有bean的注冊(cè)過(guò)程
  • invokeBeanFactoryPostProcessors這個(gè)方法具體如下:
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()));
		}
	}
  • 這個(gè)方法,其實(shí)只有第一條語(yǔ)句中的invokeBeanFactoryPostProcessors方法,是重要且對(duì)容器bean注冊(cè)有效
  • 這個(gè)方法傳入的參數(shù)第一個(gè)是工廠,第二個(gè)參數(shù)是BeanFactoryPostProcessor的list
  • 正常啟動(dòng)的時(shí)候,如果我們不做處理,那么第二個(gè)參數(shù)就是一個(gè)空的list
  • 我們也可以在代碼中進(jìn)行BeanFactoryPostProcessor的注入
  • spring容器啟動(dòng)到這里時(shí),會(huì)自動(dòng)注冊(cè)5個(gè)bean:
    在這里插入圖片描述

上述圖片中:

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

其主要處理流程如下:

  1. 對(duì)傳入的BeanFactoryPostProcessor進(jìn)行處理,添加進(jìn)等待運(yùn)行的列表中
    在這里插入圖片描述
  2. 對(duì)實(shí)現(xiàn)了BeanDefinitionRegistryPostProcessor以及PriorityOrdered接口的組件進(jìn)行實(shí)例化,并調(diào)用BeanDefinitionRegistryPostProcessor接口方法,此時(shí),就是調(diào)用ConfigurationClassPostProcessor這個(gè)組件,來(lái)進(jìn)行容器中bean的注冊(cè),具體的注冊(cè)過(guò)程此處略過(guò)不表
// 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. 對(duì)實(shí)現(xiàn)了BeanDefinitionRegistryPostProcessor以及Ordered接口的組件進(jìn)行實(shí)例化,并調(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. 對(duì)只實(shí)現(xiàn)了BeanDefinitionRegistryPostProcessor接口的組件進(jìn)行實(shí)例化,并調(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. 對(duì)實(shí)現(xiàn)了BeanDefinitionRegistryPostProcessor的組件,調(diào)用其父接口BeanFactoryPostProcessor的方法
// Now, invoke the postProcessBeanFactory callback of all processors handled so far.
			invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
			invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
  1. 對(duì)實(shí)現(xiàn)了BeanFactoryPostProcessor以及PriorityOrdered接口的組件,調(diào)用相應(yīng)接口中的方法
// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
		sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
		invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
  1. 對(duì)實(shí)現(xiàn)了BeanFactoryPostProcessor以及Ordered接口的組件進(jìn)行實(shí)例化,并調(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. 對(duì)只實(shí)現(xiàn)了BeanFactoryPostProcessor接口的組件進(jìn)行實(shí)例化,并調(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)用實(shí)現(xiàn)了BeanDefinitionRegistryPostProcessor接口的組件以及實(shí)現(xiàn)了BeanFactoryPostProcessor接口的組件;其中最主要的組件就是ConfigurationClassPostProcessor,其實(shí)現(xiàn)的是子接口,實(shí)現(xiàn)的方法功能,就是對(duì)容器中進(jìn)行beanDefinition的注冊(cè)。

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

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

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

廣告

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

營(yíng)銷型網(wǎng)站建設(shè)
武夷山市| 界首市| 苍山县| 牟定县| 蛟河市| 彩票| 永寿县| 神木县| 墨竹工卡县| 榆树市| 神木县| 商河县| 阿勒泰市| 台山市| 浮梁县| 苍梧县| 白城市| 临桂县| 东乌| 天长市| 邵阳市| SHOW| 南丹县| 陕西省| 隆化县| 建湖县| 亳州市| 高密市| 柘荣县| 枞阳县| 东乌珠穆沁旗| 昌都县| 昂仁县| 民勤县| 永昌县| 囊谦县| 哈巴河县| 措美县| 随州市| 朝阳市| 台中县|