Load Spring-Boot application.yaml file based on system environment variables
<p>My friend and I are working on a Spring-Boot application. Right now when running <code>mvn spring-boot:run</code>, Spring-Boot, by default, loads the application.yaml file under <code>src/main/resources/config</code>.</p>

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

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

<p>So now we have this:</p>

<ul>
<li>src

<ul>
<li>main

<ul>
<li>resources

<ul>
<li>config

<ul>
<li>application.yaml</li>
<li>dev

<ul>
<li>friend

<ul>
<li>application.yaml</li>
</ul></li>
<li>mmp

<ul>
<li>application.yaml</li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul>

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

<p>We know that we can do the following to create a FileSystemResource from the <code>DEV</code> variable:</p>

<pre><code>public FileSystemResource configLoader() throws IOException {
    StringBuilder path;
    URL propertiesURL;
    String hostName = System.getenv("DEV");
    path = new StringBuilder("/config");
    if (hostName != null &amp;&amp; !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());
}
</code></pre>

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