title: @Configuration bean not being loaded first
body: <p>I have a <code>@Configuration</code> bean which defines various beans in Java</p>
  
  <pre><code>@Configuration
  public class jmsConfiguration {
     // Various @Bean definitions
  }
  </code></pre>
  
  <p>I also have the folowing bean:</p>
  
  <pre><code>@Component
  @Profile({"BATCH_MODE"})
  public class BatchLauncher  {
  
      @Autowired
      private Launcher launcher;
  
      @PostConstruct
      public void init(){
         launcher.launch();
         // The launcher bean encapsulates the logic to
         // get the Spring Batch JobLauncher bean,
         // build the job parameters and start the job
      }
  }
  </code></pre>
  
  <p>The problem is this:
  The <code>init()</code> method which gets our <code>Launcher</code> bean and starts the batch job is called <em>before</em> the <code>@Configuration</code> class has been initialized.
  If the code <code>launcher.launch()</code> is commented out and the application run in debug, the debugger will stop in the code which constructs the various <code>@Bean</code>'s. However, when the <code>launcher.launch()</code> is there and is run, that code will get run first, which means that the beans on which the <code>ItemWriter</code> is dependent have not been properly initialized.</p>
  
  <p>This is the code used to start the Spring context:</p>
  
  <pre><code>GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
  ctx.load("app-context.xml");
  ctx.getEnvironment().getPropertySources().addLast(propertySource);
  ctx.refresh();
  ctx.start();
  </code></pre>
  
  <p>This is how the Launcher bean is defined in XML:</p>
  
  <pre><code>&lt;context:component-scan base-package="com.company.app" /&gt;
  &lt;beans&gt;
         &lt;bean id="launcher" class="com.company.Launcher" /&gt;
  ...
  &lt;/beans&gt;
  </code></pre>
  
  <p>What is the best way to solve this?</p>
  
  <p>Thanks</p>