title: Shutdown spring application from ApplicationListener
body: <p>Is it possible to close the context from an ApplicationListener?</p>
  
  <pre><code>   public class MyListener implements ApplicationListener&lt;ContextRefreshedEvent&gt; {
  
      @Override
          public void onApplicationEvent(ContextRefreshedEvent event) {
              // on some certain condition we want to shutdown spring ie close the context
          ((ConfigurableApplicationContext)event.getApplicationContext()).close();
          }
      }
  </code></pre>
  
  <p>The problem is that Spring still wants to finish the startup process here:</p>
  
  <pre><code>public static void main(String[] args) {
          SpringApplication.run(MyApplication.class, args);
  }
  </code></pre>
  
  <p>And therefore throws an IllegalStateException:</p>
  
  <blockquote>
    <p>java.lang.IllegalStateException:
    org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@dcb84c98
    has been closed already</p>
  </blockquote>