加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java – 运行Spring单元测试的AOP问题

发布时间:2020-12-15 01:50:40 所属栏目:大数据 来源:网络整理
导读:我有一个Spring Web应用程序,配置为使用JDK代理AOP. AOP注释(例如@Transactional)在接口上声明,而不是实现类. 应用程序本身工作正常,但是当我运行单元测试时,似乎试图使用CGLIB作为AOP功能(而不是JDK代理).这导致测试失败 我在下面附加了堆栈跟踪. 我不明白

我有一个Spring Web应用程序,配置为使用JDK代理AOP. AOP注释(例如@Transactional)在接口上声明,而不是实现类.

应用程序本身工作正常,但是当我运行单元测试时,似乎试图使用CGLIB作为AOP功能(而不是JDK代理).这导致测试失败 – 我在下面附加了堆栈跟踪.

我不明白为什么在运行测试时使用CGLIB,因为Spring配置与应用程序运行时基本相同.一个可能的显着区别是测试配置使用DataSourceTransactionManager而不是JTA事务管理器.测试类本身都延伸到AbstractJUnit4SpringContextTests,这个类是不是以某种方式硬连线使用CGLIB?

Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class $Proxy25]: 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 $Proxy25
    at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:213)
    at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:110)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:488)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:363)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:324)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:361)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1343)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
    ... 79 more
Caused by: java.lang.IllegalArgumentException: Cannot subclass final class class $Proxy25
    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)
    ... 86 more

编辑:其中一位评论员要求我发布Spring配置.我以缩写形式列出了它(即不相关的bean和XML名称空间省略).

为spring-servlet.xml

的applicationContext-service.xml中

applicationContext-test.xml这仅在运行单元测试时才包含.它的目的是覆盖在其他配置文件中声明的一些bean.

最佳答案
听起来你正在引用一个实现类而不是一个接口.有一个excerpt here有更多的细节.

春季论坛帖子:“Mixing JDK and CGLIB proxies”

一个伟大的博客文章解释pros and cons of JDK vs. CGLIB proxies.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读