问题
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)