springboot 监听器listener的使用

一.监听器

1.1 概述

监听器:主要是针对监听ServletContext、HttpSession、ServletRequest三个域对象以及这三个域对象引发的事件。

springboot 监听器listener的使用

https://blog.csdn.net/baidu_29343517/article/details/86697540

1.2 过滤器,监听器,拦截器

过滤器: https://blog.csdn.net/u011066470/article/details/115376422

监听器: https://blog.csdn.net/u011066470/article/details/108110841

拦截器:  https://mp.csdn.net/editor/html/112644897

二 springboot集成listener

2.1  第一种方式

第一种方式为:ServletContextListener,在启动类上进行@bean进行注册

package com.ljf.spring.boot.demo.spt.listener.controller;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class SecondListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("SecondListener..init.....");
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("SecondListener..destory.....");
    }
}

启动类:

springboot 监听器listener的使用

package com.ljf.spring.boot.demo.spt.listener;

import com.ljf.spring.boot.demo.spt.listener.controller.SecondListener;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class App2 {
    public static void main(String args[]){
        SpringApplication.run(App2.class, args);
        System.out.println( "Hello World!" );
    }
    /**
     * 注册listener
     */
    @Bean
    public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean(){
        ServletListenerRegistrationBean<SecondListener> bean= new ServletListenerRegistrationBean<SecondListener>(new SecondListener());
        return bean;
    }
}

3.测试:

springboot 监听器listener的使用

 2.2  第2种方式:requestListener

直接使用@WebListener注解,使用requestlistener,监听到每一个请求。

package com.ljf.spring.boot.demo.spt.listener.controller;

import org.apache.catalina.filters.ExpiresFilter;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: ReuestListener
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/04/05 11:13:52 
 * @Version: V1.0
 **/
@WebListener
public class ReuestListener implements ServletRequestListener {

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        HttpServletRequest url=(HttpServletRequest) sre.getServletRequest();
        System.out.println("======requestInitialized========"+url.getRequestURL());

    }
    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        // TODO Auto-generated method stub
        System.out.println("======requestDestroyed========");
    }

}

2.启动类:

package com.ljf.spring.boot.demo.spt.listener;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

/**
 * @ClassName: App3
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/04/05 11:16:08 
 * @Version: V1.0
 **/
@SpringBootApplication
@ServletComponentScan
public class App3 {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
        System.out.println("listener3 qidong .....");
    }
}

3.测试

springboot 监听器listener的使用

springboot 监听器listener的使用

 2.3  httpsession的监听器

 

 

上一篇:Android 监测网络切换


下一篇:配置防火墙允许从外网访问内网oracle