这是我的 applicationContext 定义的一部分,用于检索一些属性。
<!-- get some properties -->
<context:property-placeholder
ignore-resource-not-found="false" ignore-unresolvable="false"
location="classpath:/properties/${spring.profiles.active:test}/some.properties"/>
如您所见,我让 spring.profiles.active 决定将读取哪些属性。
我的测试注释为:
@ActiveProfile("integration")
您猜对了,我的 spring bean 配置文件实际上与部署/测试应用程序的环境相匹配。
我的位置属性仍然被解析为“/properties/test/some.properties”。这当然是因为在这种情况下 spring.profiles.active 似乎没有得到解决。
我怎样才能获得正确的属性?
最佳答案
这是因为事件文件 5 月 由系统属性激活(但在 @ActiveProfiles
的情况下,它以另一种方式工作)。
像这样:
<beans profile="dev,prod,qa">
<context:property-placeholder location="classpath:some.properties" ignore-unresolvable="true"/>
</beans>
<beans profile="test">
<context:property-placeholder location="classpath:some-test.properties" ignore-unresolvable="true"/>
</beans>
另外,你可以尝试改变
location="classpath:/properties/${spring.profiles.active:test}/some.properties"
到location="classpath:/properties/${spring.profiles.active}/some.properties"
关于spring - @ActiveProfile 和 spring.profiles.active,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15228441/