title: HTTPSessionListener using Spring injections and Services => impossible to access my beans
body: <p>I work on a web app using <strong>Spring annotations</strong> to inject my Controllers and Services. In the app, I have users working on projects but I need to make sure one project is only edited by one user at a time so I have an attribute in the table "Project" in the database to save if the project is open and by whom. </p>
  
  <p>I need to add an HTTPSessionListener to be able to "close" the project when the user editing it disconnects. It means that at the "sessionDestroyed" event, I want to call my DAO service to update the project in the database.
  The only problem is that this service is injected by Spring and I cannot get it... </p>
  
  <p>I tried to use @Autowired in my HTTPSessionListener but it didn't work, I tried to do like in this solution (<a href="http://www.mkyong.com/spring/spring-how-to-do-dependency-injection-in-your-session-listener/" rel="nofollow">Spring – How to do dependency injection in your session listener </a>) but I get a nullPointerException for the WebApplicationContext...</p>
  
  <p>My web.xml :</p>
  
  <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt;
   &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp"
  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  id="WebApp_ID" version="3.0"&gt;
  
  &lt;!-- jsp config =&gt; automatic inclusions in all jsp files --&gt;
  &lt;jsp-config&gt;
      &lt;jsp-property-group&gt;
          &lt;url-pattern&gt;*.jsp&lt;/url-pattern&gt;
          &lt;include-prelude&gt;taglibs.jsp&lt;/include-prelude&gt;
          &lt;include-prelude&gt;setLanguage.jsp&lt;/include-prelude&gt;
      &lt;/jsp-property-group&gt;
  &lt;/jsp-config&gt;
  &lt;display-name&gt;MEANS&lt;/display-name&gt;
  
  &lt;!--Spring dispatcher servlet --&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
      &lt;url-pattern&gt;*.do&lt;/url-pattern&gt;&lt;!-- detect all urls ending with ".do" --&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;!-- The welcome files --&gt;
  &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;connection.jsp&lt;/welcome-file&gt;
  &lt;/welcome-file-list&gt;
  &lt;session-config&gt;
      &lt;session-timeout&gt;30&lt;/session-timeout&gt;&lt;!-- the session is automatically disconnected after 30 min of inactivity --&gt;
  &lt;/session-config&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;myPackages.listener.MySessionListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  </code></pre>
  
  <p></p>
  
  <p>And the dispatcher-servlet.xml :</p>
  
  <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt;
   &lt;beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:p="http://www.springframework.org/schema/p"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/mvc
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  
  
  &lt;!-- Spring MVC Support for annotations (JSR-303) --&gt;
  &lt;mvc:annotation-driven/&gt;
  
  &lt;!-- Spring View resolver =&gt; find the jsps --&gt;
  &lt;bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:suffix=".jsp" /&gt;
  
  &lt;!--  Spring info : what packages to scan for detecting annotations --&gt;
  &lt;context:component-scan base-package="myPackages"/&gt;
  </code></pre>
  
  <p></p>
  
  <p>So I need your help... Does anybody have an idea of how I could inject a Spring Service into my HTTPSessionListener?</p>
  
  <p>Thanks in advance for your help!</p>