溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用prepareRefresh()

發(fā)布時(shí)間:2021-10-12 15:33:19 來源:億速云 閱讀:92 作者:iii 欄目:編程語(yǔ)言

這篇文章主要介紹“如何使用prepareRefresh()”,在日常操作中,相信很多人在如何使用prepareRefresh()問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”如何使用prepareRefresh()”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

refresh()

這個(gè)方法主要做了一下這幾件事情:

  • 容器刷新前的準(zhǔn)備

  • 初始化beanFactory,加載并解析配置

  • 設(shè)置beanFactory的屬性

  • BeanFactory創(chuàng)建完成后進(jìn)行的后置處理工作

  • 執(zhí)行BeanFactoryPostProcessor的方法

  • 注冊(cè)BeanPostProcessor

  • 初始化MessageSource組件

  • 初始化事件派發(fā)器

  • 子類重寫這個(gè)方法,在容器刷新的時(shí)候可以自定義邏輯

  • 給容器中將所有項(xiàng)目里面的ApplicationListener注冊(cè)進(jìn)來

  • 初始化所有剩下的單實(shí)例bean

  • 完成BeanFactory的初始化創(chuàng)建工作,IOC容器就創(chuàng)建完成

這里我們先來看看容器刷新前做了些什么吧

prepareRefresh()

方法的源碼并不多,如下:

protected void prepareRefresh() {
	// Switch to active.
	// 這一句很明顯的獲取了系統(tǒng)當(dāng)前時(shí)間,其實(shí)他的作用是來記錄當(dāng)前的啟動(dòng)時(shí)間的
	this.startupDate = System.currentTimeMillis();
	
	// 這兩個(gè)狀態(tài)的設(shè)置,前者關(guān)閉程序設(shè)置為false,后者運(yùn)行標(biāo)識(shí)設(shè)置為true
	this.closed.set(false);
	this.active.set(true);

	if (logger.isDebugEnabled()) {
		if (logger.isTraceEnabled()) {
			logger.trace("Refreshing " + this);
		}
		else {
			logger.debug("Refreshing " + getDisplayName());
		}
	}

	// Initialize any placeholder property sources in the context environment.
	// 目前還是一個(gè)空方法,應(yīng)該后面會(huì)補(bǔ)充
	initPropertySources();

	// Validate that all properties marked as required are resolvable:
	// see ConfigurablePropertyResolver#setRequiredProperties
	// 校驗(yàn) xml配置文件
	getEnvironment().validateRequiredProperties();

	// Store pre-refresh ApplicationListeners...
	// 判斷刷新前的運(yùn)用程序監(jiān)聽集合是否為空,為空初始化applicationListeners監(jiān)聽,不為空,則清空監(jiān)聽器
	if (this.earlyApplicationListeners == null) {
		this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
	}
	else {
		// Reset local application listeners to pre-refresh state.
		this.applicationListeners.clear();
		this.applicationListeners.addAll(this.earlyApplicationListeners);
	}

	// Allow for the collection of early ApplicationEvents,
	// to be published once the multicaster is available...
	// 創(chuàng)建刷新前的事件集合
	this.earlyApplicationEvents = new LinkedHashSet<>();
}

這個(gè)方法prepareRefresh(),是在spring5.3之后加了程序,所以之前的版本是沒有辦法看到的。

就做了這幾件事情

  • 設(shè)置容器的啟動(dòng)時(shí)間

  • 設(shè)置活躍狀態(tài)為true

  • 設(shè)置關(guān)閉狀態(tài)為false

  • 獲取Environment對(duì)象,并加載當(dāng)前系統(tǒng)的環(huán)境到Environment中

  • 準(zhǔn)備監(jiān)聽器和事件的集合對(duì)象,默認(rèn)為空

到此,關(guān)于“如何使用prepareRefresh()”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI