spring-boot 啟動時候 出現異常:The bean 'xxx' could not be injected as a 'xx.xxxx' because it is a JDK dynamic proxy that implements:


The bean 'xxxService' could not be injected as a 'AaaXxxService' because it is a JDK dynamic proxy that implements: 


Action:  
  
Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on @EnableAsync and/or @EnableCaching.

查了半天,原來是因為把 @Autowired 換成了 @Resource

以前的代碼是(是可以的)

@Autowired
private AaaXxxService xxxService;

修改后的代碼(不可以的)

@Resource
private AaaXxxService xxxService;

這樣就報錯了。。

主要問題是名字和類名不一樣導致 Resource 注入失敗,但是剛好又有一個 xxxService 同名的類存在,就會報這個錯誤

這里要說一下,@Resource 是根據名字找對象,名字是什么的名字呢?

先根據變量名字找,如果找到就直接使用,如果找不到就才根據類的名字找。

這個時候剛好有一個同名的對象存在,直接返回賦值,但是這個時候類型和對象是不一致的,所以就報異常。

解決方法就是 名字改成和類一樣

@Resource
private AaaXxxService aaaXxxService;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM