Skip to main content
 首页 » 编程设计

spring-test、groovy 库和限定符标签不兼容

2024年02月27日31pengyingh

我正在尝试向现有项目添加一些常规脚本,并且我坚持让集成测试工作。 我有几颗标有 <qualifier /> 的 bean 标签,用于在测试和生产代码中 Autowiring 。

在我添加“org.codehaus.groovy:groovy-all:2.4.0”之后 (也尝试过其他版本)到依赖项,甚至没有任何常规用法,我的集成测试停止工作,但异常(exception):

    SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@5e5f7983] to prepare test instance [com.dph.groovy.vs.springtest.IntegrationTest@299c9fe7] 
    java.lang.IllegalStateException: Failed to load ApplicationContext 
        at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:94) 
        at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:72) 
        at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117) 
        at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) 
        at  
...... 
    Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unexpected failure during bean definition parsing 
    Offending resource: class path resource [spring/app-config.xml] 
    Bean 'service'; nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Tag 'qualifier' must have a 'type' attribute 
    Offending resource: class path resource [spring/app-config.xml] 
    Bean 'service' 
        at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70) 
        at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85) 
        at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:323) 

运行项目(如果这很重要的话,使用 jetty 6)不会导致任何问题,所以我认为 spring-test 与 groovy 联合有一些技巧。

我可能只是将“类型”添加到我的限定符中,但是它并不能解决问题,因为我具有具有相同限定符标签配置的外部依赖项,此外据我所知,此属性是可选的。

我很想至少找出这个问题的根源。

我创建了示例项目来重现所描述的问题,并且会欣赏任何想法: https://github.com/ametiste/groovy-vs-spring-test

请您参考如下方法:

您发现了 Spring 测试支持中的一个错误。

在 Spring Framework 4.1.6 和 4.2 RC1 中已修复

我已经修复了 Spring Framework 4.1.6(计划于 2015 年 3 月底发布)和 4.2(计划于 2015 年第三季度发布)的此错误。欲了解更多详情,请参阅JIRA问题SPR-12768 .

如果您希望在上述版本之前尝试修复,请考虑针对即将推出的夜间快照之一进行构建。

临时解决方法

同时,(对于您有权编辑的 XML 配置文件)您可以通过显式设置 type 来规避此错误。 <qualifier> 中的属性标记为预期的默认值,即 "org.springframework.beans.factory.annotation.Qualifier" 。请参阅以下 XML 配置示例。

<bean id="foo" class="java.lang.String" c:_="bar"> 
    <qualifier value="foo" type="org.springframework.beans.factory.annotation.Qualifier" /> 
</bean> 

问候,

山姆 (Spring TestContext 框架的作者)