欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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());
    }
}