2011-07-06 43 views
0

MyController@Autowired工作正常myService拉不getter/setter方法:爲什麼@Autowired在我的控制器中工作,但不在我的服務中?

@Controller 
public class MyController 
{ 
    @Autowired 
    private MyService myService; 

但是當我嘗試到@Autowired標註適用於MyServicemyOtherService場,我得到一個錯誤說,它不能找到myOtherService必要的setter方法 - 但它的工作原理,如果我填的getter和setter方法對這一領域:

這個工程:

@Service 
public class MyService 
{ 
    private MyOtherService myOtherService; 

    public void setMyOtherService(MyOtherService myOtherService) 
    { 
     this.myOtherService = myOtherService; 
    } 

    public MyOtherService getMyOtherService() 
    { 
     return myOtherService; 
    } 

這不起作用:

@Service 
public class MyService 
{ 
    @Autowired 
    private MyOtherService myOtherService; 

是否@Autowired只在控制器工作?

+1

嗯......在你的例子中'MyService'中沒有'@ Autowired'。 – skaffman

+0

這是正確的。上面顯示的是我通過刪除'@ Autowired'註釋並創建適當的getter和setter方法來獲得它的狀態。對不起,如果不明確。 – Pizzicata

+0

告訴我們什麼不行,而不是做什麼。 – skaffman

回答

3

你給出了你的答案 - 你沒有<context:component-scan />這個服務包。如果你添加它,你會有註釋自動裝配

相關問題