2

I work on a web app using Spring annotations 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.

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...

I tried to use @Autowired in my HTTPSessionListener but it didn't work, I tried to do like in this solution (Spring – How to do dependency injection in your session listener ) but I get a nullPointerException for the WebApplicationContext...

My web.xml :

 <?xml version="1.0" encoding="UTF-8"?>
 <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">

<!-- jsp config => automatic inclusions in all jsp files -->
<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <include-prelude>taglibs.jsp</include-prelude>
        <include-prelude>setLanguage.jsp</include-prelude>
    </jsp-property-group>
</jsp-config>
<display-name>MEANS</display-name>

<!--Spring dispatcher servlet -->
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.do</url-pattern><!-- detect all urls ending with ".do" -->
</servlet-mapping>

<!-- The welcome files -->
<welcome-file-list>
    <welcome-file>connection.jsp</welcome-file>
</welcome-file-list>
<session-config>
    <session-timeout>30</session-timeout><!-- the session is automatically disconnected after 30 min of inactivity -->
</session-config>
<listener>
    <listener-class>myPackages.listener.MySessionListener</listener-class>
</listener>

And the dispatcher-servlet.xml :

 <?xml version="1.0" encoding="UTF-8"?>
 <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">


<!-- Spring MVC Support for annotations (JSR-303) -->
<mvc:annotation-driven/>

<!-- Spring View resolver => find the jsps -->
<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:suffix=".jsp" />

<!--  Spring info : what packages to scan for detecting annotations -->
<context:component-scan base-package="myPackages"/>

So I need your help... Does anybody have an idea of how I could inject a Spring Service into my HTTPSessionListener?

Thanks in advance for your help!

Coralie
  • 107
  • 3
  • 17

1 Answers1

3

One of the options (perhaps the best one from the design point of view) is to create a bean of scope session (don't forget to declare RequestContextListener) and put logic associated with the session lifecylce into it. Spring will automatically call its destruction method (annotated with @PreDestroy or configured as destroy-method) when session is to be destroyed.

Your approach with injection into session listener causes problems because you only have DispatcherServlet, not ContextLoaderListener, and WebApplicationContextUtils cannot obtain an application context associated with DispatcherServlet. If you choose to follow that apporach you should create a root application context and extract some of your beans into it, then you will be able to access it from session listener via WebApplicationContextUtils.

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • Do you have a complete example of how your describe solution would be achieved? I have this same problem. Yet I also tried using WebApplicationContextUtils for my bean lookup and still no luck. Thanks in advance – stackoverflow Dec 10 '13 at 13:29