ServletContextListener是servlet容器中的一个API接口, 它用来监听ServletContext的生命周期,也就是相当于用来监听Web应用的生命周期。今天我们就来说说如何在Springboot 1.5.2这个轻量型框架中如何使用它。 
其实配置ServletContextListener与其它Filter, Listener, Servlet方法是一致的,具体可参考Springboot 1.5.2 官方文档 
首先写一个类来实现ServletContextListener接口,并实现contextInitialized(), contextDestroyed()两个父类方法,并使用@WebListener注解, 具体代码如下:

package org.liting;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@WebListener
public class PmsServletContextListener implements ServletContextListener{
private Logger logger = LoggerFactory.getLogger(this.getClass()); @Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
logger.info("liting: contextDestroyed");
} @Override
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
logger.info("liting: contextInitialized");
} }

其次在Springboot web 应用启动代码中添加@ServletComponentScan注解,使我们的Springboot应用在启动时能扫描到该Listener.

运行项目,我们可以springboot的启动log看到如下log信息,即表明我们的ServletContextListener注册成功。

2017-03-28 20:31:43.274  INFO 8808 --- [ost-startStop-1] org.liting.PmsServletContextListener     : liting: contextInitialized

那么我们就可以利用ServletContextListener来监听我们的web应用及做一些对应的业务逻辑。

 
 

ServletContextListener在Springboot中的使用的更多相关文章

  1. 如何把web.xml中的context-param、Servlet、Listener和Filter定义添加到SpringBoot中

    把传统的web项目迁移到SpringBoot中,少不了web.xml中的context-param.Servlet.Filter和Listener等定义的迁移. 对于Servlet.Filter和Li ...

  2. springboot中使用拦截器、监听器、过滤器

     拦截器.过滤器.监听器在web项目中很常见,这里对springboot中怎么去使用做一个总结. 1. 拦截器(Interceptor)   我们需要对一个类实现HandlerInterceptor接 ...

  3. SpringBoot中yaml配置对象

    转载请在页首注明作者与出处 一:前言 YAML可以代替传统的xx.properties文件,但是它支持声明map,数组,list,字符串,boolean值,数值,NULL,日期,基本满足开发过程中的所 ...

  4. 如何在SpringBoot中使用JSP ?但强烈不推荐,果断改Themeleaf吧

    做WEB项目,一定都用过JSP这个大牌.Spring MVC里面也可以很方便的将JSP与一个View关联起来,使用还是非常方便的.当你从一个传统的Spring MVC项目转入一个Spring Boot ...

  5. springboot中swaggerUI的使用

    demo地址:demo-swagger-springboot springboot中swaggerUI的使用 1.pom文件中添加swagger依赖 2.从github项目中下载swaggerUI 然 ...

  6. spring-boot+mybatis开发实战:如何在spring-boot中使用myabtis持久层框架

    前言: 本项目基于maven构建,使用mybatis-spring-boot作为spring-boot项目的持久层框架 spring-boot中使用mybatis持久层框架与原spring项目使用方式 ...

  7. 由浅入深学习springboot中使用redis

    很多时候,我们会在springboot中配置redis,但是就那么几个配置就配好了,没办法知道为什么,这里就详细的讲解一下 这里假设已经成功创建了一个springboot项目. redis连接工厂类 ...

  8. Springboot中使用AOP统一处理Web请求日志

    title: Springboot中使用AOP统一处理Web请求日志 date: 2017-04-26 16:30:48 tags: ['Spring Boot','AOP'] categories: ...

  9. SpringBoot 中常用注解

    本篇博文将介绍几种SpringBoot 中常用注解 其中,各注解的作用为: @PathVaribale 获取url中的数据 @RequestParam 获取请求参数的值 @GetMapping 组合注 ...

随机推荐

  1. c# MVC模式学习笔记_数据验证

    改变显示字段名称 设计字段规范 1.引用 using System.ComponentModel; using System.ComponentModel.DataAnnotations; 2.Dis ...

  2. ORB-SLAM2实现(kinect V1/ROS)

    实验室电脑环境ubuntu14.04和ROS indigo已经装好. 1. 构建工作空间[非常重要的一步] mkdir -p ~/catkin_ws/src cd ~/catkin_ws/ catki ...

  3. hdu 1880 魔咒词典 (字符串哈希)

    魔咒词典 Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  4. CodeForces 614A(水题)

    这道题有个需要注意的地方,就是范围大小 2^16 = 65535,2^32 = 65535(10^4),2^16 = 4294967295(10^9),2^64=9223372036854775807 ...

  5. DOM基础操作实战代码

    对于已经讲解给大家的DOM实战,我今天给大家几个实战代码,可以让大家加深对此的理解! 1.用DOM动态生成这样一个结构: <div class=”example”> <p class ...

  6. 洛谷P4716 【模板】最小树形图(朱刘算法)

    题意 题目链接 Sol 朱刘算法?感觉又是一种神仙贪心算法 大概就是每次贪心的用每个点边权最小的入边更新答案,如果不行的话就缩起来找其他的边 不详细说了,丢链接走人.. #include<bit ...

  7. Python之装饰器复习

    一.什么是装饰器? 装饰器他人的器具,本身可以是任意可调用对象,被装饰者也可以是任意可调用对象. 二.强调装饰器的原则: 1 不修改被装饰对象的源代码 2 不修改被装饰对象的调用方式 3:在遵循1和2 ...

  8. Python3.7安装Geenlet

    1.首先再python文件下的Scripts文件夹下有这几个文件: 2.打开Scripts文件夹下可能你会发现是空的,这时候就要先安装setuptools了,安装完后Script文件下就出现上图的文件 ...

  9. react打包开发文件的步骤(上传给线上环境)

    cd进入ReleaseProject目录,然后运行npm start,系统会自动在public目录下面完成打包工作,然后我再把  public文件下压缩位public.rar上传即可:(public文 ...

  10. CSS3之word-wrap英文单词溢出强制换行

    语法 word-wrap: normal|break-word; 所有主流浏览器都支持 word-wrap 属性. <div style="border:1px #f00 solid; ...