title: Spring: @Autowired not working with ApplicationContext
body: <p>So I just wanted to print all the beans that have been loaded, and I understand the <code>getBeanDefinitionNames()</code> method is what a lot of people suggest. For this I understand you need an <code>ApplicationContext</code> which I autowired as below, but I am getting a null pointer exception on the object when I call <code>getBeanDefinitionNames()</code> on it:</p>
  
  <pre><code>package hello;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.ApplicationContext;
  
  public class BeansPrinter 
  {
      @Autowired
      private ApplicationContext appContext;
  
      public void printBeans()
      {
          System.out.println("Number of beans:");
          System.out.println(appContext.getBeanDefinitionCount());
  
          String[] names = appContext.getBeanDefinitionNames();
          for(String name : names)
          {
              System.out.println("-----------------");
              System.out.println(name);
          }
      }
  }
  </code></pre>
  
  <p><strong>Application.java</strong></p>
  
  <pre><code>package hello;
  
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  
  @SpringBootApplication
  public class Application {
  
      public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
  
          BeansPrinter b = new BeansPrinter();
          b.printBeans();
      }
  }
  </code></pre>