`
flashdream8
  • 浏览: 670320 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring 事务代理 $Proxy0 cannot be cast t.

 
阅读更多

          在使用Spring事务管理时,肯定有很多人都会遇到这样的异常:java.lang.ClassCastException: $Proxy0 cannot be cast t.这个问题的解决办法有两种。

        Spring的文档中这么写的:Spring AOP部分使用JDK动态代理或者CGLIB来为目标对象创建代理。如果被代理的目标实现了至少一个接口,则会使用JDK动态代理。所有该目标类型实现的接口都将被代理。若该目标对象没有实现任何接口,则创建一个CGLIB代理。

        所以,解决办法是,如果用JDK动态代理,就必须为被代理的目标实现一个接口(要注意的地方是:需要将ctx.getBean()方法的返回值用接口类型接收);如果使用CGLIB强制代理,就必选事先将CGLIB包导入项目,设置beanNameAutoProxyCreator的proxyTargetClass属性为true。

 

<aop:config proxy-target-class="true">
		<aop:pointcut id="fooServiceOperation" expression="execution(* com.xxx.service.*.*ServiceImpl.*(..))"  />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation" />
	</aop:config>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics