4

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
  • 1,183
  • 2
  • 15
  • 27

2 Answers2

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"

Michail Nikolaev
  • 3,733
  • 22
  • 18
  • That 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. – Peter De Winter Mar 05 '13 at 18:58
  • In such case you may add custom JUnitTestRunner and set system property according to @ActiveProfiles – Michail Nikolaev Mar 05 '13 at 19:41
  • We 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. – Peter De Winter Mar 06 '13 at 05:54
1

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.

Peter De Winter
  • 1,183
  • 2
  • 15
  • 27