• SpringBoot------Servlet3.0的注解自定义原生Listener监听器


    前言

    常用监听器:
    //contextListener可以监听数据库的连接,第三方组件的交互,还有静态文件加载等等
    servletContextListener
    HttpSessionListener
    servletRequestListener

    1.添加pom.xml相关依赖

    <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>top.ytheng</groupId>
      <artifactId>springboot-demo</artifactId>
      <version>0.0.1</version>
      <packaging>jar</packaging>
      
      <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
            
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <optional>true</optional>
                  <scope>true</scope>
            </dependency>
        </dependencies>
    
        <build>
            <!-- 打包的名称 -->
            <finalName>myspringboot</finalName>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

    2.添加自定义ContextListener监听器

    package top.ytheng.demo.listener;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;
    
    /*
     * 上下文监听器
     * 
     * */
    @WebListener
    public class ContextListener implements ServletContextListener {
    
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            // TODO Auto-generated method stub
            System.out.println("======contextInitialized======");
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            // TODO Auto-generated method stub
            System.out.println("======contextDestroyed======");
        }
        
    }

    3.添加自定义RequestListener

    package top.ytheng.demo.listener;
    
    import javax.servlet.ServletRequestEvent;
    import javax.servlet.ServletRequestListener;
    import javax.servlet.annotation.WebListener;
    
    @WebListener
    public class RequestListener implements ServletRequestListener{
    
        @Override
        public void requestDestroyed(ServletRequestEvent sre) {
            // TODO Auto-generated method stub
            System.out.println("======requestDestroyed======");
        }
    
        @Override
        public void requestInitialized(ServletRequestEvent sre) {
            // TODO Auto-generated method stub
            System.out.println("======requestInitialized======");
        }
        
    }

    4.添加测试控制器

    package top.ytheng.demo.controller;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/api/v2/listener")
    public class ListenerController {
        
        @GetMapping("/test")
        public Object testListener() {
            Map<String, Object> map = new HashMap<>();
            map.put("username", "theng");
            map.put("password", "123456");
            
            System.out.println("listenerController");
            return map;
        }
    }

    5.添加启动类

    package top.ytheng.demo;
    
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    import org.springframework.boot.web.servlet.ServletComponentScan;
    
    @SpringBootApplication //等于下面3个
    //@SpringBootConfiguration
    //@EnableAutoConfiguration
    //@ComponentScan
    //拦截器用到
    @ServletComponentScan
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }

    6.右键项目Run As启动,访问地址

    http://localhost:8080/api/v2/listener/test

    另附:

  • 相关阅读:
    我为能准时下班而做的准备,以及由此的收获,同时总结下不足
    用象棋的思维趣说IT人的职业发展和钱途
    简历上如果出现过于高大上的项目,反而过犹不及:再论如何通过项目引出技术
    用python的matplotlib和numpy库绘制股票K线均线的整合效果(含从网络接口爬取数据和验证交易策略代码)
    如果当前没有拿得出手的简历,也别慌,努力的话最多两年情况就能改变
    分析若干没面试机会和没体现实力的简历
    IT人为了自己父母和家庭,更得注意自己的身体和心理健康
    Spring Cloud系列文,Feign整合Ribbon和Hysrix
    以互联网公司的经验告诉大家,架构师究竟比高级开发厉害在哪?
    博客园是个大金矿,管理员不挖掘有些可惜:给博客园提一些双赢的建议
  • 原文地址:https://www.cnblogs.com/tianhengblogs/p/9782475.html
Copyright © 2020-2023  润新知