title: Run an ephemeral Spring Boot app out of main class or a startup listener?
body: <p>Java 8 and Spring Boot 2.x here. I've used Spring Boot for the last several years to write web apps and RESTful web services alike, and I absolutely love it.</p>
  
  <p>I now have a need to write a bunch of short-lived (non-service/daemon) apps that need to start up, do some work, and shut down. Exact length of runtime will vary depending on workload but never more than ~10 minutes. Although they do <em>not</em> involve hosting any server-side HTTP code (REST endpoints, controllers, etc.) like an traditional web app/service does, they do still need:</p>
  
  <ul>
  <li>Dependency injection</li>
  <li>YAML-based external configuration</li>
  <li>Auditing</li>
  <li>Metrics</li>
  <li><em>Lots</em> of other stuff that comes with stock Spring Boot</li>
  </ul>
  
  <p>So I don't see any reason why I can't use Spring Boot as the underlying framework even though these apps will not be standing up any <code>@Controllers</code> or <code>@RestControllers</code>.</p>
  
  <p>With Spring Boot, if you don't specify any controllers, it simply starts up and shuts down, because there's nothing to execute. So the question becomes: where do I put my main code that needs to execute? And the two logical places I've thought of are:</p>
  
  <ol>
  <li>Right inside the main <code>@SpringBootApplication</code>-annotated class where the <code>main</code> method is also defined; or, possibly...</li>
  <li>In an <code>ApplicationListener&lt;ContextStartedEvent&gt;</code> implementation</li>
  </ol>
  
  <p>I'm wondering if there is a <strong>right/wrong</strong> approach here. I guess I just don't know enough about the Spring/Boot lifecycle (and the complicated order of events that things execute in) to know which is correct.</p>
  
  <p>For instance, what if it takes Spring Boot a certain amount of time to fire up properly, but I put my code all inside of the main <code>@SpringBootApplication</code>-annotated class and it executes in a few seconds, and prompts Spring to shut down before its even properly finished starting up fully? So I guess the question comes down to this, does one use:</p>
  
  <pre><code>@SpringBootApplication
  public class MyApp {
      public static void main(String[] args) {
          SpringApplication.run(MyApp.class, args);
          new MyApp().run();
      }
  
      private void run() {
          // put all the app code in here?
      }
  }
  </code></pre>
  
  <p><strong>Or</strong>, something like:</p>
  
  <pre><code>@Component
  public class StartupListener implements ApplicationListener&lt;ContextStartedEvent&gt; {
      @Override
      public void onApplicationEvent(ContextStartedEvent event) {
          // put all the app code in here?
      }
  }
  </code></pre>
  
  <p>Considerations of either approach? Does Spring Boot offer anything better than either of these solutions, perhaps, something specifically intended for short-lived/ephemeral applications?</p>