admin管理员组文章数量:1636897
使用spring-boot做事务管理时,出现异常:The bean 'xxx' could not be injected as a 'xx.xxxx' because it is a JDK dynamic proxy that implements:
搞了半天发现是因为代理的原因;
异常信息:
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-08-01 09:37:14.845 ERROR 12264 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'AdapterSchemeVersionService' could not be injected as a 'com.yihu.hos.rest.services.standard.adapter.AdapterSchemeVersionService' because it is a JDK dynamic proxy that implements:
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.
使用 @Transactional
开启@Transactional 注解支持,两种方式,一种是通过xml的方式(如下)
<tx:annotation-driven transaction-manager="txManager"/><!-- a PlatformTransactionManager is still required -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- (this dependency is defined somewhere else) -->
<property name="dataSource" ref="dataSource"/>
</bean>
@EnableTransactionManagement
,将该注解标注在
@Configuration
类上,等价于上面的
<tx:annotation-driven/>
Spring推荐奖该注解标记在类(方法)而不是接口,将注解标记在接口上时,只有使用动态代理的时候才会生效,需要标记
proxy-target-class="true"
或者
mode="aspectj",如下:
@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
@ComponentScan("com.yihu.hos.rest")
public class BeanConfiguration {}
@Transaction
的类会自动开启动态代理,java的代理机制主要有
JDK动态代理
和
CGLIB
,报上面的错误是因为使用了JDK动态代理机制,我尝试开启@Transaction设置@EnableTransactionManagement(proxyTargetClass = true)
,问题解决;特此记录一下;
参考文章:http://hrps.me/2016/11/03/spring-transaction/
版权声明:本文标题:spring-boot 事务异常: because it is a JDK dynamic proxy that implement 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1729234396a1191826.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论