我需要在会话侦听器类中自动装配一个用 @Service 注释进行注释的服务类,因为我需要对会话销毁方法执行一些数据库操作。我无法自动装配服务类,因为我已在 web.xml 中添加了侦听器,并且它不再由 spring 管理。我尝试了几种选项(解决方法),例如通过 servlet 上下文从应用程序上下文获取 bean,但我没有以这种方式获取任何 bean。
以下是我的课程:- 我的服务:
@Service
@Transactional
public class FxTransactionService{
//some autowirings
public void performDBoperation(Long id)
{
//business logic
}
}
会话监听器:
public class SessionHandler implements HttpSessionListener {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private MyService myService;
@Override
public void sessionCreated(HttpSessionEvent arg0) {
System.out.println("Session created");
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(arg0.getSession()
.getServletContext());
System.out.println(Arrays.toString(context.getBeanDefinitionNames()));
//This gives me empty list
}
@Override
public void sessionDestroyed(HttpSessionEvent arg0) {
Long id = (Long) arg0.getSession().getAttribute("Id");
myService.performDBoperation(id);
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.abc.controller.SessionHandler</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<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>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<filter>
<filter-name>preAuthHeaderAdditionFilter</filter-name>
<filter-class>com.abc.filter.PreAuthHeaderAdditionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>preAuthHeaderAdditionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- <filter> <filter-name>openEntityManagerInViewFilter</filter-name> <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter> <filter-mapping> <filter-name>openEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern> </filter-mapping> -->
</web-app>
首先安装Spring监听器ContextLoaderListener。 在您自己的侦听器中,您可以使用 WebApplicationContextUtils 访问上下文。 但它不是自动装配,你必须自己获取所需的 bean/服务。
您面临的问题是SessionHandler类不是由Spring管理的,因此Spring无法自动注入依赖项。要解决此问题,您可以从 Spring 应用程序上下文手动获取 MyService bean。方法如下:
确保您的 Spring 上下文已正确加载且可访问。 在 sessionCreated 和 sessionDestroyed 方法中从 WebApplicationContext 检索 MyService bean。
@Override
public void sessionCreated(HttpSessionEvent event) {
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(
event.getSession().getServletContext());
if (context != null) {
myService = context.getBean(MyService.class);
}
logger.info("Session created");
}
@Override
public void sessionDestroyed(HttpSessionEvent event) {
if (myService == null) {
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(
event.getSession().getServletContext());
if (context != null) {
myService = context.getBean(MyService.class);
}
}
Long id = (Long) event.getSession().getAttribute("Id");
if (myService != null) {
myService.performDBoperation(id);
} else {
logger.error("MyService bean is not available");
}
}
从 ServletContext 中检索 WebApplicationContext。 从上下文中获取 MyService bean 并将其存储在 myService 字段中