title: Bean Lifecycle Management Spring Boot
body: <p>I am currently trying to deploy a Spring Boot Application into an external Tomcat instance, and am running into some questions regarding how to best manage the instantiation of certain things.</p>
  
  <p>As presently structured I have something along the lines of </p>
  
  <pre><code>public class MyClass extends SpringBootServletInitializer{
  
  
  @Bean
  public ThreadPool pool(){
      return new ThreadPool();
  }
  
  @Bean
  public BackgroundThread setupInbox() {
      BackgroundThread inbox = new BackgroundThread(pool());
      inbox.start();
      return inbox;
  }
  
  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
      return application.sources(MyClass.class);
  }
  
  public static void main(String[] args) throws Exception {
      SpringApplication.run(MyClass.class, args);
  }
  
  
  }
  </code></pre>
  
  <p>where BackgroundThread is a thread that is listening to an AMQP type messaging queue for new jobs. I know Spring offers some RabbitMQ ways to do this, but we are not using Rabbit for this so it doesn't help.</p>
  
  <p>The entire purpose of this *.war file that is being deployed is to expose some functionality to the wire via messaging, so my question is what is the best way to instantiate, start and then destroy the BackgroundThread through the lifecycle of Spring? XML Configuration? </p>