我有一个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.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。