java web会话监听并使用spring注入
Java  /  管理员 发布于 5年前   365
在java web应用中,当你想在建立会话或移除会话时,让系统做某些事情,比如说,统计在线用户,每当有用户登录时,或退出时,那么可以用下面这个监听器来监听。
import java.util.ArrayList;import java.util.List;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.http.HttpSessionAttributeListener;import javax.servlet.http.HttpSessionBindingEvent;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.context.support.WebApplicationContextUtils;public class OnlineCountListener implements HttpSessionAttributeListener ,HttpSessionListener,ServletContextListener{/** * 定义监听的session属性名. */public final static String LISTENER_NAME = "user_name";/** * 定义存储客户登录session的集合. */private static List sessions = new ArrayList();private static int userSum=0;/** * session加入某值时此方法会被调用. * * @param HttpSessionBindingEvent * session事件 */public void attributeAdded(HttpSessionBindingEvent sbe) {if (LISTENER_NAME.equals(sbe.getName())) {sessions.add(sbe.getValue());}}/** * session失效时的监听方法. * * @param HttpSessionBindingEvent * session事件 */public void attributeRemoved(HttpSessionBindingEvent sbe) {if (LISTENER_NAME.equals(sbe.getName())) {sessions.remove(sbe.getValue());}}/** * session覆盖时的监听方法. * * @param HttpSessionBindingEvent * session事件 */public void attributeReplaced(HttpSessionBindingEvent sbe) {if (LISTENER_NAME.equals(sbe.getName())) {sessions.remove(sbe.getValue());sessions.add(sbe.getValue());}}public void sessionCreated(HttpSessionEvent arg) {userSum++;}public void sessionDestroyed(HttpSessionEvent arg) {userSum--;}/** * 返回客户登录session的集合. * * @return */public static List getSessions() {return sessions;}public static int getMemberSum() {return sessions.size();}public static int getSum() {return userSum;}public static Boolean isExistInSessions(String username) {String user_name;for (Integer i = 0; i < sessions.size(); i++) {user_name = (String) sessions.get(i);if (user_name.equals(username)) {return true;} else {continue;}}return false;}//此方法在初始化监听器的时候会调用,用于spring注入public void contextInitialized(ServletContextEvent sce) { WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()); this.xxxService=(XxxService)applicationContext.getBean("xxxService"); } public void contextDestroyed(ServletContextEvent sce) { } private XxxService xxxService;//这里是你希望注入的服务类 public XxxService getXxxService(){ return this.xxxService; } public void setxxxService(XxxService xxxService){ this.xxxService=xxxService; }}
在web.xml中加入以下配置代码,注意必须在spring容器配置之后。
<listener> <listener-class> com.xxx.xxx.xxx.OnlineCountListener </listener-class> </listener>
然后在spring配置
<bean id="xxxService">xxx.xxx.xxx.xxxService</bean>
我的网店,有劳各位参观参观 http://mrs-x.taobao.com/
123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..路人 在
php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例中评论 教程很详细,如果加个前端chatgpt对话页面就完美了..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号