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注入進來:
使用方法如下:
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)文章
SpringBoot項目集成Swagger和swagger-bootstrap-ui及常用注解解讀
這篇文章主要介紹了SpringBoot項目集成Swagger和swagger-bootstrap-ui及常用注解解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
Spring?Boot提高開發(fā)效率必備工具lombok使用
這篇文章主要為大家介紹了Spring?Boot提高開發(fā)效率的必備工具lombok使用方法示例及步驟說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-03-03
springboot多數(shù)據(jù)源配合docker部署mysql主從實現(xiàn)讀寫分離效果
這篇文章主要介紹了springboot多數(shù)據(jù)源配合docker部署mysql主從實現(xiàn)讀寫分離,通過使用docker獲取mysql鏡像,具體內(nèi)容詳情跟隨小編一起看看吧2021-09-09
Mybatis如何實現(xiàn)InsertOrUpdate功能
這篇文章主要介紹了Mybatis如何實現(xiàn)InsertOrUpdate功能,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
spring學(xué)習(xí)之參數(shù)傳遞與檢驗詳解
這篇文章主要給大家介紹了關(guān)于spring參數(shù)傳遞與檢驗的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作能帶來一定的幫助,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-07-07
記一次springboot服務(wù)凌晨無故宕機問題的解決
這篇文章主要介紹了記一次springboot服務(wù)凌晨無故宕機問題的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09

