title: Spring @Autowired @Lazy
body: <p>I'm using Spring annotations and I want to use lazy initialization.</p>
  
  <p>I'm running into a problem that when I want to import a bean from another class I am forced to use <code>@Autowired</code> which does not seem to use lazy init. Is there anyway to force this lazy init behaviour?</p>
  
  <p>In this example I do not want to see "Loading parent bean" ever being printed as I am only loading <code>childBean</code> which has no dependencies on <code>lazyParent</code>.</p>
  
  <pre><code>@Configuration
  public class ConfigParent {
      @Bean
      @Lazy
      public Long lazyParent(){
          System.out.println("Loading parent bean");
          return 123L;
      }
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>@Configuration
  @Import(ConfigParent.class)
  public class ConfigChild {
      private @Autowired Long lazyParent;
      @Bean
      public Double childBean() {
          System.out.println("loading child bean");
          return 1.0;
      }
      @Bean
      @Lazy
      public String lazyBean() {
          return lazyParent+"!";
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>public class ConfigTester {
      public static void main(String[] args) {
          ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigChild.class);
          Double childBean=ctx.getBean(Double.class);
          System.out.println(childBean);
  
      }
  
  }
  </code></pre>