@ActiveProfile和spring.profiles.active

@ActiveProfile和spring.profiles.active,spring,properties,configuration,applicationcontext,profiles,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}/so

这是我的applicationContext定义的一部分,用于检索一些属性

 <!-- get some properties -->
<context:property-placeholder
        ignore-resource-not-found="false" ignore-unresolvable="false"
        location="classpath:/properties/${spring.profiles.active:test}/some.properties"/>
您猜对了,我的Springbean配置文件实际上与部署/测试应用程序的环境相匹配。 我的位置属性仍然被解析为“/properties/test/some.properties”。这当然是因为spring.profiles.active在本例中似乎没有得到解决


如何获得正确的属性

这是因为活动配置文件可能会被系统属性激活(但在
@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”

参见票证:

已经有人对此提出了请求:

通过“-Dspring.profiles.active”或其他系统属性从命令行重写运行时测试指定的@ActiveProfile的选项

我的评论是:

或者它应该设置属性spring.profiles.active


这确实是我也在考虑的一个解决方法,但对@ActiveProfile注释实现来说,这仍然是一个bug。为了完整性,我希望它设置spring.profiles.active属性。我不太喜欢编写大量xml。在这种情况下,您可以添加自定义JUnitTestRunner并根据@ActiveProfile设置系统属性。我们还可以使用@propertysource设置propertysource,并确保我们的PropertySplaceHolder不会抱怨缺少属性文件。但关键是失去这一财产。
<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>