Springboot - multiple property files per profile
Asked Answered
P

4

11

Looking for best approach in configuring multiple Profile specific property files in Springboot application. Below is an example:

-resources
   -- application.properties
       -- dev
             -- application-dev.properties
             -- ldap-dev.properties
             -- quartz-dev.properties
             -- etc-dev.properties
     -- test
             -- application-test.properties
             -- ldap-test.properties
             -- quartz-test.properties
             -- etc-test.properties
     -- prod
             -- application-prod.properties
             -- ldap-prod.properties
             -- quartz-prod.properties
             -- etc-prod.properties

The application.properties and application-profile.properties files are loading okay. I'm looking for a recommended approach in loading the other profile specific property files. I'm not sure if there is a way to load all property files from a folder based on a profile?

Proteiform answered 18/11, 2014 at 20:51 Comment(0)
R
6

You have quite a lot of flexibility with the built in configuration listener. E.g. You could set spring.config.name=application,ldap,quartz,etc and spring.config.location=classpath:/,classpath:/dev,classpath:/prod,classpath:/test. Or the equivalent env vars. The links in Selim's answer document the basic behaviour and config options.

Redpoll answered 19/11, 2014 at 18:18 Comment(0)
U
4

As of Spring boot 2.0.4, this feature comes out of the box as long as you specify your config file in spring.config.name environment variable, e.g spring.config.name=application,ldap,quartz

Uproarious answered 18/4, 2019 at 8:14 Comment(0)
A
3

I'm not sure is there any better way to that or is my suggestion really work but you can try this:

Add @PropertySource Annotation right before your configuration class

@PropertySource("classpath:ldap-${spring.profiles.active}.properties", "classpath:quartz-${spring.profiles.active}.properties", "classpath:etc-${spring.profiles.active}.properties")

To understand better how Spring load configurations from different sources and profiles see this and this.

I hope it helps.

Assets answered 19/11, 2014 at 9:31 Comment(0)
N
0

@PropertySources can be used to load multiple properties files with profile as ldap-${spring.profiles.active}.properties

But if you are using YAML, @PropertySource will not work. You have to use @ConfigurationProperty to load YML files other than application.yml

Nixon answered 7/12, 2018 at 11:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.