Spring Boot 整合 Listener

Spring Boot 整合 Listener

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>

    <!--引入spring boot-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
    </parent>

    <dependencies>
        <!--web相关依赖-->
        <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;

/**
 * @author khue
 * @description 启动类
 * @date 2020-11-05
 * */
@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;

/**
 * @author khue
 * @description 自定义监听器
 * @date 2020-11-05
 * */
@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());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值