问题:
The bean 'xxx' could not be injected as a 'com.github.service.xx' because it is a JDK dynamic proxygit
先说说问题的来源吧,当前这个问题是我在springboot配置事务时出现的,原本我搭建了一个springboot的web框架后,启动事务配置只须要以下两步便可完成:
1.在启动类Application类上设置@EnableTransactionManagement,表示启动springboot事务支持
2.在Service层的实现类中指定的方法上设置@Transactional
以上简单的两步便可完成,但恰恰在启动后会报如上错误。经过观察控制台报错的缘由以下:
Console描述1:
Description:github
The bean 'userServiceImpl' could not be injected as a 'com.ysq.springboot.service.Impl.UserServiceImpl' because it is a JDK dynamic proxy that implements:
com.ysq.springboot.service.UserServiceweb
根据上面描述,它是在个人Action层中,注入userServiceImpl这个Bean时失败,(失败的缘由就是我有实现接口,而springboot的事务默认是使用jdk的动态代理,即基于接口))。到这里我忽然明白了,在action层中我注入的Bean是实现类,所以就会报错。因而我将此注入Bean的方式改为了其接口,问题迎刃而解。
Console描述2:
Action:spring
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以前是须要引入的;springboot
假如你是在不想将注入的Bean改为接口方式,非得要用实现类,并且还不想再启动事务时配置proxyTargetClass=true,那么接下来让你知道什么叫厉害,有以下方法:
在你Service层对应的实现类上配置@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)注解,代表此类上全部方法上的事务都是CGLib方式代理的,问题照样能够解决。(参考:https://blog.csdn.net/z394642048/article/details/84759331)框架
写在最后:这里我须要强调一点,就是网上有些解决此问题都说在properties文件中设置spring.aop.proxy-target-class=true。你要搞清楚咱们的问题是在开启springboot事务时出现的哦,虽然和AOP代理过程产生的问题同样,但解决的方式不一样额。ide
参考文献:
https://github.com/alibaba/druid/issues/2330
https://blog.csdn.net/m0_38016299/article/details/78390363ui