把sessionId绑定到user身上(db),在session失效时解绑
首先在web.xml中定义:
<listener>
<listener-class>com.yto.zhiyun.listener.LogoutSessionListener</listener-class>
</listener>
LogoutSessionListener 代码:
public class LogoutSessionListener implements HttpSessionListener {
private static final Logger LOGGER = Logger.getLogger(LogoutSessionListener.class);
/* (non-Javadoc)
* @see javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent)
*/
@Override
public void sessionCreated(HttpSessionEvent se) {
}
/* (non-Javadoc)
* @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
*/
@Override
public void sessionDestroyed(HttpSessionEvent se) {
//update user sessionId
HttpSession session = se.getSession();
User user = (User) session.getAttribute("user");
final UserRequestBean req1 = new UserRequestBean();
req1.setType(RequestType.COMMON_USER_LOGOUT);
req1.setUserParam(user);
ApplicationContext cox = WebApplicationContextUtils.getWebApplicationContext(session.getServletContext(), "org.springframework.web.servlet.FrameworkServlet.CONTEXT.appServlet");
ClientService clientService = (ClientService) cox.getBean("clientService");
clientService.service(req1);
LOGGER.info("session destroyId, user logout:" + user.getId());
}
}
这边有几个地方要注意:
1. 获取ApplicationContext, 使用默认的方式WebApplicationContextUtils.getWebApplicationContext(session.getServletContext())取到的将会是root的applicationContext
就是web.xml中定义的
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
而真正的bean都是定义在DispatcherServlet中的
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
而DispatcherServlet的webapplicationContext与root是不同的,需要通过
WebApplicationContextUtils.getWebApplicationContext(ServletContext sc, String attributeName)
来得到
而DispatchServlet 默认的name是在DispatchServlet的父类FrameworkServlet中定义的
public class DispatcherServlet extends FrameworkServlet
其中的
protected WebApplicationContext initWebApplicationContext() {
WebApplicationContext rootContext =
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebApplicationContext wac = null;
if (this.webApplicationContext != null) {
// A context instance was injected at construction time -> use it
wac = this.webApplicationContext;
if (wac instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
if (!cwac.isActive()) {
// The context has not yet been refreshed -> provide services such as
// setting the parent context, setting the application context id, etc
if (cwac.getParent() == null) {
// The context instance was injected without an explicit parent -> set
// the root application context (if any; may be null) as the parent
cwac.setParent(rootContext);
}
configureAndRefreshWebApplicationContext(cwac);
}
}
}
if (wac == null) {
// No context instance was injected at construction time -> see if one
// has been registered in the servlet context. If one exists, it is assumed
// that the parent context (if any) has already been set and that the
// user has performed any initialization such as setting the context id
wac = findWebApplicationContext();
}
if (wac == null) {
// No context instance is defined for this servlet -> create a local one
wac = createWebApplicationContext(rootContext);
}
if (!this.refreshEventReceived) {
// Either the context is not a ConfigurableApplicationContext with refresh
// support or the context injected at construction time had already been
// refreshed -> trigger initial onRefresh manually here.
onRefresh(wac);
}
if (this.publishContext) {
// Publish the context as a servlet context attribute.
String attrName = getServletContextAttributeName();
getServletContext().setAttribute(attrName, wac);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() +
"' as ServletContext attribute with name [" + attrName + "]");
}
}
return wac;
}
其中的
public String getServletContextAttributeName() {
return SERVLET_CONTEXT_PREFIX + getServletName();
}
常量为:
public static final String SERVLET_CONTEXT_PREFIX = FrameworkServlet.class.getName() + ".CONTEXT.";
最后确认为:
org.springframework.web.servlet.FrameworkServlet.CONTEXT.appServlet
而一开始没注意,一直用Root的webapplicationContext
总是报NoSuchBean 的错误