title: How can I load a `application-{profile}.yml` property source without using `spring.profiles.active`?
body: <p>I'm trying to integrate an existing application with Spring Boot. One of the things I'd like to do is clean up the way we load property files by having the usual Spring Boot application configuration file like this:</p>
  
  <p><code>
  ├── application.yaml
  ├── application-DEV.yaml
  </code></p>
  
  <p>The thing is that I can't add the system property <code>spring.profiles.active</code> because I'm relying on some existing infrastructure which has a system property with the name <code>environment</code> instead. I'm trying an alternative solution to set the active profile by using an <a href="http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/ApplicationContextInitializer.html" rel="nofollow noreferrer"><code>ApplicationContextInitializer</code></a> which according to the documentation:</p>
  
  <blockquote>
    <blockquote>
      <blockquote>
        <p>Typically used within web applications that require some programmatic initialization of the application context. For example, registering property sources or <strong>activating profiles</strong> against the context's environment.</p>
      </blockquote>
    </blockquote>
  </blockquote>
  
  <p>This is my code which tries to add an active profile by reading the <code>environment</code> system property:</p>
  
  <pre class="lang-java prettyprint-override"><code>  @Slf4j
    public class AddEnvironmentProfileApplicationContextInitializer implements ApplicationContextInitializer {
  
       @Override
       public void initialize(ConfigurableApplicationContext applicationContext) {
          String environment = System.getProperty("environment");
          log.info("Adding active profile from System Property environment={}", environment);
          applicationContext.getEnvironment().addActiveProfile(environment);
       }
    }
  </code></pre>
  
  <p>which I'm loading with a starter dependency by defining the initializer in <code>spring.factories</code> with the key <code>org.springframework.context.ApplicationContextInitializer</code>.</p>
  
  <p>If I run the application with <code>-Denvironment=DEV</code>, I can see that the <code>DEV</code> profile is added correctly, but the <code>application-DEV.yml</code> file is not loaded.</p>
  
  <p>Am I missing something? I did inspect the source code for Spring Boot and it looks like the Property Sources are configured before running the Initializers, but then why would you define a profile with it if the right files are not loaded? Is this the desired behaviour or is it maybe a bug?</p>