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

java– 运行Spring单元测试的AOP问题,第1张

概述我有一个Spring Web应用程序,配置为使用JDK代理AOP. AOP注释(例如@Transactional)在接口上声明,而不是实现类.应用程序本身工作正常,但是当我运行单元测试时,似乎试图使用CGLIB作为AOP功能(而不是JDK代理).这导致测试失败 - 我在下面附加了堆栈跟踪.我不明白为什么在运行测试时使用CGLIB,因为Spring配置与应用程

我有一个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 moreCaused 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. 总结

以上是内存溢出为你收集整理的java – 运行Spring单元测试的AOP问题全部内容,希望文章能够帮你解决java – 运行Spring单元测试的AOP问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1269950.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)