【发布时间】:2012-10-16 07:39:18
【问题描述】:
问题
Cobertura 仪器在特定情况下会破坏自动装配的弹簧。有谁知道如何解决这个问题?
场景
- 我正在运行带有 cobertura-maven-plugin 版本 2.5.1 的 MVN 3.0.4。
- mvn 测试运行没有问题
- mvn compile、package 等也可以正常运行。
- mvn cobertura:cobertura 也运行没有任何问题,直到添加了 2 个引入许多新类的新功能,包括两个新的 com.mycompany.executor 执行器类。 (示例:除了现有的 MyExecutor 之外,还添加了 MyHappyExecutor 和 MySadExecutor)
- 从 cobertura 检测过程中排除 MyExecutor 似乎可以修复自动装配
- 检查 spring 自动装配输出确认正确的 bean 正在自动装配。
故障点
尝试在 myService 中自动装配 myExecutor 的检测版本时,自动装配失败。这在添加 MyHappyExecutor 和 MySadExecutor 之前运行良好。 MyHappyExecutor 和 MySadExecutor 是自动装配的,并且只在 MyExecutor 中使用。
我在下面附上了异常输出。请注意类名和包名已被编辑。
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myService': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mycompany.executor.MyExecutor com.mycompany.service.impl.MyServiceImpl.myExecutor;
nested exception is java.lang.IllegalArgumentException: Can not set com.mycompany.executor.MyExecutor field com.mycompany.service.impl.MyServiceImpl.myExecutor to $Proxy20
结论
Cobertura 检测过程中的某些东西弄乱了 Springs 的自动装配。
更新 1
强制 CGLIB 类代理会将错误类型更改为“java.lang.NoClassDefFoundError”错误。这会影响标准测试目标以及 Cobertura 目标。
<aop:config proxy-target-class="true"/>
更新 2
这是 3 个有问题的类的 springs 启动过程的输出。
2012-11-01 16:21:51 INFO [main] Overriding bean definition for bean 'myExecutor': replacing [Generic bean: class [com.mycompany.executor.MyExecutor]; scope=; abstract=false; lazyInit=false; autowireMode=1; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [configuration.xml]] with [Generic bean: class [com.mycompany.executor.MyExecutor]; scope=; abstract=false; lazyInit=false; autowireMode=1; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [configuration.xml]] - (DefaultListableBeanFactory.java:623)
2012-11-01 16:21:51 INFO [main] Overriding bean definition for bean 'happyExecutor': replacing [Generic bean: class [com.mycompany.executor.HappyExecutor]; scope=; abstract=false; lazyInit=false; autowireMode=1; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [configuration.xml]] with [Generic bean: class [com.mycompany.executor.HappyExecutor]; scope=; abstract=false; lazyInit=false; autowireMode=1; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [configuration.xml]] - (DefaultListableBeanFactory.java:623)
2012-11-01 16:21:51 INFO [main] Overriding bean definition for bean 'sadExecutor': replacing [Generic bean: class [com.mycompany.executor.SadExecutor]; scope=; abstract=false; lazyInit=false; autowireMode=1; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [configuration.xml]] with [Generic bean: class [com.mycompany.executor.SadExecutor]; scope=; abstract=false; lazyInit=false; autowireMode=1; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [configuration.xml]] - (DefaultListableBeanFactory.java:623)
【问题讨论】:
-
@TomaszNurkiewicz 这似乎是一个相关的问题。 AOP 代理方法在这种情况下似乎不起作用。对一些更高级的调试方法有什么建议吗? (我使用显式
语法在 applicationContext 中设置我的 bean。) -
我应该提一下,执行器没有实现任何接口。
标签: spring maven instrumentation autowired cobertura