springboot开启事务支持时报代理错误

问题:
The bean 'xxx' could not be injected as a 'com.github.service.xx' because it is a JDK dynamic proxy

先说说问题的来源吧,当前这个问题是我在springboot配置事务时出现的,本来我搭建了一个springboot的web框架后,启动事务配置只需要如下两步即可完成:
1.在启动类Application类上设置@EnableTransactionManagement,表示启动springboot事务支持
2.在Service层的实现类中指定的方法上设置@Transactional
以上简单的两步即可完成,但偏偏在启动后会报如上错误。通过观察控制台报错的原因如下:
Console描述1:
Description:

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.UserService

根据上面描述,它是在我的Action层中,注入userServiceImpl这个Bean时失败,(失败的原因就是我有实现接口,而springboot的事务默认是使用jdk的动态代理,即基于接口))。到这里我突然明白了,在action层中我注入的Bean是实现类,因此就会报错。于是我将此注入Bean的方式改成了其接口,问题迎刃而解。

posted @ 2020-12-22 13:01  浅笑19  阅读(385)  评论(0编辑  收藏  举报