title: How to make autowire object initialized in ApplicationListener?
body: <p>I want to read data in ApplicationListener, but my object is not initialized. Below is my code:</p>
  
  <p><strong>AppContextListener.java</strong></p>
  
  <pre><code>@Component
  public class AppContextListener implements ApplicationListener&lt;ContextRefreshedEvent&gt; {
      @Override
      public void onApplicationEvent(ContextRefreshedEvent event) {       
          AppContext.getInstance();
      }
  }
  </code></pre>
  
  <p><strong>AppContext.java</strong></p>
  
  <pre><code>public class AppContext {    
      private static AppContext instance;
  
      @Autowired
      MyElasticsearchRepository repository;
  
      public AppContext(){
          InitData();
      }
  
      public static synchronized AppContext getInstance() {
          if (instance == null) {
              instance = new AppContext();
          }
          return instance;
      }   
  
      private void InitData(){       
          List&lt;MyEntity&gt; dataList = repository.findAllEntities();//repository is null here
          //.......
      }   
  }
  </code></pre>
  
  <p><strong>MyElasticsearchRepository.java</strong></p>
  
  <pre><code> public interface MyElasticsearchRepository extends ElasticsearchRepository&lt;MyEntity,String&gt;
   { }
  </code></pre>
  
  <p><strong>problem</strong></p>
  
  <blockquote>
    <p>As you can see in my code, at InitData(), repository is null. I don't
    know why <code>@Autowired MyElasticsearchRepository repository;</code> does not
    worked here.</p>
  </blockquote>
  
  <p>Please show me how to fix this. Thank you very much.</p>