一.监听器
1.1 概述
监听器:主要是针对监听ServletContext、HttpSession、ServletRequest三个域对象以及这三个域对象引发的事件。
Java Web中过滤器和监听器重点总结_妖精小狗的博客-CSDN博客_过滤器和监听器
1.2 过滤器,监听器,拦截器
过滤器: springboot的filter 过滤器的使用_健康平安的活着的专栏-CSDN博客
监听器: springboot之HttpSessionListener的用法创建,销毁_健康平安的活着的专栏-CSDN博客_httpsessionlistener
拦截器: CSDN
拦截器:springboot整合拦截器intercepter_健康平安的活着的专栏-CSDN博客_springboot 拦截器
二 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.....");
}
}
启动类:
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.测试:
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.测试
2.3 httpsession的监听器
springboot之HttpSessionListener的用法创建,销毁_健康平安的活着的专栏-CSDN博客_httpsessionlistener