MMP
MMP

Reputation: 556

Load Spring-Boot application.yaml file based on system environment variables

My friend and I are working on a Spring-Boot application. Right now when running mvn spring-boot:run, Spring-Boot, by default, loads the application.yaml file under src/main/resources/config.

My friend and I have a system environment variable called 'DEV' setup. So in the command line, typing echo $DEV would return dev.friend or dev.mmp.

Under src/main/resources/config, we want to add the directories /dev/friend and /dev/mmp. And within these directories, there will be a specific application.yaml file.

So now we have this:

How can we set up Spring configurations using only annotations (we want to avoid writing anymore xml other than pom.xml) such that when we run mvn spring-boot:run the application.yaml file loaded will be based on the DEV environment variable of the current computer.

We know that we can do the following to create a FileSystemResource from the DEV variable:

public FileSystemResource configLoader() throws IOException {
    StringBuilder path;
    URL propertiesURL;
    String hostName = System.getenv("DEV");
    path = new StringBuilder("/config");
    if (hostName != null && !hostName.isEmpty()) {
        String[] hostNameParts = hostName.split("\\.");
        for (String s : hostNameParts) {
            path.append('/').append(s);
        }
    }
    path.append('/').append("application.yaml");
    propertiesURL = getClass().getResource(path.toString());
    return new FileSystemResource(propertiesURL.getFile());
}

But we have no idea how to proceed, this is our first Spring project. Google searches only lead to examples loading a default application.yaml (which we already have) located under config/ and that's not what we want. We want to extend this so that our staging server will have its own DEV system variable and we can just add another directory under config (like above) with an application.yaml file.

Upvotes: 2

Views: 14094

Answers (2)

Ali Dehghani
Ali Dehghani

Reputation: 48123

In addition to application.yml files, profile-specific yamls can also be defined using the naming convention application-{profile}.yml. So, it's better to define three yaml configs as following: application.yml, application-devfriend.yml and application-devmmp.yml.

If you do not activate any profile, by default application.yml will be loaded. If you activate devfriend profile, config values from application-devfriend.yml would override the application.yml ones. In order to activate one profile, use spring.profiles.active property. For example:

java -jar -Dspring.profiles.active=devfriend yourapp.jar

You can also add a environment variable for spring-profiles-active.

Update: If you insist to use DEV environment variable for switching between profiles, add this in your application.yml:

spring:
  profiles:
    active: ${dev}

Upvotes: 3

m.aibin
m.aibin

Reputation: 3593

You can use:

@ConfigurationProperties(locations = {"yourProperties"}) //create this path as a String above

to specify the location, which Spring Boot will load from a config class.

More here: Spring External Config

Alternatively, in command line you can use property to mention an additional boot configuration file:

--spring.config.name="file:/path/to/application.yaml"

Upvotes: 1

Related Questions