java web会话监听并使用spring注入
Java  /  管理员 发布于 3年前   199
在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/
路人 在
php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例中评论 教程很详细,如果加个前端chatgpt对话页面就完美了..博主 在
科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 @ mashrdn 多切换几个节点测试,免费ssr是没那么稳..mashrdn 在
科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 V2rayn免费节点添加上去了,youtobe无法打开网页,是怎么回事..张伟 在
科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 3q!有用,不过免费节点隔天就要去git上复制新的导进去..博主 在
科学上网翻墙访问Google , 上外网神器佛跳墙VPN(永久免费)使用流程步骤中评论 该篇教程已不能用了,告知大家,免的老有老铁问我!..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号