問題
最近項目小組在重新規划工程的業務緩存,其中涉及到部分代碼重構,過程中發現有些工具類中的靜態方法需要依賴別的對象實例(該實例已配置在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(); } }
原文:https://blog.csdn.net/RogueFist/article/details/79575665