The bean ‘xxx’ could not be injected as a ‘xxxMapper’ because it is a JDK dynamic proxy that implements: com.baomidou.mybatisplus.core.mapper.BaseMapper
一、发现问题
Spring框架启动注入时发现报错信息:
Description:
The bean ‘busKpiMapper’ could not be injected as a ‘com.lnsoft.sdhxgk.asset.device.life.mapper.DlpBusKpiMapper’ 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.
二、问题原因及解决办法
该问题是由于有两个busKpiMapper的bean重名了,且恰好使用了mybatisplus代理出现的问题,只需要把其中一个bean的name属性修改即可
解决办法1
(1)使用@Repository注解,注解有value属性用来设置bean名称,修改mapper层的bean名称
(2)到注入该mapper对象的实现类,使用@Resource注解并设置其name属性,让bean注入按照名称注入即可解决问题
解决办法2
Mapper层与解决方法1保持不变,impl实现类注入bean时可以使用@Autowired和@Qualifier两个注解配合使用,仍然是根据bean名称注入即可
三、总结
导致出现该问题的原因是因为spring注入bean时发现有两个相同的bean名称,导致spring不知道要注入哪一个,进而导致报错,那我们只需要将两个名字相同的bean名称修改,并且根据名称指定注入所需要的bean即可!