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

问题记录

阅读更多

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的方式,故没有上述问题。

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

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics