我在IntelliJ上有一个Serenity-BDD项目,包含Serenity-Spring和多个.properties文件,一个显示每个部署环境(dev,qa,production),一个包含localhost变量的基本.properties文件。
test.properties
test-dev.properties
test-qa.properties
test-prod.properties
我在CLI命令(-Denvironment)中传递一个参数来选择将覆盖基础的.properties文件。
./gradlew build -Denvironment
在我的@PropertiesSource中,我列出了两个文件,以及覆盖文件的环境变量:
@PropertySource(value = {"test.properties", "test-${environment}.properties"}, ignoreResourceNotFound = true)
但是,当我通过IntelliJ本地运行它(意思是没有 - 环境变量,意思是localhost,并且只想要test.properties文件)时,我的输出中出现以下错误:
信息:属性位置[test - $ {environment} .properties]无法解析:无法解析值“test - $ {environment} .properties”中的占位符“environment”
究竟是什么错误,解决它的最佳方法是什么?
当您硬编码加载两个属性文件的事实时,我会使用Spring SpEL默认值机制:
@PropertySource(value = {"test.properties", "test-${environment:local}.properties"})
这样,当没有可用的环境时,Spring将加载test.properties和test-local.properties。
你有[test - $ {environment} .properties]不可解析的错误是正常的,因为在加载PropertySources
之前解析占位符(如果你考虑它就是逻辑)。
此外,ignoreResourceNotFound = true
容易出错,我不建议在生产代码中使用它。
如果您只使用一个文件,则可以使用
@PropertySource("${environment:local}.properties")
这是Spring将加载的内容: