Spring中ApplicationContextAware的使用方法詳解
ApplicationContextAware
通過它Spring容器會(huì)自動(dòng)把上下文環(huán)境對(duì)象調(diào)用ApplicationContextAware
接口中的setApplicationContext
方法。
我們?cè)?code>ApplicationContextAware的實(shí)現(xiàn)類中,就可以通過這個(gè)上下文環(huán)境對(duì)象得到Spring容器中的Bean。
看到—Aware就知道是干什么的了,就是屬性注入的,但是這個(gè)ApplicationContextAware的不同地方在于,實(shí)現(xiàn)了這個(gè)接口的bean,當(dāng)spring容器初始化的時(shí)候,會(huì)自動(dòng)的將ApplicationContext注入進(jìn)來:
使用方法如下:
1.實(shí)現(xiàn)ApplicationContextAware接口:
import org.springframework.beans.BeansException; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import com.co.ayz.rpc.registry.ServiceRegistry; public class RpcServer implements ApplicationContextAware{ private ApplicationContext context; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { // TODO Auto-generated method stub context = applicationContext; } //獲得applicationContext public static ApplicationContext getApplicationContext() { //assertContextInjected(); return context; } public static void clearHolder(){ context=null; } //獲取Bean public static <T> T getBean(Class<T> requiredType){ //assertContextInjected(); return (T) getApplicationContext().getBean(requiredType); } @SuppressWarnings("unchecked") public static <T> T getBean(String name){ assertContextInjected(); return (T) getApplicationContext().getBean(name); } //判斷application是否為空 public static void assertContextInjected(){ Validate.isTrue(context==null, "application未注入 ,請(qǐng)?jiān)趕pringContext.xml中注入SpringHolder!"); } }
因?yàn)槲覀冊(cè)谧鲩_發(fā)的時(shí)候,并不是說在每一個(gè)地方都能將屬性注入到我們想要的地方去的,比如在Utils使用到dao,我們就不能直接注入了,這個(gè)時(shí)候就是我們需要封裝springContext的時(shí)候了,而ApplicationContextAware就起了關(guān)鍵性的作用。
自己寫的demo:
/** * @Title: SpringJobBeanFactory.java * @Package com.founder.mrp.job * @Description: TODO * @version V1.0 */ package com.founder.mrp.job; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class SpringJobBeanFactory implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringJobBeanFactory.applicationContext=applicationContext; } public static ApplicationContext getApplicationContext() { return applicationContext; } @SuppressWarnings("unchecked") public static <T> T getBean(String name) throws BeansException { if (applicationContext == null){ return null; } return (T)applicationContext.getBean(name); } } 使用:
TypeSetErpService typeSetErpServ = SpringJobBeanFactory.getBean("typeSetErpServiceImpl");
到此這篇關(guān)于Spring中ApplicationContextAware的作用的文章就介紹到這了,更多相關(guān)Spring中ApplicationContextAware作用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis關(guān)聯(lián)查詢的實(shí)現(xiàn)
MyBatis可以通過定義多個(gè)表的關(guān)聯(lián)關(guān)系,實(shí)現(xiàn)多表查詢,本文主要介紹了MyBatis關(guān)聯(lián)查詢的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11Java高并發(fā)編程之CAS實(shí)現(xiàn)無(wú)鎖隊(duì)列代碼實(shí)例
這篇文章主要介紹了Java高并發(fā)編程之CAS實(shí)現(xiàn)無(wú)鎖隊(duì)列代碼實(shí)例,在多線程操作中,我們通常會(huì)添加鎖來保證線程的安全,那么這樣勢(shì)必會(huì)影響程序的性能,那么為了解決這一問題,于是就有了在無(wú)鎖操作的情況下依然能夠保證線程的安全,需要的朋友可以參考下2023-12-12Java Socket實(shí)現(xiàn)多線程通信功能示例
這篇文章主要介紹了Java Socket實(shí)現(xiàn)多線程通信功能,結(jié)合具體實(shí)例形式較為詳細(xì)的分析了java多線程通信的原理及客戶端、服務(wù)器端相應(yīng)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06SpringBoot項(xiàng)目配置postgresql數(shù)據(jù)庫(kù)完整步驟(配置多數(shù)據(jù)源)
PostgreSQL是一種特性非常齊全的自由軟件的對(duì)象-關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)(ORDBMS),下面這篇文章主要給大家介紹了關(guān)于SpringBoot項(xiàng)目配置postgresql數(shù)據(jù)庫(kù)(配置多數(shù)據(jù)源)的相關(guān)資料,需要的朋友可以參考下2023-05-05啟動(dòng)Tomcat時(shí)出現(xiàn)大量亂碼的解決方法
今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識(shí),文章圍繞著啟動(dòng)Tomcat時(shí)出現(xiàn)大量亂碼的解決方法展開,文中有非常詳細(xì)的介紹及圖文示例,需要的朋友可以參考下2021-06-06java.lang.OutOfMemoryError: Metaspace異常解決的方法
這篇文章主要介紹了java.lang.OutOfMemoryError: Metaspace異常解決的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03