The bean ‘xxx‘ could not be injected as a ‘com.dmo.service.xx‘ because it is a JDK dynamic proxy

原因:springboot 开启了事务支持
1.启动类加了@EnableTransactionManagement
2.service的实现类加了@Transactional

报错:Description:
The bean 'ParkingServiceImpl' could not be injected as a 'com.dmo.parkingview.service.Impl.ParkingServiceImpl' because it is a JDK dynamic proxy that implements:
com.dmo.parkingview.service.ParkingService

描述:我在其他的类中注入了ParkingServiceImpl这个bean 失败了,因为springboot的事务默认是使用jdk的动态代理,即基于接口,所以改为注入ParkingService就可以了。

spring给的解决: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

可以考虑下将action中注入的Bean改成接口方式或者强迫事务使用CGLib代理方式(基于类,即设置proxyTargetClass=True在启动事务管理上@EnableTransactionManagement(proxyTargetClass=True)),这种以CGLib代理方式进行,按照之前写法,我们应该是需要引入相应的cglib库的jar包。而在springboot中已经集成了。但在spring3.2之前是需要引入的;

假如你是在不想将注入的Bean改成接口方式,非得要用实现类,而且还不想再启动事务时配置proxyTargetClass=true,那么,有如下方法:
在你Service层对应的实现类上配置@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)注解,表明此类上所有方法上的事务都是CGLib方式代理的,问题照样可以解决。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值