title: Instantiate beans in order in Spring?
body: <p>Is it possible to set order of instantiation in Spring?</p>
  
  <p>I don't want to use <code>@DependsOn</code> and I don't want to use <code>Ordered</code> interface. I just need an order of instantiation.</p>
  
  <p>The following usage of <code>@Order</code> annotation does not work:</p>
  
  <pre><code>import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.core.annotation.Order;
  
  /**
   * Order does not work here
   */
  public class OrderingOfInstantiation {
  
     public static class MyBean1 {{
        System.out.println(getClass().getSimpleName());
     }}
  
     public static class MyBean2 {{
        System.out.println(getClass().getSimpleName());
     }}
  
     @Configuration
     public static class Config {
  
        @Bean
        @Order(2)
        public MyBean1 bean1() {
           return new MyBean1();
        }
  
        @Bean
        @Order(1)
        public MyBean2 bean2() {
           return new MyBean2();
        }
  
     }
  
     public static void main(String[] args) {
        new AnnotationConfigApplicationContext(Config.class);
     }
  
  }
  </code></pre>
  
  <p>Beans are still instantiated in lexicographic order.</p>
  
  <p>Why it does not work here?</p>
  
  <p>Can I rely of lexicographic order anyway?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I would like any solution allowing to provide order of creation. </p>
  
  <p>The goal is to populate collections at config level in correct order. <code>Depends on</code> -- does not match the task. Any "explanations" on why Spring does not like to have ordered instantiations -- also does not match the task.</p>
  
  <p>Order means order :)</p>