天天看点

The bean ‘xxx‘ could not be injected as a ‘xxx‘ because it is a JDK dynamic proxy that implements:

这种错误不仅在mapper(dao)层,在service层也会出现~

我当时在本地跑的时候一点问题都没得有,因为我的这个项目我只拉下来一个模块,并没有把其他模块拉下来,结果在提交代码往测试环境上推了后项目直接起不来,并报了如下错误~

错误信息:

Description:

The bean 'attachMapper' could not be injected as a 'org.macrocloud.modules.sys.mapper.ProcessInspectionAttachMapper' because it is a JDK dynamic proxy that implements:
	com.baomidou.mybatisplus.core.mapper.BaseMapper


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.           

问题代码:

@Service
@Slf4j
public class BtProcessInspectionServiceImpl extends ServiceImpl<BtProcessInspectionMapper, BtProcessInspectionEntity> implements IBtProcessInspectionService {

    @Autowired
    private ISysClient iSysClient;

    @Resource
    private BtProcessInspectionAttachMapper attachMapper;

    @Resource
    private ProcessResourceMapper resourceMapper;

    @Resource
    private ResourceInfoMapper infoMapper;           

原因:

根本原因是因为什么呢?

@Resource 是根据名字找对象,谁的名字呢?

它有个寻找的优先级,先找与被注解的相同的 变量 的名,如果又就直接使用,如果没有就会根据类的名字找,如果项目中恰好存在相同名称的变量名,那么在被注解拿来用时就会发现存在问题并报以上错误(这也是为什么在我本地的时候代码不报错,合并到测试环境后出现问题的原因,因为我本的变量较少,没有引发此问题,测试环境代码模块较多,问题就很容易浮现出来了)。

继续阅读