0

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 .

Michael Michailidis
  • 1,002
  • 1
  • 8
  • 21
peekay
  • 1,259
  • 2
  • 25
  • 49
  • That is the special Spring Boot properties loading mechanism that isn't going to work with plain spring. Also `${profile}` isn't going to do anything as that isn't known. If you are using Spring Boot then use spring boot and let that load the files for you. Specify a `spring.config.name` with a comma separated value of files to load `-Dspring.config.name=application,batch` and remove what you have and Spring Boot will load what you expected it to load. – M. Deinum Oct 28 '15 at 07:01
  • Can you make your comment the answer? I am getting the properties file to load now "o.s.b.c.c.ConfigFileApplicationListener : Loaded config file 'classpath:/batch-hsql.properties'". I am still seeing an error with it not finding the properties inside the file but I think that is a different issue. thanks. – peekay Oct 28 '15 at 08:05

1 Answers1

3

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.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224