1.依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.khue</groupId>
<artifactId>Practice</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
</dependency>
</dependencies>
</project>
2.启动类
package cn.khue.practice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
3.自定义过滤器
package cn.khue.practice.listener_integration;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.*;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
@WebListener
@Slf4j
public class CustomRequestListener implements ServletRequestListener {
@Override
public void requestDestroyed(ServletRequestEvent sre) {
log.info("Request监听器注销"+sre.getServletRequest().getScheme());
}
@Override
public void requestInitialized(ServletRequestEvent sre) {
log.info("Request监听器初始化"+sre.getServletRequest().getScheme());
}
}
@WebListener
@Slf4j
class CustomRequestAttributeListener implements ServletRequestAttributeListener{
@Override
public void attributeAdded(ServletRequestAttributeEvent srae) {
log.info("新增Request参数"+srae.getName());
}
@Override
public void attributeRemoved(ServletRequestAttributeEvent srae) {
log.info("移除Request参数"+srae.getName());
}
@Override
public void attributeReplaced(ServletRequestAttributeEvent srae) {
log.info("替换Request参数"+srae.getName());
}
}
@WebListener
@Slf4j
class CustomSessionListener implements HttpSessionListener{
@Override
public void sessionCreated(HttpSessionEvent se) {
log.info("Session监听器初始化"+se.getSession().getId());
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
log.info("Session监听器注销"+se.getSession().getId());
}
}
@WebListener
@Slf4j
class CustomSessionAttributeListener implements HttpSessionAttributeListener{
@Override
public void attributeAdded(HttpSessionBindingEvent se) {
log.info("新增Session参数"+se.getName());
}
@Override
public void attributeRemoved(HttpSessionBindingEvent se) {
log.info("移除Session参数"+se.getName());
}
@Override
public void attributeReplaced(HttpSessionBindingEvent se) {
log.info("替换Session参数"+se.getName());
}
}
@WebListener
@Slf4j
class CustomServletContextListener implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent sce) {
log.info("ServletContext监听器初始化"+sce.getServletContext().getServletContextName());
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
log.info("ServletContext监听器注销"+sce.getServletContext().getServletContextName());
}
}
@WebListener
@Slf4j
class CustomServletContextAttributeListener implements ServletContextAttributeListener{
@Override
public void attributeAdded(ServletContextAttributeEvent scae) {
log.info("新增ServletContext参数"+scae.getName());
}
@Override
public void attributeRemoved(ServletContextAttributeEvent scae) {
log.info("移除ServletContext参数"+scae.getName());
}
@Override
public void attributeReplaced(ServletContextAttributeEvent scae) {
log.info("替换ServletContext参数"+scae.getName());
}
}