Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy22]

本文探讨了 Spring AOP 中配置 CGLIB 代理的问题,特别是当使用 <aop:aspectj-autoproxy proxy-target-class=true/> 时遇到的错误。文中详细解释了 CGLIB 和 JDK 动态代理的区别,以及如何正确配置避免出现不能为 final 类生成子类的异常。

<aop:aspectj-autoproxy proxy-target-class="true"/>,加上红色部分

报错内容如下:
Error creating bean with name 'userMapper': Post-processing of the FactoryBean's object failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy22]: 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.$Proxy22


<tx:annotation-driven transaction-manager="transactionManager" 
                                       proxy-target-class="true"/>
  注意:proxy-target-class属性值决定是基于接口的还是基于类的代理被创建。如果proxy-target-class 属性值被设置为true,那么基于类的代理将起作用(这时需要cglib库)。如果proxy-target-class属值被设置为false或者这个属性被省略,那么标准的JDK 基于接口的代理将起作用。

即使你未声明 proxy-target-class="true" ,但运行类没有继承接口,spring也会自动使用CGLIB代理。

高版本spring自动根据运行类选择 JDK 或 CGLIB 代理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值