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

来源:互联网 发布:k8网络分销平台 编辑:程序博客网 时间:2024/05/23 23:45

使用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/




阅读全文
0 0
原创粉丝点击