
Spring Team recommends "Always use constructor based dependency injection in your beans. Always use assertions for mandatory dependencies".
翻譯:
Spring建議”總是在您的bean中使用構造函數建立依賴注入。總是使用斷言強制依賴”。
這段代碼警告原來的寫法是:
@Autowired
private EnterpriseDbService service;
建議后寫成下面的樣子:
private final EnterpriseDbService service;
@Autowired
public EnterpriseDbController(EnterpriseDbService service) {
this.service = service;
}
奇怪,為何會有這樣的建議。
我們知道:@Autowired 可以對成員變量、方法以及構造函數進行注釋。那么對成員變量和構造函數進行注釋又有什么區別呢?
@Autowired注入bean,相當於在配置文件中配置bean,並且使用setter注入。而對構造函數進行注釋,就相當於是使用構造函數進行依賴注入了吧。莫非是這兩種注入方法的不同。
以下是:@Autowired和構造方法執行的順序解析
先看一段代碼,下面的代碼能運行成功嗎?
1 @Autowired 2 private User user; 3 private String school; 4 5 public UserAccountServiceImpl(){ 6 this.school = user.getSchool(); 7 }
答案是不能。
因為Java類會先執行構造方法,然后再給注解了@Autowired 的user注入值,所以在執行構造方法的時候,就會報錯。
報錯信息可能會像下面:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name '...' defined in file [....class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [...]: Constructor threw exception; nested exception is java.lang.NullPointerException
報錯信息說:創建Bean時出錯,出錯原因是實例化bean失敗,因為bean時構造方法出錯,在構造方法里拋出了空指針異常。
解決辦法是,使用構造器注入,如下:
1 private User user; 2 private String school; 3 4 @Autowired 5 public UserAccountServiceImpl(User user){ 6 this.user = user; 7 this.school = user.getSchool(); 8 }
可以看出,使用構造器注入的方法,可以明確成員變量的加載順序。
PS:Java變量的初始化順序為:靜態變量或靜態語句塊–>實例變量或初始化語句塊–>構造方法–>@Autowired
轉載來源:http://blog.csdn.net/ruangong1203/article/details/50992147