@ActiveProfile and spring.profiles.active

随声附和 提交于 2020-01-14 19:25:07

问题


This is a piece of my applicationContext definition to retrieve some properties.

 <!-- get some properties -->
<context:property-placeholder
        ignore-resource-not-found="false" ignore-unresolvable="false"
        location="classpath:/properties/${spring.profiles.active:test}/some.properties"/>

As you can see I letting the spring.profiles.active decide which properties will be read. My tests are annotated with:

@ActiveProfile("integration")

You guessed it right my spring bean profiles are actually matching the environments in which to deploy/test the application. Still my location property is getting resolved to "/properties/test/some.properties". Which is of course because the spring.profiles.active doesn't seem to get resolved in this case.

How could I achieve getting the the right properties?


回答1:


It is because active profiles may be activated by system property (but in case of @ActiveProfiles it works another way).

Just like this:

<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>

Also, you may try to change location="classpath:/properties/${spring.profiles.active:test}/some.properties" to location="classpath:/properties/${spring.profiles.active}/some.properties"




回答2:


See ticket: https://jira.springsource.org/browse/SPR-8982#comment-88498

Someone already had made a request for this:

An option to override an @ActiveProfile specified by test in runtime from command line by "-Dspring.profiles.active" or other systemProperty

My comment:

That or it should set the property spring.profiles.active.



来源:https://stackoverflow.com/questions/15228441/activeprofile-and-spring-profiles-active

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!