我現在正在使用Spring,因爲幾個月後,我認爲使用@Autowired註釋進行依賴注入也需要一個setter來注入該字段。Spring依賴注入@Autowired without setter
所以,我使用它是這樣的:
@Controller
public class MyController {
@Autowired
MyService injectedService;
public void setMyService(MyService injectedService) {
this.injectedService = injectedService;
}
...
}
但我已經試過這今天:
@Controller
public class MyController {
@Autowired
MyService injectedService;
...
}
噢驚喜,沒有編譯錯誤,啓動時無錯誤,應用程序運行完美...
所以我的問題是,使用@Autowired註釋進行依賴注入需要setter嗎?
我使用Spring 3.1.1。
似乎你已經回答了你自己的問題。 – darrengorman 2012-04-13 13:02:10