ApplicationContextAware中獲取不到bean
阿新 • • 發佈:2020-08-11
今天遇到一個非常奇怪的問題,寫了一個工具類實現ApplicationContextAware介面來獲取Spring上下文, 程式碼如下:
public class SpringContextHolder implements ApplicationContextAware { private static ApplicationContext applicationContext = null; /** * 獲取靜態變數中的ApplicationContext. */ public static ApplicationContext getApplicationContext() { assertContextInjected(); return applicationContext; } /** * 從靜態變數applicationContext中得到Bean, 自動轉型為所賦值物件的型別. */ @SuppressWarnings("unchecked") public static <T> T getBean(String name) { assertContextInjected(); return (T) applicationContext.getBean(name); } /** * 從靜態變數applicationContext中得到Bean, 自動轉型為所賦值物件的型別. */ public static <T> T getBean(Class<T> requiredType) { assertContextInjected(); return applicationContext.getBean(requiredType); } /** * 清除SpringContextHolder中的ApplicationContext為Null. */ public static void clearHolder() { applicationContext = null; } /** * 實現ApplicationContextAware介面, 注入Context到靜態變數中. */ @Override public void setApplicationContext(ApplicationContext applicationContext) { SpringContextHolder.applicationContext = applicationContext; } /** * 檢查ApplicationContext不為空. */ private static void assertContextInjected() { Validate.validState(applicationContext != null, "applicaitonContext屬性未注入, 請在applicationContext.xml中定義SpringContextHolder."); } }
專案啟動的時候,APPLICATION_CONTEXT是初始化了的,但是當定時任務呼叫的時候,獲取到的就為null。 後來仔細排查,將Spring Boot Devtools依賴去了,就正常了。專案中用的是quartz,獲取容器中的bean,獲取不帶。
查了下Spring Boot Devtools的熱部署原理: 深層原理是使用了兩個ClassLoader,一個Classloader載入那些不會改變的類(第三方Jar包), 另一個ClassLoader載入會更改的類,稱為restart ClassLoader,這樣在有程式碼更改的時候, 原來的restart ClassLoader被丟棄,重新建立一個restart ClassLoader,由於需要載入的類相比較少,所以實現了較快的重啟時間。