HttpSessionListener 实现session管理

把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 的错误

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值