今天遇到一個非常奇怪的問題,寫了一個工具類實現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,由於需要加載的類相比較少,所以實現了較快的重啟時間。