title: Spring /boot, profiles, java config and bean creation order (Unexpected behaviour??)
body: <p>I am working on a spring boot app. We have properties files for each environment (dev, int, qa, prd etc). As for the beans, some needs to be created differently for production. So we have this configuration:</p>
  
  <pre><code>@Configuration
  class MyBeans {
      @Bean(name="someBean")
      @Profile("prd")
      MyBean someBeanProd() {
          ...
      }
  
      @Bean(name="someBean")
      MyBean someBeanDev() {
          ...
      }
  }
  </code></pre>
  
  <p>MyBean is autowired as follow:</p>
  
  <pre><code>@Component
  public class SomeService 
  {
      @Autowired
      private MyBean someBean;
  }
  </code></pre>
  
  <p>When starting the application with: </p>
  
  <blockquote>
    <p>-Dspring.profiles.active=prd</p>
  </blockquote>
  
  <p>'someBeanProd' method is getting called (as expected), and the relevant properties file is being loaded (application-prd.properties). For any other profile 'someBeanDev' is being called (as expectyed) and the relevant properties file is loaded. </p>
  
  <p>However, if I change the order of the methods in the config as below, 'someBeanDev' method is being called ALWAYS (even when active profile is prd), but the correct properties file is being used.    </p>
  
  <pre><code>@Configuration
  class MyBeans {
      @Bean(name="someBean")
      MyBean someBeanDev() {
          ...
      }
  
      @Bean(name="someBean")
      @Profile("prd")
      MyBean someBeanProd() {
          ...
      }
  }
  </code></pre>
  
  <p>Is this the correct/expected behaviour? I expected spring to create the correct bean regardless of the order of the methods in the config file.</p>
  
  <p>Is there a way to define a bean for one profile, and any other profile should fallback to the 'default' profile?  </p>
  
  <p>Creating the prd bean in a different Java config class is also not working.</p>