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 {@Overridepublic void contextInitialized(ServletContextEvent sce) {// TODO Auto-generated method stubSystem.out.println("======contextInitialized======");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {// TODO Auto-generated method stubSystem.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{@Overridepublic void requestDestroyed(ServletRequestEvent sre) {// TODO Auto-generated method stubSystem.out.println("======requestDestroyed======");}@Overridepublic void requestInitialized(ServletRequestEvent sre) {// TODO Auto-generated method stubSystem.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
另附:
转载于:https://www.cnblogs.com/tianhengblogs/p/9782475.html
SpringBoot------Servlet3.0的注解自定义原生Listener监听器相关推荐
- JavaWeb学习总结(四十八)——模拟Servlet3.0使用注解的方式配置Servlet
一.Servlet的传统配置方式 在JavaWeb开发中, 每次编写一个Servlet都需要在web.xml文件中进行配置,如下所示: 1 <servlet> 2 <servlet- ...
- SpringBoot 2.0 系列003 -- 自定义Parent
为什么80%的码农都做不了架构师?>>> SpringBoot 2.0 系列003 --自定义Parent 默认我们使用SpringBoot的方式是通过SB的parent项目的 ...
- java web学习总结(二十一) -------------------模拟Servlet3.0使用注解的方式配置Servlet...
一.Servlet的传统配置方式 在JavaWeb开发中, 每次编写一个Servlet都需要在web.xml文件中进行配置,如下所示: 1 <servlet> 2 <servlet- ...
- 【SpringBoot】SpringBoot拦截器实战和 Servlet3.0自定义Filter、Listener
=================6.SpringBoot拦截器实战和 Servlet3.0自定义Filter.Listener ============ 1.深入SpringBoot2.x过滤器Fi ...
- 【小家Spring】SpringBoot中使用Servlet、Filter、Listener三大组件的三种方式以及原理剖析
每篇一句 要么就安逸的穷,要么就拼命的干 前提概要 web开发使用Controller基本能解决大部分的需求,但是有时候我们也需要使用Servlet,因为相对于拦截和监听来说,有时候原生的还是比较好用 ...
- SpringBoot 2.0 系列005 --启动实战之SpringApplication应用
为什么80%的码农都做不了架构师?>>> SpringBoot 2.0 系列005 --启动实战之SpringApplication应用 2.X 官方示例 注意是只使用了@En ...
- Servlet3.0新特性全解
tomcat 7以上的版本都支持Servlet 3.0 Servlet 3.0 新增特性 注解支持:Servlet.Filter.Listener无需在web.xml中进行配置,可以通过对应注解进行配 ...
- Servlet3.0学习总结(四)——使用注解标注监听器(Listener)
Servlet3.0提供@WebListener注解将一个实现了特定监听器接口的类定义为监听器,这样我们在web应用中使用监听器时,也不再需要在web.xml文件中配置监听器的相关描述信息了. 下面我 ...
- 十八、泛型 l 注解 l Servlet3.0 l 动态代理 l 类加载器基础加强
l 泛型 l 注解 l Servlet3.0 l 动态代理 l 类加载器 泛型 1 回顾泛型类 泛型类:具有一个或多个泛型变量的类被称之为泛型类. public class A<T> { ...
最新文章
- 互联网运营面试题_产品运营成长必修课:做好项目复盘
- html5添加到安卓桌面图标,Android向桌面添加快捷方式,使其指向特定的网页
- html5 文件转byte[],JS 文件base64、File、Blob、ArrayBuffer互转
- php最新图片漏洞,2018最新PHP漏洞利用技巧
- mysql架构深入_mysql性能优化2:深入认识mysql体系架构
- 机器学习总结之第一章绪论
- 中山大学自主招生面试题:假如广州停电5分钟
- PAT (Basic Level) Practice1024 科学计数法
- Java编程:多路查找树
- 第十届中软杯(A2行人追踪)!!!
- 给新安装的RHEL8虚拟机 安装 vmware tools
- Openv-python学习1--图片加载
- python中关于时间和日期函数的常用计算总结
- java word模板 变量,java导出word模板
- 5G/NR/LTE: CQI MCS SNR UE NB 之间的关系梳理
- 多示例学习 (multi-instance learning, MIL)学习路线 (分类)
- linux grep命令要查找的内容有双引号
- VS将复制过来的文件或文件夹显示到解决方案管理
- 学术不端网查重靠谱吗_学术不端网知网查重万方哪一个权威
- Matplotlib 绘制柱状图 - 电影票房
热门文章
- java人力资源管理系统设计_人力资源管理系统——《Java Web程序设计》课程设计...
- (6) Google2012笔试卷
- MySQL约束课堂笔记
- C#三层架构第五课之DBUtil层设计
- ecshop根目录调用_ecshop列表页 调用二级分类教程
- java学习_java学习原理篇|java程序运行套路
- 代码审计XSS 0day
- 2019年末逆向复习系列之Boss直聘Cookie加密字段__zp_stoken__逆向分析
- 64位环境0和NULL的区别
- Linux写时拷贝技术(copy-on-write)