关于问题:The bean 'XXX' could not be injected as a 'XXX' because it is a JDK dynamic proxy that implements
问题描述解决过程思考问题描述
今天在写代码的时候,新增了一个功能模块,写完代码之后,启动项目无法启动,报出了这么一行的错误:
***************************APPLICATION FAILED TO START***************************Description:The bean 'contractModifyMapper' could not be injected as a 'com.dophy.dophyerpadmin.module.contract.mapper.SalesContractModifyMapper' because it is a JDK dynamic proxy that implements:com.baomidou.mybatisplus.core.mapper.BaseMapperAction: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.
解决过程
字面意思看应该是注入的错误,就在排查相关的注入,排查了一遍又一遍之后,并没有发现问题,之后就百度,发现了如下的解决方法:
在启动类中加上@EnableTransactionManagement(proxyTargetClass = true)
按照他们的说法,这是事务的问题,但是我明明没有动事务,再者之前系统也没有报相关的错误,所以即使这样可以解决问题,我也还是不明白其中的原理。
之后我就继续找相关的原因,最后终于找到了这一段解释:
多半是使用@Resource注解导致的问题
因为@Autowired默认按类型装配,而 @Resource优先按名称装配,如果使用
@Resource
private XabYcdService ycdSerivce;
恰巧有另外一个bean叫“ycdSerivce” 也可能出现这个错误。
看到这里恍然大悟,原来是@Resource导致的错误,之后就进行了相关的排查,果然找到了相关的错误。