我使用 Spring 注释开发一个 Web 应用程序来注入(inject)我的 Controller 和服务。在应用程序中,我有用户在处理项目,但我需要确保一个项目一次只能由一名用户编辑,因此我在数据库的“项目”表中有一个属性,用于在项目打开时保存谁。
我需要添加一个 HTTPSessionListener 以便能够在编辑项目的用户断开连接时“关闭”该项目。这意味着在“sessionDestroyed”事件中,我想调用我的 DAO 服务来更新数据库中的项目。 唯一的问题是这个服务是由 Spring 注入(inject)的,我无法获取它......
我尝试在我的 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 服务注入(inject)到我的 HTTPSessionListener 中吗?
预先感谢您的帮助!
最佳答案
其中一个选项(从设计角度来看可能是最好的选项)是创建 a bean of scope session
(不要忘记声明RequestContextListener)并将与 session 生命周期相关的逻辑放入其中。当 session 要被销毁时,Spring 会自动调用其销毁方法(使用 @PreDestroy
注解或配置为 destroy-method
)。
您注入(inject) session 监听器的方法会导致问题,因为您只有 DispatcherServlet
,而不是 ContextLoaderListener
,并且 WebApplicationContextUtils
无法获取关联的应用程序上下文使用DispatcherServlet。如果您选择遵循该方法,您应该创建一个根应用程序上下文并将一些 Bean 提取到其中,然后您将能够通过 WebApplicationContextUtils
从 session 监听器访问它。
关于spring - 使用 Spring 注入(inject)和服务的 HTTPSessionListener => 无法访问我的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11718216/