title: ApplicationContextInitializer loaded twice by Spring Cloud Bootstrap context & Spring Boot
body: <p>When declaring initializers in <code>spring.factories</code> file to create Spring Boot starters, we realized that those initializers were loaded twice : </p>
  
  <ul>
  <li>once by the Spring Cloud Boostrap Context</li>
  <li>once by the Spring Boot context</li>
  </ul>
  
  <p>In our case, we start databases in docker containers, so we don't want to do it twice.</p>
  
  <p>According to this issue, this is Spring Cloud's expected behavior : <a href="https://github.com/spring-cloud/spring-cloud-config/issues/1151" rel="nofollow noreferrer">https://github.com/spring-cloud/spring-cloud-config/issues/1151</a></p>
  
  <p>When asking how the boostrap context should be distinguished from the "regular" application context, the answer given is to </p>
  
  <blockquote>
    <p>Check the ID of the context.</p>
  </blockquote>
  
  <p>After running a sample application, <code>ConfigurableApplicationContext.getId()</code> returns by default :</p>
  
  <ul>
  <li><code>application</code> for the Spring Cloud Bootstrap context</li>
  <li><code>application-1</code> for the Spring Boot context</li>
  </ul>
  
  <p>Some of our users don't define <code>spring.application.name</code>, others don't use Spring Cloud at all.</p>
  
  <p><strong>Question</strong> : How can we reliably load an initializer only once ?  </p>
  
  <p>If <code>ApplicationContextInitializer</code>s are meant to be idempotent, it should probably figure in the interface's Javadocs.</p>
  
  <p>In the worst case, how can we safely distinguish the Spring Cloud boostrap context from the Spring Boot context ?</p>