【问题标题】:HTTPSessionListener using Spring injections and Services => impossible to access my beansHTTPSessionListener 使用 Spring 注入和服务 => 无法访问我的 bean
【发布时间】:2012-07-27 22:15:09
【问题描述】:

我使用 Spring 注释 开发一个网络应用程序来注入我的控制器和服务。在应用程序中,我有用户在处理项目,但我需要确保一个项目一次只能由一个用户编辑,所以我在数据库的“项目”表中有一个属性,如果项目打开并且通过谁。

我需要添加一个 HTTPSessionListener 以便能够在用户编辑项目断开连接时“关闭”项目。这意味着在“sessionDestroyed”事件中,我想调用我的 DAO 服务来更新数据库中的项目。 唯一的问题是这个服务是Spring注入的,我拿不到...

我尝试在我的 HTTPSessionListener 中使用 @Autowired 但它不起作用,我尝试在此解决方案 (Spring – How to do dependency injection in your session listener ) 中这样做,但我得到了 WebApplicationContext 的 nullPointerException...

我的 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>

还有 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"/>

所以我需要你的帮助...有人知道如何将 Spring 服务注入到我的 HTTPSessionListener 中吗?

提前感谢您的帮助!

【问题讨论】:

  • 我用 web.xml 更新了我的帖子!谢谢

标签: spring jakarta-ee service dependency-injection httpsession


【解决方案1】:

其中一个选项(从设计的角度来看可能是最好的选项)是创建a bean of scope session(不要忘记声明RequestContextListener)并将与会话生命周期相关的逻辑放入其中。当 session 被销毁时,Spring 会自动调用其销毁方法(注解@PreDestroy 或配置为destroy-method)。

您注入会话侦听器的方法会导致问题,因为您只有DispatcherServlet,没有ContextLoaderListener,并且WebApplicationContextUtils 无法获得与DispatcherServlet 关联的应用程序上下文。如果您选择遵循该方法,您应该创建一个根应用程序上下文并将一些 bean 提取到其中,然后您将能够通过 WebApplicationContextUtils 从会话侦听器访问它。

【讨论】:

  • 您是否有完整的示例说明您的描述解决方案将如何实现?我有着同样的问题。然而,我也尝试使用 WebApplicationContextUtils 进行 bean 查找,但仍然没有运气。提前致谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-28
  • 1970-01-01
  • 2014-02-21
  • 2011-01-26
  • 2017-10-23
  • 2018-12-12
相关资源
最近更新 更多