title: I can't seem to get an implementation of Spring's Lifecycle working
body: <p>I need something to happen immediately after Spring's application context is loaded. As I understand it, I need to create an implementation of Lifecycle and put a bean reference inside the context. So I have something like this in my context:</p>
  
  <pre><code>&lt;bean id="mySpringLifecycle" class="com.my.project.MySpringLifecycle" /&gt;
  </code></pre>
  
  <p>The class looks something like this:</p>
  
  <pre><code>public class MySpringLifecycle implements Lifecycle {
  
      @Override
      public void start() {
          System.out.println("The lifecycle has started.");
      }
  
      @Override
      public void stop() {
          return;
      }
  
      @Override
      public boolean isRunning() {
          return true;
      }
  }
  </code></pre>
  
  <p>I get no errors, but MySpringLifecycle never prints out "The lifecycle has started.", and my application starts just fine.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>This is the fixed code:</p>
  
  <pre><code>public class MySpringLifecycle implements SmartLifecycle {
  
      private volatile boolean isRunning = false;
  
      @Override
      public boolean isAutoStartup() {
          return true;
      }
  
      @Override
      public void stop(Runnable r) {
          System.out.println("STOPPED RUNNABLE!!!");
          isRunning = false;
      }
  
      @Override
      public void start() {
          System.out.println("STARTED!!!");
          isRunning = true;
      }
  
      @Override
      public void stop() {
          System.out.println("STOPPED!!!");
          isRunning = false;
      }
  
      @Override
      public boolean isRunning() {
          return isRunning;
      }
  
      @Override
      public int getPhase() {
          return 1;
      }
  }
  </code></pre>
  
  <p>As a side note I also wanted to mention an alternative solution that I may use as well. I have the following in my web.xml:</p>
  
  <pre><code>&lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/spring.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  </code></pre>
  
  <p>The ContextLoaderListener has a method on it called contextInitialized. So what I did was created my own implementation and added that to the web.xml instead. In my implementation I extended ContextLoaderListener, provided an override for the contextInitialized method, called super for that method first, and then executed my own functionality. This gets executed only once, and appears to work well.</p>