title: How to inject extra property sources in SPRING before other beans are created?
body: <p>I am writing a library which will be used by spring-boot projects. I'd like to inject into the boot projects' <code>SpringEnvironment</code> a property source that I take from the Internet.</p>
  
  <p>I tried the following</p>
  
  <pre><code>@Configuration
  public class MyCustomConfiguration {
  
    @Bean
    BeanDefinedAbove above() { /* do some work */ }
  
    @Bean
    @ConditionalOnBean(BeanDefinedAbove.class)
    SmartInitializingSingleton propertySourceSetting(ConfigurableEnvironment env, BeanDefinedAbove bean) {
      return () -&gt; { 
        PropertySource source = bean.getPropertySourceDownloadedFromTheInternet();
        env.getPropertySources().addFirst(source);
      }
    }
  }
  </code></pre>
  
  <p>In my clients' projects when I debug this code what happens is either one of the two:</p>
  
  <ol>
  <li><code>above()</code> is called</li>
  <li>user's <code>@Service</code> or <code>@Controller</code> are called</li>
  <li><code>propertySourceSetting(...)</code> is called</li>
  </ol>
  
  <p>OR </p>
  
  <ol>
  <li>user's <code>@Service</code> or <code>@Controller</code> are called</li>
  <li><code>above()</code> is called</li>
  <li><code>propertySourceSetting(...)</code> is called</li>
  </ol>
  
  <p>Depending whether or not my client's depend on my <code>BeanDefinedAbove</code> bean, which is normal as the <code>@Service</code> is depdent on the bean created in <code>above()</code>.</p>
  
  <p>I have also added the FQDN of my class to the <code>EnableAutoConfiguration</code> in the <code>META-INF/spring.factories</code>.</p>
  
  <p>So how to ensure that the logic in <code>propertySourceSetting(..)</code> is called before users' <code>@Service</code> and <code>@Controller</code></p>