问题记录

Case:spring-mybatis配置事务的时候报错,错误如下:

 

Caused by: java.lang.IllegalArgumentException: Cannot subclass final class class com.sun.proxy.$Proxy8
	at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:446)
	at net.sf.cglib.transform.TransformingClassGenerator.generateClass(TransformingClassGenerator.java:33)
	at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
	at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
	at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
	at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:285)
	at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:201)
	... 37 more

 

[org.apache.ibatis.session.defaults.DefaultSqlSession@c5577c] was not registered for synchronization because synchronization is not active 
2014-05-09 17:47:26 [org.springframework.jdbc.datasource.DataSourceUtils]-[DEBUG] Fetching JDBC Connection from DataSource 
2014-05-09 17:47:26 [org.mybatis.spring.transaction.SpringManagedTransaction]-[DEBUG] JDBC Connection [32681756(oracle.jdbc.driver.OracleConnection@1664a9b)] will not be managed by Spring

 

 

解决过程:

 

查看配置,发现 proxy-target-class="true"导致问题,此配置加上后会导致spring默认全部使用CGLIB进行代理,使用CGLIB的时候需要考虑一下问题:

 

Scanning for mappers1.final methods cannot be advised, as they cannot be overridden.
2.As of Spring 3.2, it is no longer necessary to add CGLIB to your project classpath, as CGLIB classes are repackaged under org.springframework and included directly in the spring-core JAR. This means that CGLIB-based proxy support 'just works' in the same way that JDK dynamic proxies always have.
3.The constructor of your proxied object will be called twice. This is a natural consequence of the CGLIB proxy model whereby a subclass is generated for each proxied object. For each proxied instance, two objects are created: the actual proxied object and an instance of the subclass that implements the advice. This behavior is not exhibited when using JDK proxies. Usually, calling the constructor of the proxied type twice, is not an issue, as there are usually only assignments taking place and no real logic is implemented in the constructor.

 由于Mybatis-spring中支持使用org.mybatis.spring.SqlSessionFactoryBean或者Scanning for mappers(<mybatis:scan/>、@MapperScan 、MapperScannerConfigurer)配置Mapper接口bean对象,每个Mapper的实现类似由框架自动创建final class $Proxy,故会出现上述问题JDK dynamic proxies使用组合的target object的方式,故没有上述问题。

上述的第二个问题,仔细检查客户端配置,一般是配置问题。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值