Cannot subclass final class class com.sun.proxy.$Proxy

简介:

背景

这个错误是我在使用AOP动态切换数据库,实现数据库的读写分离的时候出现的问题,使用到的系统环境是:

<spring.version>3.2.6.RELEASE</spring.version> <mybatis.version>3.2.4</mybatis.version> <mybatis-spring.version>1.1.1</mybatis-spring.version>

使用的代码

执行切点的代码是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.xuliugen.choosedb.demo.aspect;
 
import  org.apache.commons.lang3.StringUtils;
import  org.aspectj.lang.JoinPoint;
import  org.aspectj.lang.annotation.Aspect;
import  org.aspectj.lang.annotation.Before;
import  org.aspectj.lang.annotation.Pointcut;
import  org.slf4j.Logger;
import  org.slf4j.LoggerFactory;
import  org.springframework.context.annotation.EnableAspectJAutoProxy;
import  org.springframework.stereotype.Component;
 
/**
  * 切换数据源(不同方法调用不同数据源)
  */
@Aspect
@Component
@EnableAspectJAutoProxy(proxyTargetClass =  true )
public class DataSourceAspect {
 
     protected Logger logger = LoggerFactory.getLogger(this.getClass());
 
     // 这个包是存放MyBatis的sql的包
     @Pointcut( "execution(* com.xuliugen.choosedb.demo.mybatis.dao.*.*(..))" )
     public void aspect() {
     }
 
     /**
      * 配置前置通知,使用在方法aspect()上注册的切入点
      */
     @Before( "aspect()" )
     public void before(JoinPoint point) {
         String className = point.getTarget().getClass().getName();
         String method = point.getSignature().getName();
         logger.info(className +  "."  + method +  "("  + StringUtils. join (point.getArgs(),  "," ) +  ")" );
         try {
             for  (String key : ChooseDataSource.METHOD_TYPE_MAP.keySet()) {
                 for  (String  type  : ChooseDataSource.METHOD_TYPE_MAP.get(key)) {
                     if  (method.startsWith( type )) {
                         DataSourceHandler.putDataSource(key);
                     }
                 }
             }
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
}

 

代码的整体意思就说获取配置文件中的读、写数据库和进行Aop的方法(在下边的配置文件中可以看到),DataSourceHandler是一个存放数据源的Handler。

配置文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!-- 配置动态分配的读写 数据源 -->
     <bean  id = "dataSource"  class= "com.xuliugen.choosedb.demo.aspect.ChooseDataSource"  lazy-init= "true" >
         <property name= "targetDataSources" >
             <map key- type = "java.lang.String"  value- type = "javax.sql.DataSource" >
                 <!-- write -->
                 <entry key= "write"  value-ref= "writeDataSource" />
                 <!--  read  -->
                 <entry key= "read"  value-ref= "readDataSource" />
             < /map >
         < /property >
         <property name= "defaultTargetDataSource"  ref= "writeDataSource" />
         <property name= "methodType" >
             <map key- type = "java.lang.String" >
                 <!--  read  -->
                 <entry key= "read"  value= ",get,select,count,list,query" />
                 <!-- write -->
                 <entry key= "write"  value= ",add,create,update,delete,remove," />
             < /map >
         < /property >
     < /bean >
// 这里省去读写数据源的配置

  

运行的错误信息

这里写图片描述

这里写图片描述

主要错误信息如下:

Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy16]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class com.sun.proxy.$Proxy16 at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:217) at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:111) at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:477) at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:362) at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:322) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:409) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.postProcessObjectFromFactoryBean(AbstractAutowireCapableBeanFactory.java:1655) at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:162) ... 70 more Caused by: java.lang.IllegalArgumentException: Cannot subclass final class class com.sun.proxy.$Proxy16 at org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:446) at org.springframework.cglib.transform.TransformingClassGenerator.generateClass(TransformingClassGenerator.java:33) at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216) at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:377) at org.springframework.cglib.proxy.Enhancer.create(Enhancer.java:285) at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:205) ... 77 more

解决的过程:

Cannot subclass final class class com.sun.proxy.$Proxy16

 

这句错误的原因很好理解,就是一个final 不可以被继承了,即final类不能子类化,因此不能代理proxy。

http://stackoverflow.com/上的一个回答,

You are not injecting an interface so you need to use CGLIB proxies, the spring reference manual states:

Spring AOP defaults to using standard J2SE dynamic proxies for AOP proxies. This enables any interface (or set of interfaces) to be proxied. Spring AOP can also use CGLIB proxies. This is necessary to proxy classes, rather than interfaces. CGLIB is used by default if a business object does not implement an interface. As it is good practice to program to interfaces rather than classes, business classes normally will implement one or more business interfaces. Spring has decided to use a J2SE proxy (com.sun.proxy.$Proxy57) probably because CrudService implements an interface. To force the use of CGLIB you can tweak your XML: <aop:aspectj-autoproxy proxy-target-class="true"/>

spring AOP can also use CGLIB proxies. This is necessary to proxy classes, rather than interfaces. 可以看出,在默认的情况下如果一个业务类没有继承接口的话是会使用CGLIB 的代理方式。CGLib是不能代理final类,或代理被final,private修饰的方法,cglib面对具体类代理,不能是接口。jdk的代理面向的是接口代理。因此如果你的业务类中没有使用到接口,而是直接使用类的方式,那么在进行 @Autowired或者@Inject的时候会出现错误。

两种的区别:Java动态代理是利用反射机制生成一个实现代理接口的匿名类,在调用具体方法前调用InvokeHandler来处理。而cglib动态代理是利用asm开源包,对代理对象类的class文件加载进来,通过修改其字节码生成子类来处理。

1、如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP 
2、如果目标对象实现了接口,可以强制使用CGLIB实现AOP 
3、如果目标对象没有实现了接口,必须采用CGLIB库,spring会自动在JDK动态代理和CGLIB之间转换

如何强制使用CGLIB实现AOP? 
* 在spring配置文件中加入

JDK动态代理和CGLIB字节码生成的区别? 
* JDK动态代理只能对实现了接口的类生成代理,而不能针对类 
* CGLIB是针对类实现代理,主要是对指定的类生成一个子类,覆盖其中的方法 
因为是继承,所以该类或方法最好不要声明成final


关于Spring的AOP代理方式,详细可以参考:http://blog.csdn.net/caomiao2006/article/details/51295158

http://blog.csdn.net/caomiao2006/article/details/51297443


他这里提到的解决方式在配置文件中加入:

<aop:aspectj-autoproxy proxy-target-class="true"/>

可以看出我在DataSourceAspect 类上已经加入@EnableAspectJAutoProxy(proxyTargetClass = true) 的注解,效果是一样的的,因此这种方式不符合我遇到的问题。

因此,如果你在配置文件中进行了配置(<aop:aspectj-autoproxy proxy-target-class="true"/>),并且按照了Spring AOP 提供的代理方式,那么,这种方式是不可以解决的。

切换Spring版本解决问题

可以看出,我使用到的版本是3.2.6.RELEASE,查找相关资料发现,从Spring3.2以后,spring框架本身不在需要cglib这个jar包了,因为cjlib.jar已经被spring项目的jar包集成进去。为了防止项目中其他对cglib版本依赖不一样的冲突。

根据这个,想到了切换版本,然后将Spring的版本切换到了4.2.5.RELEASE 再次测试,正常运行,错误不见了。

可以初步的得到是由于版本的问题造成了这个错误的出现,因此对于这个问题可以参考上述的两种解决方式去解决实际的问题,希望能够对你的问题有所帮助。

 本文转自二郎三郎博客园博客,原文链接:http://www.cnblogs.com/haore147/p/7212551.html,如需转载请自行联系原作者

相关文章
|
6月前
|
Java 数据库 Spring
【异常】because it is a JDK dynamic proxy that implements
【异常】because it is a JDK dynamic proxy that implements
84 0
|
1月前
|
网络安全
ssh报错java.lang.ClassCastException: com.sun.proxy.$Proxy6 cannot be cast to org.service.impl.EmpServi
ssh报错java.lang.ClassCastException: com.sun.proxy.$Proxy6 cannot be cast to org.service.impl.EmpServi
10 1
|
Nacos
解决:java.lang.IllegalStateException: Invalid host: lb://exam_farmer_service
解决:java.lang.IllegalStateException: Invalid host: lb://exam_farmer_service
297 0
解决:java.lang.IllegalStateException: Invalid host: lb://exam_farmer_service
jdk动态代理:ClassCastException: com.sun.proxy.$Proxy4 cannot be cast to com.cx.bank.dao.BankDaoImpl
jdk动态代理:ClassCastException: com.sun.proxy.$Proxy4 cannot be cast to com.cx.bank.dao.BankDaoImpl
jdk动态代理:ClassCastException: com.sun.proxy.$Proxy4 cannot be cast to com.cx.bank.dao.BankDaoImpl
|
存储 JavaScript 前端开发
Proxy-Reflect
JS查漏补缺系列是我在学习JS高级语法时做的笔记,通过实践费曼学习法进一步加深自己对其的理解,也希望别人能通过我的笔记能学习到相关的知识点。这一次我们来了解Proxy-Reflect
93 0
Java中访问修饰符public、private、protect、default范围
Java中访问修饰符public、private、protect、default范围
|
Java 编译器
Java 9 Private Interface Methods
Java 9 Private Interface Methods
|
Java Android开发
JAVA interface报错:abstract methods do not specify a body
JAVA interface报错:abstract methods do not specify a body
297 0
|
SQL XML 存储
源码分析 There is no getter for property named '*' in 'class java.lang.String(2)
源码分析 There is no getter for property named '*' in 'class java.lang.String
210 0
源码分析 There is no getter for property named '*' in 'class java.lang.String(2)
|
SQL Java 测试技术
源码分析 There is no getter for property named '*' in 'class java.lang.String(1)
源码分析 There is no getter for property named '*' in 'class java.lang.String
434 0
源码分析 There is no getter for property named '*' in 'class java.lang.String(1)