spring boot 监听器
程序员文章站
2022-03-02 18:29:49
...
1.监听全局唯一对象 ServletContextListener,可以在项目启动时将数据写入缓存,结束时将数据保存到数据库
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class ServletContextListenerimplements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent){
System.err.println("ServletContext 上下文初始化");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent){
System.err.println("ServletContext 上下文销毁");
}
}
2.监听session,和request请求
(1)session,可以用来统计在线人数
import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.*;
import java.util.HashSet;
@WebListener()
public class SessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent event) {
System.err.println("---sessionCreated----");
HttpSession session = event.getSession();
ServletContext application = session.getServletContext();
// 在application范围由一个HashSet集保存所有的session
HashSet sessions = (HashSet) application.getAttribute("sessions");
if (sessions == null) {
sessions = new HashSet();
application.setAttribute("sessions", sessions);
}
// 新创建的session均添加到HashSet集中
sessions.add(session);
// 可以在别处从application范围中取出sessions集合
// 然后使用sessions.size()获取当前活动的session数,即为“在线人数”
}
@Override
public void sessionDestroyed(HttpSessionEvent event) throws ClassCastException {
System.err.println("---sessionDestroyed----");
HttpSession session = event.getSession();
System.err.println("deletedSessionId: "+session.getId());
System.out.println(session.getCreationTime());
System.out.println(session.getLastAccessedTime());
ServletContext application = session.getServletContext();
HashSet sessions = (HashSet) application.getAttribute("sessions");
// 销毁的session均从HashSet集中移除
sessions.remove(session);
}
}
(2)request
@WebListener()
public class RequestListener implements ServletRequestListener {
@Override
public void requestInitialized(ServletRequestEvent sre){
HttpServletRequest request=(HttpServletRequest) sre.getServletRequest();
String s=request.getParameter("s");
System.err.println("request initialized");
}
@Override
public void requestDestroyed(ServletRequestEvent sre){
System.err.println("request destroyed");
}
}
3.监听全局上下文属性(ServletContextAttributeListener),session属性(HttpSessionAttributeListener),request属性(ServletRequestAttributeListener)
import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.*;
import java.util.HashSet;
@WebListener()
public class SessionListener implements HttpSessionAttributeListener{
@Override
public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
System.err.println("--attributeAdd--");
HttpSession session=httpSessionBindingEvent.getSession();
System.err.println(httpSessionBindingEvent.getName());
System.err.println(httpSessionBindingEvent.getValue());
}
@Override
public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
System.err.println("--attributeRemoved--");
System.err.println(httpSessionBindingEvent.getName());
System.err.println(httpSessionBindingEvent.getValue());
}
@Override
public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
System.err.println("--attributeReplaced--");
System.err.println(httpSessionBindingEvent.getName());
System.err.println(httpSessionBindingEvent.getValue());
}
}
推荐阅读
-
Spring Boot+Mybatis的整合过程
-
myeclipse创建spring配置文件正确却显示红叉该怎么办?
-
Spring Boot 编写Servlet、Filter、Listener、Interceptor的方法
-
Spring Cloud Gateway入门解读
-
Spring Boot容器加载时执行特定操作(推荐)
-
自己动手在Spring-Boot上加强国际化功能的示例
-
3行代码快速实现Spring Boot Oauth2服务功能
-
spring boot容器启动流程
-
详解spring cloud整合Swagger2构建RESTful服务的APIs
-
spring boot 本地图片不能加载(图片路径)的问题及解决方法