title: Spring - Injecting a dependency into a ServletContextListener
body: <p>I would like to inject a dependency into a <code>ServletContextListener</code>. However, my approach is not working. I can see that Spring is calling my setter method, but later on when <code>contextInitialized</code> is called, the property is <code>null</code>.</p>
  
  <p>Here is my set up:</p>
  
  <p><strong>The ServletContextListener:</strong></p>
  
  <pre><code>public class MyListener implements ServletContextListener{
  
      private String prop;
  
      /* (non-Javadoc)
       * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
       */
      @Override
      public void contextInitialized(ServletContextEvent event) {
          System.out.println("Initialising listener...");
          System.out.println(prop);
      }
  
      @Override
      public void contextDestroyed(ServletContextEvent event) {
      }
  
      public void setProp(String val) {
          System.out.println("set prop to " + prop);
          prop = val;
      }
  }
  </code></pre>
  
  <p><strong>web.xml: (this is the last listener in the file)</strong></p>
  
  <pre><code>&lt;listener&gt;
    &lt;listener-class&gt;MyListener&lt;/listener-class&gt;
  &lt;/listener&gt; 
  </code></pre>
  
  <p><strong>applicationContext.xml:</strong></p>
  
  <pre><code>&lt;bean id="listener" class="MyListener"&gt;
    &lt;property name="prop" value="HELLO" /&gt;
  &lt;/bean&gt;  
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>set prop to HELLO
  Initialising listener...
  null
  </code></pre>
  
  <p>What is the correct way to achieve this?</p>