Spring @Autowired注解靜態(tài)實(shí)例對(duì)象
問(wèn)題
最近項(xiàng)目小組在重新規(guī)劃工程的業(yè)務(wù)緩存,其中涉及到部分代碼重構(gòu),過(guò)程中發(fā)現(xiàn)有些工具類中的靜態(tài)方法需要依賴別的對(duì)象實(shí)例(該實(shí)例已配置在xml成Spring bean,非靜態(tài)可以用@Autowired加載正常使用),而我們知道,類加載后靜態(tài)成員是在內(nèi)存的共享區(qū),靜態(tài)方法里面的變量必然要使用靜態(tài)成員變量,這就有了如下代碼:
1
2
3
4
5
6
7
8
9
|
@Component public class TestClass { @Autowired private static AutowiredTypeComponent component; // 調(diào)用靜態(tài)組件的方法 public static void testMethod() { component.callTestMethod(); } } |
編譯正常,但運(yùn)行時(shí)報(bào)java.lang.NullPointerException: null異常,顯然在調(diào)用testMethod()方法時(shí),component變量還沒(méi)被初始化,報(bào)NPE。
原因
所以,在Springframework里,我們是不能@Autowired一個(gè)靜態(tài)變量,使之成為一個(gè)Spring bean的。為什么?其實(shí)很簡(jiǎn)單,因?yàn)楫?dāng)類加載器加載靜態(tài)變量時(shí),Spring上下文尚未加載。所以類加載器不會(huì)在bean中正確注入靜態(tài)類,并且會(huì)失敗。
解決方案
方式一
將@Autowired 注解到類的構(gòu)造函數(shù)上。很好理解,Spring掃描到AutowiredTypeComponent的bean,然后賦給靜態(tài)變量component。示例如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
@Component public class TestClass { private static AutowiredTypeComponent component; @Autowired public TestClass(AutowiredTypeComponent component) { TestClass.component = component; } // 調(diào)用靜態(tài)組件的方法 public static void testMethod() { component.callTestMethod(); } } |
方式二
給靜態(tài)組件加setter方法,并在這個(gè)方法上加上@Autowired。Spring能掃描到AutowiredTypeComponent的bean,然后通過(guò)setter方法注入。示例如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
@Component public class TestClass { private static AutowiredTypeComponent component; @Autowired public void setComponent(AutowiredTypeComponent component){ TestClass.component = component; } // 調(diào)用靜態(tài)組件的方法 public static void testMethod() { component.callTestMethod(); } } |
方式三
定義一個(gè)靜態(tài)組件,定義一個(gè)非靜態(tài)組件并加上@Autowired注解,再定義一個(gè)初始化組件的方法并加上@PostConstruct注解。這個(gè)注解是JavaEE引入的,作用于servlet生命周期的注解,你只需要知道,用它注解的方法在構(gòu)造函數(shù)之后就會(huì)被調(diào)用。示例如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@Component public class TestClass { private static AutowiredTypeComponent component; @Autowired private AutowiredTypeComponent autowiredComponent; @PostConstruct private void beforeInit() { component = this .autowiredComponent; } // 調(diào)用靜態(tài)組件的方法 public static void testMethod() { component.callTestMethod(); } } |
方式四
直接用Spring框架工具類獲取bean,定義成局部變量使用。但有弊端:如果該類中有多個(gè)靜態(tài)方法多次用到這個(gè)組件則每次都要這樣獲取,個(gè)人不推薦這種方式。示例如下:
1
2
3
4
5
6
7
|
public class TestClass { // 調(diào)用靜態(tài)組件的方法 public static void testMethod() { AutowiredTypeComponent component = SpringApplicationContextUtil.getBean( "component" ); component.callTestMethod(); } } |
總結(jié)
在上面的代碼示例中,我每個(gè)類都加了@Component注解,其實(shí)可以根據(jù)需要進(jìn)行變更,比如這個(gè)類是處理業(yè)務(wù)邏輯,可以換成@Service;這個(gè)類是處理請(qǐng)求進(jìn)行轉(zhuǎn)發(fā)或重定向的,可以換成@Controller(是Spring-mvc的注解);這個(gè)類是專門用來(lái)操作Dao的就@Repository。
Spring的注解幫你做了一件很有意義的事:就是它們對(duì)應(yīng)用進(jìn)行了分層,這樣就能將請(qǐng)求處理、業(yè)務(wù)邏輯處理、數(shù)據(jù)庫(kù)操作處理分離出來(lái),為代碼解耦,也方便了項(xiàng)目的開(kāi)發(fā)和維護(hù)。
Spring容器bean加載機(jī)制用到了Java的反射,這里先不作贅述,以后會(huì)專門寫一篇文章來(lái)總結(jié)Java反射在Spring的IoC和AoP中的應(yīng)用。
@Autowired注解和靜態(tài)方法
一、業(yè)務(wù)場(chǎng)景
spring框架應(yīng)用中有些靜態(tài)方法需要依賴被容器管理的類,就像這樣:
1
2
3
4
5
6
7
8
|
@Component public class Test { @Autowired private static UserService userService; public static void test() { userService.test(); } } |
這樣一定會(huì)報(bào)java.lang.NullPointerException: null異常。
二、原理剖析
靜態(tài)變量、類變量不是對(duì)象的屬性,而是一個(gè)類的屬性,所以靜態(tài)方法是屬于類(class)的,普通方法才是屬于實(shí)體對(duì)象(也就是New出來(lái)的對(duì)象)的,spring注入是在容器中實(shí)例化對(duì)象,所以不能使用靜態(tài)方法。
而使用靜態(tài)變量、類變量擴(kuò)大了靜態(tài)方法的使用范圍。靜態(tài)方法在spring是不推薦使用的,依賴注入的主要目的,是讓容器去產(chǎn)生一個(gè)對(duì)象的實(shí)例,然后在整個(gè)生命周期中使用他們,同時(shí)也讓testing工作更加容易。
一旦你使用靜態(tài)方法,就不再需要去產(chǎn)生這個(gè)類的實(shí)例,這會(huì)讓testing變得更加困難,同時(shí)你也不能為一個(gè)給定的類,依靠注入方式去產(chǎn)生多個(gè)具有不同的依賴環(huán)境的實(shí)例,這種static field是隱含共享的,并且是一種global全局狀態(tài),spring同樣不推薦這樣去做。
三、解決方法
1、將@Autowire加到構(gòu)造方法上
1
2
3
4
5
6
7
8
|
@Component public class Test { private static UserService userService; @Autowired public Test(UserService userService) { Test.userService = userService; } } |
2、用@PostConstruct注解
1
2
3
4
5
6
7
8
9
10
|
@Component public class Test { private static UserService userService; @Autowired private UserService userService2; @PostConstruct public void beforeInit() { userService = userService2; } } |
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/RogueFist/article/details/79575665