我的应用程序.yaml有以下条目:
spring:
profiles:
active: test
include: h2, test
key:
enabled: on
字符串
我有以下三个bean定义,并希望在我的测试中加载TestConfig bean。但它在测试中接受NoConfig Bean:
@ConditionalOnProperty(name = "key.enabled", matchIfMissing = true)
@Profile("test")
public class TestConfig {}
@ConditionalOnMissingBean(Config.class)
public class NoConfig {}
@ConditionalOnProperty(name = "key.enabled", matchIfMissing = true)
@Profile("!test")
public class Config{}
型
我还检查了测试中的活动配置文件,它确实是测试配置文件。
@Value("${spring.profiles.active:Unknown}")
private String activeProfile;
型
1条答案
按热度按时间xam8gpfp1#
您拥有的Bean
NoConfig
总是在没有BeanConfig
时创建。您可以尝试使用@TestConfiguration
来定义特定测试所需的Bean,或者您可以编写BeanFactoryPostProcessor
以从初始化中排除不必要的Bean或者尝试在
@ConditionalOnProperty
注解中添加havingValue
属性,并将yaml文件中的key.enabled
属性的值设置为true