原因: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方式代理的,问题照样可以解决。