@ActiveProfile and spring.profiles.active
Solution 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"
Solution 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.
Related videos on Youtube
Peter De Winter
Check the blog or my linked-in profile for more info.
Updated on June 04, 2022Comments
-
Peter De Winter almost 2 years
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?
-
Peter De Winter about 11 yearsThat is indeed a workaround I was also thinking about, but still this feels like a bug to me in the @ActiveProfile annotation implementation. I would have expected it to set the spring.profiles.active property for completeness sake. I`m not a big fan of writing a lot of xml.
-
Michail Nikolaev about 11 yearsIn such case you may add custom JUnitTestRunner and set system property according to @ActiveProfiles
-
Peter De Winter about 11 yearsWe could also set the propertysource using @PropertySource and make sure our PropertyPlaceHolder does not complain about missing property files. But the point is getting lost of having this property.