您的位置:首页 > 编程语言 > Java开发

spring-boot 事务异常: because it is a JDK dynamic proxy that implement

2017-08-01 09:40 1621 查看
使用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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐