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;