问题描述

项目启动时报错,提示无法注入 bean,需要设置在  @EnableAsync  或  @EnableCaching  上配置属性 proxyTargetClass=true 强行使用 CGLib 代理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'invoiceMapper' could not be injected as a 'com.xxx.yyyMapper' 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.


Process finished with exit code 1

原因分析

这是在注入 bean invoiceMapper 时使用了  @Resource  而不是 spring 的  @Autowired,前者通过名称来匹配对象,先根据注入 bean 的变量名来查找,找不到则根据类名查找,若遇到同名对象但类型不一致时,就会出现注入失败的问题。

解决方案

在注入变量时,将变量名写作类名小写形式的变量,尽量完整,若仍然有同名现象,可重新指定 bean 的名称。

1
2
@Resource
private AbcxxxopqService abcxxxopqService;

评论