關鍵字:spring容器加載完畢做一件事情(利用ContextRefreshedEvent事件)
應用場景:很多時候我們想要在某個類加載完畢時幹某件事情,但是使用了spring管理對象,我們這個類引用了其他類(可能是更復雜的關聯),所以當我們去使用這個類做事情時發現包空指針錯誤,這是因爲我們這個類有可能已經初始化完成,但是引用的其他類不一定初始化完成,所以發生了空指針錯誤,解決方案如下:
1、寫一個類繼承spring的ApplicationListener監聽,並監控ContextRefreshedEvent事件(容易初始化完成事件)
2、定義簡單的bean:<bean id="beanDefineConfigue" class="com.creatar.portal.webservice.BeanDefineConfigue"></bean>
或者直接使用@Component("BeanDefineConfigue")註解方式
完整的類如下:
package com.creatar.portal.webservice;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
@Component("BeanDefineConfigue")
public class BeanDefineConfigue implements
ApplicationListener<ContextRefreshedEvent> {//ContextRefreshedEvent爲初始化完畢事件,spring還有很多事件可以利用
// @Autowired
// private IRoleDao roleDao;
/**
* 當一個ApplicationContext被初始化或刷新觸發
*/
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
// roleDao.getUserList();//spring容器初始化完畢加載用戶列表到內存
System.out.println("spring容易初始化完畢================================================");
}
}
或者使用xml配置方式(非註解),簡單配置個bean即可
<bean id="beanDefineConfigue" class="com.creatar.portal.webservice.BeanDefineConfigue"></bean>
其他定義方式:
完整的類如下:
package com.creatar.portal.webservice;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
@Component("BeanDefineConfigue2")
public class BeanDefineConfigue2 implements ApplicationListener<ApplicationEvent> {
List<String> list = new ArrayList<String>();
/**
* 當一個ApplicationContext被初始化或刷新觸發
*/
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent) {
System.out.println("spring容易初始化完畢================================================888");
}
}
}
spring其他事件:
spring中已經內置的幾種事件:
ContextClosedEvent 、ContextRefreshedEvent 、ContextStartedEvent 、ContextStoppedEvent 、RequestHandleEvent
後續研究:
applicationontext和使用MVC之後的webApplicationontext會兩次調用上面的方法,如何區分這個兩種容器呢?
但是這個時候,會存在一個問題,在web 項目中(spring mvc),系統會存在兩個容器,一個是root application context ,另一個就是我們自己的 projectName-servlet context(作爲root application context的子容器)。
這種情況下,就會造成onApplicationEvent方法被執行兩次。爲了避免上面提到的問題,我們可以只在root application context初始化完成後調用邏輯代碼,其他的容器的初始化完成,則不做任何處理,修改後代碼
如下:
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if(event.getApplicationContext().getParent() == null){//root application context 沒有parent,他就是老大.
//需要執行的邏輯代碼,當spring容器初始化完成後就會執行該方法。
}
}
後續發現加上以上判斷還是能執行兩次,不加的話三次,最終研究結果使用以下判斷更加準確:event.getApplicationContext().getDisplayName().equals("Root WebApplicationContext")
spring容器加載完畢做一件事情(利用ContextRefreshedEvent事件)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
使用 certbot 申請泛域名https證書
天天向上卡索
2019-02-24 12:51:43
第二十二章:動畫(四)
wangccsy
2019-02-24 10:57:10
關於 script src=http://7o8.net >的解決方案
ifuckyours
2019-02-23 13:40:57
2018-2019年中國CDN市場發展報告:阿里雲成爲中國CDN市場的領軍者
xx暖憶
2019-02-23 13:30:10
部署SSL證書後,網頁內容造成頁面錯誤提示的處理辦法
doudingmao
2019-02-23 13:29:48
單機殺毒軟件僅是企業信息安全的時尚毛衣
doudingmao
2019-02-23 13:29:47
淺析新站做SEO的三大要點
zuotuitui
2019-02-23 13:37:36
2019年Java面試-併發容器篇
王知無
2019-02-24 15:12:46
深入理解Spring事務的傳播行爲
ゞ .邓澎波
2019-02-24 12:42:54
Spring Cloud Alibaba基礎教程:支持的幾種服務消費方式(RestTemplate、WebClient、Feign)
程序猿dd
2019-02-24 10:41:27
某熊的技術之路指北 ☯
王下邀月熊_Chevalier
2019-02-23 16:23:03
[spring boot] jdbc
locke
2019-02-23 16:13:10
Spring Security視頻地址
joseph999
2019-02-23 13:24:27
精華【分佈式微服務雲架構dubbo+zookeeper+springmvc+mybatis+shiro+redis】分佈式大型互聯網企業架構!
qq5930d18310d6b
2019-02-23 13:16:59
JEESZ分佈式框架--技術介紹文檔
qq5930d18310d6b
2019-02-23 13:16:59
24小時熱門文章