【发布时间】:2016-06-14 09:28:05
【问题描述】:
我有这样的 Spring Boot 测试:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {
PropertyConfig.class,
ServiceConfigA.class,
ServiceConfigB.class}
)
public class SpringTest {
@Test
public void test() {
...
}
}
当 PropertyConfig 类在类列表中首先声明时,我收到上下文初始化错误,因为 PropertyConfig 中的 bean 被忽略并且服务配置中的 bean 无法自动装配某些字段。当我在一些服务配置之后移动PropertyConfig 时,PropertyConfig 中的 bean 被初始化。
更详细地说,PropertyConfig 包含两个 bean:PropertiesFactoryBean 和 PropertySourcesPlaceholderConfigurer。 PropertySourcesPlaceholderConfigurer 的原因不存在来自服务配置的 bean 无法使用 @Value 注释自动装配字段(无法自动从字符串转换为整数)。
我的问题是为什么 PropertyConfig 的 bean 在第一种情况下没有初始化?
Spring Boot 测试中是否有一些配置加载功能?
【问题讨论】:
-
我的提示是否让您的
PropertySourcesPlaceholderConfigurerbeanstatic为您工作?
标签: java spring spring-boot spring-test