使用@EnableTransactionManagement报错
创始人
2025-05-29 09:05:22
0次

记录一下报错:

今天想做事务控制,在启动器类上加了**@EnableTransactionManagemen**t来开启事务,并在service层方法上加上了 @Transactional(rollbackFor = Exception.class),如何启动启动器类,就报如下错误了,第一次碰到
版本:

 org.springframework.bootspring-boot-starter-parent2.2.7.RELEASE 

在这里插入图片描述

2023-03-16 15:13:56.330  WARN 1244 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'excelDemo': Unsatisfied dependency expressed through field 'resoucefileService'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'resoucefileServiceImpl' is expected to be of type 'com.example.demo.service.impl.ResoucefileServiceImpl' but was actually of type 'com.sun.proxy.$Proxy96'
2023-03-16 15:13:56.333  INFO 1244 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2023-03-16 15:13:56.343  INFO 1244 --- [           main] ConditionEvaluationReportLoggingListener : Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-03-16 15:13:56.346 ERROR 1244 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : ***************************
APPLICATION FAILED TO START
***************************Description:The bean 'resoucefileServiceImpl' could not be injected as a 'com.example.demo.service.impl.ResoucefileServiceImpl' because it is a JDK dynamic proxy that implements:com.example.demo.service.IResoucefileServiceAction: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.Process finished with exit code 1

翻译一下:

上下文初始化期间遇到异常 -
取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependency异常:创建名为“excelDemo”的Bean时出错:通过字段“resoucefileService”表示的不满意依赖项;嵌套异常是org.springframework.beans.factory.BeanNotOfRequiredTypeException:名为“resoucefileServiceImpl”的Bean应该属于“com.example.demo.service.impl.ResoucefileServiceImpl”类型,但实际上“com.sun.proxy.$Proxy 96”类型。

描述:
bean ‘resoucefileServiceImpl’ 无法作为’com.example.demo.service.impl.ResoucefileServiceImpl’ 注入,因为它是一个 JDK动态代理,它实现了: com.example.demo.service.IResoucefileService

行动:

考虑将 Bean 注入为其接口之一,或者通过在@EnableAsync和/或@EnableCaching上设置
proxyTargetClass=true 来强制使用基于 CGLib 的代理。

解决方法(强制使用CGLIB的代理)
(一)在这里插入图片描述
(二)
在这里插入图片描述

原因分析

我用的是mybatisPlus框架,mybatis底层是基于jdk的代理方式
查看报错,可知我的service层是基于jdk动态代理生成的。
在这里插入图片描述

而springboot2.o则默认是基于cglib的代理方式,而我在方法上用**@Transactional(rollbackFor = Exception.class)**注解,该注解的目标主要是用于控制数据库事务,那么它的内部必须通过AOP切面的方法去实现事务控制,所以会用到动态代理,而且是采用CGLib。

这样就会产生冲突,所以需要强制Springboot使用cglib的代理方式来生成。

相关内容

热门资讯

一分钟了解透视器!清风手游透视... 一分钟了解透视器!清风手游透视器AI技巧,清风手游多功能竟然是有透视器技巧1、在清风手游插件功能辅助...
实测分享开挂!冰球突破开挂AI... 实测分享开挂!冰球突破开挂AI插件,冰球突破多功能切实有开挂插件1、超多福利:超高返利,海量正版游戏...
专业讨论辅助!老牌人辅助AI技... 专业讨论辅助!老牌人辅助AI技巧,老牌人多功能一贯真的有辅助技巧1、用户打开应用后不用登录就可以直接...
重大通报作弊器!朱雀大厅作弊器... 重大通报作弊器!朱雀大厅作弊器AI软件,朱雀大厅多功能切实真的是有作弊器软件1、打开软件启动之后找到...
玩家必用作弊器!兴动游戏作弊器... 玩家必用作弊器!兴动游戏作弊器AI教程,兴动游戏多功能果然是有作弊器教程1、玩家必用作弊器!兴动游戏...
一分钟了解开挂!鸿福休闲开挂A... 一分钟了解开挂!鸿福休闲开挂AI教程,鸿福休闲多功能原来存在有开挂教程1、这是跨平台的鸿福休闲轻量版...
一分钟揭秘作弊器!新老夫子作弊... 一分钟揭秘作弊器!新老夫子作弊器AI技巧,新老夫子多功能一直是有作弊器技巧亲,关键说明,新老夫子透视...
专业讨论外挂!掼蛋外挂AI方法... 专业讨论外挂!掼蛋外挂AI方法,掼蛋多功能原来存在有外挂方法1)掼蛋辅助插件:进一步探索掼蛋辅助大陆...
科普透视器!MG棋牌透视器AI... 您好,MG棋牌这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款...
科普辅助!微乐广西麻将辅助AI... 科普辅助!微乐广西麻将辅助AI软件,微乐广西麻将多功能好像真的是有辅助软件1、完成微乐广西麻将有辅助...