title: Understanding Spring Boot @Autowired
body: <p>I don't understand how spring boot's annotation <code>@Autowired</code> correctly works. Here is a simple example:</p>
  
  <pre><code>@SpringBootApplication
  public class App {
      @Autowired
      public Starter starter;
  
      public static void main(String[] args) {
          SpringApplication.run(App.class, args);
      }
      public App() {
          System.out.println("init App");
          //starter.init();
      }
  }
  </code></pre>
  
  <p>--</p>
  
  <pre><code>@Repository
  public class Starter {
      public Starter() {System.out.println("init Starter");}
      public void init() { System.out.println("call init"); }
  }
  </code></pre>
  
  <p>When I execute this code, I get the logs <code>init App</code> and <code>init Starter</code>, so spring creates this objects. But when I call the init method from <code>Starter</code> in <code>App</code>, I get a <code>NullPointerException</code>. Is there more I need to do other than using the annotation <code>@Autowired</code> to init my object?</p>
  
  <pre><code>Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'app': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [{package}.App$$EnhancerBySpringCGLIB$$87a7ccf]: Constructor threw exception; nested exception is java.lang.NullPointerException
  </code></pre>