Spring中ApplicationContextAware的使用方法詳解
ApplicationContextAware 通過它Spring容器會自動把上下文環(huán)境對象調(diào)用ApplicationContextAware接口中的setApplicationContext方法。
我們在ApplicationContextAware的實現(xiàn)類中,就可以通過這個上下文環(huán)境對象得到Spring容器中的Bean。
看到—Aware就知道是干什么的了,就是屬性注入的,但是這個ApplicationContextAware的不同地方在于,實現(xiàn)了這個接口的bean,當(dāng)spring容器初始化的時候,會自動的將ApplicationContext注入進(jìn)來:
使用方法如下:
1.實現(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未注入 ,請在springContext.xml中注入SpringHolder!");
}
}因為我們在做開發(fā)的時候,并不是說在每一個地方都能將屬性注入到我們想要的地方去的,比如在Utils使用到dao,我們就不能直接注入了,這個時候就是我們需要封裝springContext的時候了,而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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis關(guān)聯(lián)查詢的實現(xiàn)
MyBatis可以通過定義多個表的關(guān)聯(lián)關(guān)系,實現(xiàn)多表查詢,本文主要介紹了MyBatis關(guān)聯(lián)查詢的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2023-11-11
Java高并發(fā)編程之CAS實現(xiàn)無鎖隊列代碼實例
這篇文章主要介紹了Java高并發(fā)編程之CAS實現(xiàn)無鎖隊列代碼實例,在多線程操作中,我們通常會添加鎖來保證線程的安全,那么這樣勢必會影響程序的性能,那么為了解決這一問題,于是就有了在無鎖操作的情況下依然能夠保證線程的安全,需要的朋友可以參考下2023-12-12
SpringBoot項目配置postgresql數(shù)據(jù)庫完整步驟(配置多數(shù)據(jù)源)
PostgreSQL是一種特性非常齊全的自由軟件的對象-關(guān)系型數(shù)據(jù)庫管理系統(tǒng)(ORDBMS),下面這篇文章主要給大家介紹了關(guān)于SpringBoot項目配置postgresql數(shù)據(jù)庫(配置多數(shù)據(jù)源)的相關(guān)資料,需要的朋友可以參考下2023-05-05
java.lang.OutOfMemoryError: Metaspace異常解決的方法
這篇文章主要介紹了java.lang.OutOfMemoryError: Metaspace異常解決的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

