spring - @ActiveProfile 和 spring.profiles.active

标签 spring properties configuration applicationcontext profiles

这是我的 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/

相关文章:

delphi - 在 IDE 对象检查器中动态显示/隐藏属性?

wcf - 更改 SwaggerWcf 中的参数类型

java - 如何从代码动态地为 Java RMI 设置 SSL?

python - 为什么@property 比属性慢,而字节码是相同的

linux - 如何在 Puppet 中使用配置文件模板

java - 如何将Java项目从jar导入到eclipse中?

spring - 通过属性禁用 CSRF 保护

spring - 启用身份验证时如何配置spring boot admin客户端?

java - 未找到 com.models.entities.OrderEntity 类型的属性

javascript - 向数组对象添加属性