springpropertiesspring-bootapplicationcontextspring-profiles

Loading profile specific properties when there are multiple active profiles


According to http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html I can load different application.properties files using application-${profile}.properties and setting the active profile. That is great if application.properties is changing, but what if its batch.properties that I need to do this with? and what if I have multiple active profiles? for example:

spring.active.profile=oracle,hornetq,redis

and my properties are loaded using:

<bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    depends-on="datasourceProperty">
    <property name="locations" ref="propLocations" />
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="order" value="1" />
</bean>
<util:list id="propLocations">
    <value>classpath:batch-default.properties</value>
    <value>classpath*:batch-${profile}.properties</value>
</util:list>

I was assuming that batch-${profile}.properties would try to look for all the properties files with any of the active profiles, so batch-oracle.properties, batch-redis.properties, batch-hornetq.properties

The ones it found it would use and the ones not found would be ignored. This does not seem to be the case however, and the ${profile} is simply not found.

if I only have one active profile its fine, I can just use ${spring.active.profile}, but I am slowly creating more profiles as I componentize my application and I would like to use the profile to load the properties without creating a bunch of profile specific property placeholder beans.

----- UPDATE ----- Based on the comment by "M. Deinum" I have tried the following:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes={BatchBootApplication.class})
@WebIntegrationTest(value={"spring.config.name=application,batch"})
@ActiveProfiles("hsql")
public class BatchBootApplicationTest {

    @Test
    public void testAppContextLoads() {
    }

}

and I see the properties file .


Solution

  • By default Spring Boot loads the application.properties and with its loading mechanism also the application-{profile}.properties (or yml files). See the External Config section of the Spring Boot Reference guide.

    To override/extend the files load you can specify a spring.config.name environment variable. This variable can take a comma separated string to identify multiple property files. See the sample here.

    So instead of trying to hack something together yourself use Spring Boot. When starting the application simply add -Dspring.config.name=application,batch,other file and Spring Boot will apply the loading rules to all the specified names.