Home > Net >  Spring Boot application to have separated multiple property files
Spring Boot application to have separated multiple property files

Time:07-08

Coming from Play Framework, a handy feature that has helped to organize the application configurations was to use includes (Link) to spilt the various configurations into multiple .conf files as below.

application.conf Content

include "play-http.conf"
include "play-modules.conf"
include "play-i18n.conf"
include "authentication.conf"
include "hbase.conf"
include "custom-caches.conf"
include "custom-filters.conf"

#Any other root level application configurations

Is there an equivalent to this in Spring Boot .properties files?

CodePudding user response:

I use yaml configuration files myself but I think that the configuration is mostly similar. You should take a look at the PropertySourcesPlaceholderConfigurer.

I've defined a PropertySourcesPlaceholderConfigurer bean to use a configuration override file located outside of the jar. Anything that is in the override file will be used instead of the default configuration. Anything that is not in the override file is still retrieved from the default configuration file. I think you can create a similar bean to achieve what you are looking for.

Here's my code:

@Bean
static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    var properties = new PropertySourcesPlaceholderConfigurer();
    properties.setLocation(new FileSystemResource("./application.yaml"));
    properties.setIgnoreResourceNotFound(true);
    return properties;
}

For my use case, I only needed to define one properties location, but it is also possible to specify multiple locations:

...
properties.setLocations(Resource... locations);
...

I hope this helps.

CodePudding user response:

From Spring 2.4, we can create multiple properties file for each profiles as below.

application-main1.properties 
application-sub1.properties
application-sub2.properties

And then in default application.properties file we can group all sub profiles and activate the main profile

spring.profiles.group.main1=sub1,sub2

spring.profiles.active=main1

I am not sure if we can group sub profiles under default profile. You can try out

spring.profiles.group.default=sub1,sub2

This way you don't need to have another file for main profile.

  • Related