【发布时间】:2019-02-04 09:21:55
【问题描述】:
当将 spring 从 2.0.8 升级到 2.1.2(使用 JDK 8)时,应用程序启动并运行良好,但由于 java.lang.IllegalStateException: Failed to load ApplicationContext 而测试失败。
我正在使用一些测试扩展的抽象类。
@SpringBootTest
@RunWith(SpringRunner.class)
public abstract class AbstractTestkonfiguration {
@TestConfiguration
static class TestEnvironmentConfiguration {
@Component
@PropertySource(value = "classpath:my-test.properties")
@ConfigurationProperties(prefix = "my")
public static class MyTestProperties extends EnvironmentalProperties {
}
}
}
EnvironmentalProperties 类是类型安全配置属性的类 (Doc)
在有效的升级和提供一类 EnvironmentalProperties 之前,但现在我得到了一个
[...]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.abc.EnvironmentalProperties' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.inject.Inject()}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1651)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1210)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1164)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
... 90 more
这可能与嵌套配置类检测 (Upgrading to Spring Framework 5.x) 的更改有关吗?如果是这样,我如何配置一个仅用于测试的 EnvironmentalProperties bean?
更新:即使如下使用它也不起作用(相同的结果)。
@SpringBootTest
@RunWith(SpringRunner.class)
public abstract class AbstractTestkonfiguration {
@Configuration
public static class TestEnvironmentConfiguration {
@Bean
public MyTestProperties environmentalProperties(){
return new EnvironmentalProperties() {
// manual creation of an instance
}
}
}
}
【问题讨论】: