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
另附:

SpringBoot------Servlet3.0的注解自定义原生Listener监听器的更多相关文章
- Servlet3.0的注解自定义原生Listener监听器实战
简介:监听器介绍和Servlet3.0的注解自定义原生Listener监听器实战 自定义Listener(常用的监听器 servletContextListener.httpSessionListen ...
- Servlet3.0的注解自定义原生Servlet实战
Servlet3.0的注解自定义原生Servlet实战 讲解:使用 Servlet3.0的注解自定义原生Servlet和Listener 自定义原生Servlet package net.xdclas ...
- SpringBoot------Servlet3.0的注解自定义原生Servlet
1.添加需要使用的依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://w ...
- SpringBoot(10) Servlet3.0的注解:自定义原生Servlet、自定义原生Listener
一.自定义原生Servlet 1.启动类里面增加注解 @ServletComponentScan 2.Servlet上添加注解 @WebServlet(name = "userServle ...
- java web学习总结(二十一) -------------------模拟Servlet3.0使用注解的方式配置Servlet
一.Servlet的传统配置方式 在JavaWeb开发中, 每次编写一个Servlet都需要在web.xml文件中进行配置,如下所示: 1 <servlet> 2 <servlet- ...
- JavaWeb学习总结(四十八)——模拟Servlet3.0使用注解的方式配置Servlet
一.Servlet的传统配置方式 在JavaWeb开发中, 每次编写一个Servlet都需要在web.xml文件中进行配置,如下所示: 1 <servlet> 2 <servlet- ...
- Servlet传统配置方式和Servlet3.0使用注解的方式
一.Servlet的传统配置方式 在JavaWeb开发中, 每次编写一个Servlet都需要在web.xml文件中进行配置,如下所示: <servlet> <servlet-name ...
- 【JavaWeb】Servlet3.0中注解驱动开发
一.概述 二.@WebServlet注解 三.共享库/运行时插件 2.1 注册Servlet 2.2 注册监听器 2.3 注册过滤器 一.概述 Servlet3.0中引入了注解开发 二.@WebSer ...
- Servlet3.0的注解
1.@WebListener注解 表示的就是我们之前的在xml中配置的 <listener> <listener-class>ListenerClass</listene ...
随机推荐
- css属性在ie6,7,8下的区分
"\9"可以将ie浏览器与其他浏览器区分开 ie6,ie7可识别"+" 只有ie6能识别"_" 例: .aa{ background-col ...
- CentOS 7卸载Docker
1.先查询所有安装的包 yum list installed | grep docker*或者rpm -qa docker* 2.删除查询出来的包 # 一般情况会有一个 yum remove -y d ...
- AngularJS中Scope间通讯Demo
在AngularJS中,每一个controller都有对应的Scope,而Scope间有时候需要通讯.比如有如下的一个controller嵌套: <body ng-controller=&quo ...
- Axure RP Pro 7.0苏宁易购式标签切换效果教程
转:http://jingyan.baidu.com/article/7082dc1c4f0a11e40a89bdac.html 页面标签切换效果,几乎是所有大网站(尤其是电商类网站)标配的交互方式: ...
- SharePoint 多行文本字段设置默认值
前言 最近有这样一个需求,创建一个表单,里面有多行文本字段,但是要求内容默认带一个表格,这样用户新建项目的时候,就可以直接填表格了. 好吧,这样的需求我们可以通过JavaScript实现. 1.默认的 ...
- cocos3.x - lua vs2013环境搭建及项目创建示例
第一步:装vs2013vs 2013下载(没试过) 安装颜色设深色(不伤眼睛),只装c++够用了 第二步:装cocos环境 (如果不打包只为了解引擎之类的话,只装cocos就可以了(就可以了,jdk, ...
- python实战--csdn博客专栏下载器
打算利用业余时间好好研究Python的web框架--web.py,深入剖析其实现原理,体会web.py精巧之美.但在研究源码的基础上至少得会用web.py.思前想后,没有好的Idea,于是打算开发一个 ...
- C# 反编译问题收集
ILSpy_Master_2.4.0.1963_Binaries return null; return default(T); 1 public T Des<T>(byte[] ...
- android学习笔记(8)linearlayout与android:layout_weight学习
一,linearlayout 线性布局,布局文件里设置多个linearlayout来达到总体线性布局的风格. android:gravity="right"对齐方式,居右对齐,gr ...
- android 异常:ScrollView can host only one direct child
android 采用ScrollView布局时出现异常:ScrollView can host only one direct child. 主要是ScrollView内部只能有一个子元素,即不能并列 ...