在Cobertura仪表课的情况下,Spring @Autowired失败了

ely*_*ion 12 spring instrumentation cobertura autowired maven

Cobertura仪器在特定情况下打破弹簧自动装配.有谁知道如何解决这个问题?

脚本

  • 我使用cobertura-maven-plugin版本2.5.1运行MVN 3.0.4.
  • mvn测试没有问题
  • mvn编译,包等也运行没有问题.
  • mvn cobertura:cobertura也没有遇到任何问题,直到添加了2个新功能,这些功能引入了许多新类,包括两个新的com.mycompany.executor执行器类.(示例:除了现有的MyExecutor之外还添加了MyHappyExecutor和MySadExecutor)
  • 从cobertura仪器过程中排除MyExecutor似乎可以修复自动装配
  • 检查弹簧自动装配输出确认正确的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
Run Code Online (Sandbox Code Playgroud)

结论

Cobertura仪表过程中的某些东西会影响Springs的自动装配.

更新1

强制CGLIB类代理将错误类型更改为"java.lang.NoClassDefFoundError"错误.这会影响标准测试目标以及Cobertura目标.

<aop:config proxy-target-class="true"/>
Run Code Online (Sandbox Code Playgroud)

更新2

以下是有关3个类的弹簧启动过程的输出.

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)
Run Code Online (Sandbox Code Playgroud)

jas*_*sop 0

对于您的测试,您需要设置proxyTargetClass=true

@EnableTransactionManagement(mode=AdviceMode.ASPECTJ, proxyTargetClass=true)
Run Code Online (Sandbox Code Playgroud)

如果这适用于您的测试,但当您运行应用程序时它失败,那么您需要为测试和应用程序进行单独的配置。测试配置集proxyTargetClass=true和应用程序配置集proxyTargetClass=false

对于您的NoClassDefFoundError错误,我们需要查看堆栈跟踪。您可能还没有包含 spring-aop 库