Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple properties file for a single spring profile

We are using spring boot 2.0.0. We have three environments dev, staging, production. Our current config structure

dev

application-dev.yml
application-dev.properties

Likewise, we have a yml and properties file for each environment. After a year of development now the single yml file for a profile become a large monolithic config.

is it possible to have a multiple config files for a profile like below?

application-dev.yml
application-dev-sqs.yml
application-dev-redis.yml
like image 954
kannanrbk Avatar asked Jun 08 '26 11:06

kannanrbk


2 Answers

I think there are 2 ways you can achieve this requirement.

spring.profiles.active accepts a comma-separated list of active profiles, so you can always provide dev,dev-sqs,dev-redis as the value.

Another approach is by making use of @PropertySource and a custom PropertySourceFactory to achieve this requirement. You can find an implementation which takes the value from spring.profiles.active to load one corresponding YAML file in the article below. It should be super easy to adapt the implementation to load multiple files by looking for the profile id in the name of the YAML files.

[How-to] Read profile-based YAML configurations with @PropertySource

like image 80
Mr.J4mes Avatar answered Jun 11 '26 03:06

Mr.J4mes


I was dealing with a similar problem and I'd recommend using yaml configuration.

Let's describe .properties file:

Initital approach

One can use it like this:

@Component
@PropertySources({
    @PropertySource("classpath:application.properties"),
    @PropertySource("classpath:application-${spring.profiles.active}.properties")
})
public class AppProperties {
}

This is very easy to configure. Limitation is, that you cannot combine profiles. I mean, that when you want to use profile as dev,local where local just alters some config properties for dev profile, Spring will try to load application-dev,local.properties file, which is very likely not what you want.

Btw, this is what Spring will do for you automatically, this is useful for topics as you described.

There is no way to configure it per profile (and not for whole list). Other possibility would be, that one can specify the list in spring.config.name which is not the case at the moment.

Better approach

In short, use:

@Profile("dev")
@Configuration
@PropertySources({
        @PropertySource("classpath:topic1-dev.properties"),
        @PropertySource("classpath:topic2-dev.properties")
})
public class AppPropertiesDev {
}

Disadvantage is, you have to have several such config classes (dev, staging), but know you have the topics. Also you can use mutliple profiles, which are (as of my testing) loaded in order you specified. That way, your developer can easily use dev configuration and alter just what's needed for his/her testing.

Yaml approach

You can see the approach with yaml in question I asked earlier - Property resolving for multiple Spring profiles (yaml configuration), benefit is smaller amount of files - yaml has all the profiles in one file, which may or may not be what you want.

like image 33
Betlista Avatar answered Jun 11 '26 01:06

Betlista



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!