問題
最近項目小組在重新規劃工程的業務緩存,其中涉及到部分代碼重構,過程中發現有些工具類中的靜態方法需要依賴別的對象實例(該實例已配置在xml成Spring bean,非靜態可以用@Autowired加載正常使用),而我們知道,類加載後靜態成員是在內存的共享區,靜態方法裏面的變量必然要使用靜態成員變量,這就有了如下代碼:
@Component
public class TestClass {
@Autowired
private static AutowiredTypeComponent component;
// 調用靜態組件的方法
public static void testMethod() {
component.callTestMethod();
}
}
編譯正常,但運行時報java.lang.NullPointerException: null異常,顯然在調用testMethod()方法時,component變量還沒被初始化,報NPE。
原因
所以,在Springframework裏,我們是不能@Autowired一個靜態變量,使之成爲一個Spring bean的。爲什麼?其實很簡單,因爲當類加載器加載靜態變量時,Spring上下文尚未加載。所以類加載器不會在bean中正確注入靜態類,並且會失敗。
解決方案
方式一
將@Autowired 註解到類的構造函數上。很好理解,Spring掃描到AutowiredTypeComponent的bean,然後賦給靜態變量component。示例如下:
@Component
public class TestClass {
private static AutowiredTypeComponent component;
@Autowired
public TestClass(AutowiredTypeComponent component) {
TestClass.component = component;
}
// 調用靜態組件的方法
public static void testMethod() {
component.callTestMethod();
}
}
方式二
給靜態組件加setter方法,並在這個方法上加上@Autowired。Spring能掃描到AutowiredTypeComponent的bean,然後通過setter方法注入。示例如下:
@Component
public class TestClass {
private static AutowiredTypeComponent component;
@Autowired
public void setComponent(AutowiredTypeComponent component){
TestClass.component = component;
}
// 調用靜態組件的方法
public static void testMethod() {
component.callTestMethod();
}
}
方式三
定義一個靜態組件,定義一個非靜態組件並加上@Autowired註解,再定義一個初始化組件的方法並加上@PostConstruct註解。這個註解是JavaEE引入的,作用於servlet生命週期的註解,你只需要知道,用它註解的方法在構造函數之後就會被調用。示例如下:
@Component
public class TestClass {
private static AutowiredTypeComponent component;
@Autowired
private AutowiredTypeComponent autowiredComponent;
@PostConstruct
private void beforeInit() {
component = this.autowiredComponent;
}
// 調用靜態組件的方法
public static void testMethod() {
component.callTestMethod();
}
}
方式四
直接用Spring框架工具類獲取bean,定義成局部變量使用。但有弊端:如果該類中有多個靜態方法多次用到這個組件則每次都要這樣獲取,個人不推薦這種方式。示例如下:
public class TestClass {
// 調用靜態組件的方法
public static void testMethod() {
AutowiredTypeComponent component = SpringApplicationContextUtil.getBean("component");
component.callTestMethod();
}
}
總結
在上面的代碼示例中,我每個類都加了@Component註解,其實可以根據需要進行變更,比如這個類是處理業務邏輯,可以換成@Service;這個類是處理請求進行轉發或重定向的,可以換成@Controller(是Spring-mvc的註解);這個類是專門用來操作Dao的就@Repository。Spring的註解幫你做了一件很有意義的事:就是它們對應用進行了分層,這樣就能將請求處理、業務邏輯處理、數據庫操作處理分離出來,爲代碼解耦,也方便了項目的開發和維護。
Spring容器bean加載機制用到了Java的反射,這裏先不作贅述,以後會專門寫一篇文章來總結Java反射在Spring的IoC和AoP中的應用。
---------------------
原文:https://blog.csdn.net/RogueFist/article/details/79575665