后台springboot启动失败-The bean 'productMapper' could not be injected because it is a JDK dynamic proxy

发布时间 2023-11-01 17:29:54作者: 汤圆aiWL

 报错信息

The bean 'productMapper' could not be injected because it is a JDK dynamic proxy

The bean is of type 'com.sun.proxy.$Proxy224' and implements:
pwc.taxtech.dal.admin.mapper.ProductMapper
org.springframework.aop.SpringProxy
org.springframework.aop.framework.Advised
org.springframework.core.DecoratingProxy

Expected a bean of type 'pwc.taxtech.dal.admin.mapper.TpProductMapper' which implements:
pwc.taxtech.common.mybatis.BaseMapper

  

 

 本人原因是 有相同名称的 mapper 同名:

 

下面二个注入的mapper  实例名称 相同
@Resource private TpProductMapper productMapper; @Resource private ProductMapper productMapper; 改为 @Resource private ProductMapper xxxXproductMapper;
修改掉其中一个mapper的名称就行了

 

 

第二种: 

在Spring框架中,有两种类型的代理:接口代理和类代理。

接口代理是通过实现接口来实现的,类代理是基于目标类的继承或代理实现的。默认情况下,Spring使用JDK动态代理实现接口代理,使用CGLIB动态代理实现类代理。

你遇到的问题可能是因为Spring默认使用JDK动态代理,而JDK动态代理只能基于接口进行代理,所以如果你正在使用类级别的@EnableAsync和/或@EnableCaching,那么你需要强制Spring使用CGLIB动态代理。

解决方案是在@EnableAsync和/或@EnableCaching上设置proxyTargetClass=true。这会强制Spring使用CGLIB动态代理。

@Configuration  
@EnableAsync(proxyTargetClass = true)  
@EnableCaching(proxyTargetClass = true)  
public class SpringConfig {  
    // your configuration here  
}

  

 

第三种注意情况:

  1. TpProductMapper 类不存在或者路径错误。请确保你已经创建了 TpProductMapper 类,并且它位于正确的包路径下(pwc.taxtech.dal.admin.mapper)。

  2. TpProductMapper 类没有被Spring扫描到。你需要确保你的Spring配置正确,且Spring能够扫描到 TpProductMapper 类。你可以在你的Spring配置文件中加入 @ComponentScan("pwc.taxtech.dal.admin.mapper"),以确保Spring能够扫描到这个包。

  3. 你可能没有为 TpProductMapper 类添加正确的注解。如果你使用的是MyBatis,那么你可能需要为 TpProductMapper 类添加 @Mapper 或者 @MapperScan 注解。例如:

@Mapper  
public interface TpProductMapper {  
    // your methods here  
}


或者在Spring的配置文件中添加:
注意下面 namespace 路径
 <mapper namespace="pwc.taxtech.dal.admin.mapper.TpProductMapper">  
    <!-- your mapper methods here -->  
</mapper>

你也可能没有正确配置MyBatis与Spring的集成。你需要确保你已经正确配置了MyBatis与Spring的集成,这样Spring才能管理并注入你的Mapper。这通常涉及到在Spring配置文件中添加MyBatis的SqlSessionFactoryBean。