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;

 

posted @ 2018-03-28 15:41  Romic的博客园  阅读(11036)  评论(0编辑  收藏  举报