问题描述
我正在从 Spring 4.3 升级到 Spring 5.3,但似乎 @ActiveProviles
注释不再支持占位符。
以下代码适用于旧的 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 测试框架中的重大变化。
解决方法
围绕测试用例进行调试后,我对这个问题有了更多的了解。
好像在
org.springframework.core.env.PropertySourcesPropertyResolver#getProperty(java.lang.String,java.lang.Class<T>,boolean)
测试属性源 'test' 拆分配置文件并有两个条目:
spring.profiles.active[0] -> "${profileA}
spring.profiles.active[1] -> "someProfileWithoutPlaceholders"
所以当使用键“spring.profiles.active”查找时,它没有找到任何值,并且没有调用 org.springframework.core.env.AbstractPropertyResolver#resolveNestedPlaceholders
。
这是在构建测试应用程序上下文时在 org.springframework.core.env.AbstractEnvironment#doGetActiveProfiles
中完成的..
在旧的 Spring 版本中,只有一个地图条目:
spring.profiles.active -> "${profileA},someProfileWithoutPlaceholders"
所以它找到了条目并解析了占位符。