天天看點

Idea中 裝配對象時(@Autowired) 出現 Could not autowire. no beans of "XXX" type found

問題場景,我在service層的實作類中  進行裝配dao層對象時,提示Could not autowired. no beans of "XXX" type found.

沒有裝配到,肯定不行啊,有問題。如圖:

Idea中 裝配對象時(@Autowired) 出現 Could not autowire. no beans of "XXX" type found

有的大牛說這個idea的糾錯機制對spring的某些配置沒有很好的支援。

可以不用管,但是程式員一般都是有強迫症的,還是想要解決掉它。

網上常見的兩個方向,我簡單提一下。想要詳細了解的csdn搜一大堆。

第一種

降低Autowired的檢測級别,将Serverity的級别由之前的error改成warning或者其他可以忽略的級别。如圖:

Idea中 裝配對象時(@Autowired) 出現 Could not autowire. no beans of "XXX" type found

第二種

可能是你加@Service注解時,導入包有誤。

正确的包是:

org.springframework.stereotype.Service      

最後說一下我的終極解決辦法:那就是在dao層的類上加@Repository注解,就ok了。在去看dao層,就沒有那個提示了。

該注解時從spring2.0開始增加的注解,用于簡化spring的開發,将資料通路層的類辨別為SpringBean。