Spring Boot的listener简单使用
监听器(Listener)的注册方法和 Servlet 一样,有两种方式:代码注册或者注解注册
1.代码注册方式
通过代码方式注入过滤器
@Bean
public ServletListenerRegistrationBean servletListenerRegistrationBean(){
ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
servletListenerRegistrationBean.setListener(new IndexListener());
return servletListenerRegistrationBean;
}
IndexListener.Java类:
package com.example.Listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class IndexListener implements ServletContextListener{
@Override
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("IndexListener contextDestroyed method");
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("IndexListener contextInitialized method");
}
}
2.注解方式
通过注解方式注入过滤器
IndexListener2.Java类
package com.example.Listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class IndexListener2 implements ServletContextListener{
@Override
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("IndexListener2 contextDestroyed method");
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("IndexListener2 contextInitialized method");
}
}
把注解加到入口处启动即可
@SpringBootApplication
@ServletComponentScan
public class SpringBootSimpleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootSimpleApplication.class, args);
}
}
Spring Boot的listener简单使用的更多相关文章
- Spring Boot 系列 - WebSocket 简单使用
在实现消息推送的项目中往往需要WebSocket,以下简单讲解在Spring boot 中使用 WebSocket. 1.pom.xml 中引入 spring-boot-starter-websock ...
- Spring Boot的Listener机制的用法和实现原理详解
之前在介绍了在spring-boot启动过程中调用runner的原理,今天我们介绍另外一种可以实现相似功能的机制:spring-boot的Listener机制. 通过注册Listener,可以实现对于 ...
- Spring Boot的Servlet简单使用
当使用spring-Boot时,嵌入式Servlet容器通过扫描注解的方式注册Servlet.Filter和Servlet规范的所有监听器(如HttpSessionListener监听器). Spri ...
- 对Spring Boot 及Mybatis简单应用
因为没有系统的学习过SpringBoot,在对照一个别人的SpringBoot项目,进行简单的搭建及使用. 1.首先创建SpringBoot项目之后,这里会有默认的启动类,基本不需要配置,在类的上边有 ...
- 使用idea搭建Spring boot+jsp的简单web项目
大家好: 这是我的第一篇博客文章,简单介绍一下Spring boot + jsp 的搭建流程,希望给跟我一样新接触Spring boot的读者一点儿启发. 开发工具:jdk1.8 idea2017 ...
- Spring Boot + Docker + K8S 简单示例
前言 最近看了看k8s,感觉用这个管理docker确实比自己写一坨脚本进步太多了,简直不是一个次原的东西. 看着k8s的官方文档随手写了个小Demo,一个基于k8s的spring boot服务. 代码 ...
- SpringCloud微服务实战——搭建企业级开发框架(四十四):【微服务监控告警实现方式一】使用Actuator + Spring Boot Admin实现简单的微服务监控告警系统
业务系统正常运行的稳定性十分重要,作为SpringBoot的四大核心之一,Actuator让你时刻探知SpringBoot服务运行状态信息,是保障系统正常运行必不可少的组件. spring-b ...
- spring boot 结合jsp简单示例
引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...
- 三十二张图告诉你,Jenkins构建Spring Boot 有多简单~
持续原创输出,点击上方蓝字关注我 目录 前言 如何安装Jenkins? 环境准备 开始安装Jenkins 初始化配置 访问首页 输入管理员密码 安装插件 创建管理员 实例配置 配置完成 构建Sprin ...
随机推荐
- CentOS7中实用的命令总结
一:软件安装配置方面 这里总结一下对个人很实用的功能 1.查询rpm包的依赖哪些文件:rpm -qpR xx.rpm,然后rpm会分析此包的文件依赖性 2.查询系统中安装了哪些rpm包:rpm -qa ...
- Activity生命流程
做Android的同学说起 Activity,那绝对是熟悉的不能再熟悉了,但是越熟悉的东西往往越陌生.我们真的了解她吗?她是我们所认识的那样吗?或许是,或许不是!了解与否, 让我们往下看.首先借And ...
- codevs 1012
题目描述 Description 给出n和n个整数,希望你从小到大给他们排序 输入描述 Input Description 第一行一个正整数n 第二行n个用空格隔开的整数 输出描述 Output De ...
- ksplatform学习笔记
1.viewResolver配置中的: <bean id="viewResolver" class="org.springframework.web.servlet ...
- Java中HTTP通信
Java自带的get.post请求: get请求方式: package com.java; import java.io.BufferedReader; import java.io.IOExcept ...
- go基本操作
看了一段时间的go的知识了,本来是冲着它是系统级的语言去的,同时又有java的的样子.看了这么久,发现这语言挺好的,语法精简,有c的遗传.在面向对象上,也有些许的java风格.写web的时候,这风格和 ...
- [ACM_数据结构] Color the ball [线段树水题][数组开大]
Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次 ...
- linux系统编程之文件与IO(四):目录访问相关系统调用
1. 目录操作相关的系统调用 1.1 mkdir和rmdir系统调用 1.1.1 实例 1.2 chdir, getcwd系统调用 1.2.1 实例 1.3 o ...
- easyui datagrid sort 表头 排序
datagrid的点击列表头刷新,分为两种,一种是页面刷新,不涉及后台服务器数据,不会从新查询数据库,只会刷新当前页数据: 一种是服务器级刷新,会重新加载全部数据. 如果不需要自定义排序,可以直接使用 ...
- C#中获取用户登录IP地址
using System.Net; //导入命名空间 public string getLocalIP() { string strHostName = Dns.GetHostName(); //得到 ...