0
在MyController
,@Autowired
工作正常myService
拉不getter/setter方法:爲什麼@Autowired在我的控制器中工作,但不在我的服務中?
@Controller
public class MyController
{
@Autowired
private MyService myService;
但是當我嘗試到@Autowired
標註適用於MyService
的myOtherService
場,我得到一個錯誤說,它不能找到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
只在控制器工作?
嗯......在你的例子中'MyService'中沒有'@ Autowired'。 – skaffman
這是正確的。上面顯示的是我通過刪除'@ Autowired'註釋並創建適當的getter和setter方法來獲得它的狀態。對不起,如果不明確。 – Pizzicata
告訴我們什麼不行,而不是做什麼。 – skaffman