@ActiveProviles
我正在从 Spring 4.3 升级到 Spring 5.3,并且注释似乎不再支持占位符。
以下代码适用于旧的 Spring 版本:
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles({"${profileA}","someProfileWithoutPlaceholders"})
@ContextConfiguration(classes = MyApplication.class)
public class MyTest {...}
但它停止了升级工作,现在它得到了
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'nucleus.hibernate.dialect' in value "${nucleus.hibernate.dialect}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:178)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:175)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:936)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1321)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
... 55 more
请注意,“nucleus.hibernate.dialect”是在“${profileA}”引用的应用程序属性中定义的。
作为一种解决方法,我尝试使用显式将属性指定为测试属性源
@TestPropertySource(locations={"classpath:/application-${profileA}.properties"}})
那行得通。
我不确定在 Spring 集成测试中使用占位符选择 Spring 配置文件是否是官方支持的功能。如果是,我认为这是 Spring 测试框架中的重大变化。