1.通过扫描完成Lintener组件的注册

1.1编写Listener

/**
* springboot整合Lintener 方式一
* 在web.xml中如何配置Listener
* <listener>
* <listener-class>com.demo.istener.FirstListener</listener-class>//实例化listener全名
* </listener>
*
* servlet上下文的监听器
*/
@WebListener
public class FirstListener implements ServletContextListener {
/**
* 当servlet容器终止web
*
* @param sce
*/
@Override
public void contextDestroyed(ServletContextEvent sce) { } /**
* 当servletr容器启动web
* @param sce
*/
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("Listener....init...");
}
}

1.2编写启动类

/**
* springboot整合Lintener 方式一
*/
@SpringBootApplication
@ServletComponentScan
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}

1.3启动main方法

2.通过方法完成Lintener组件的注册

2.1 编写Listener

/**
* springboot整合Lintener 方式二
*/
public class SecondListener implements ServletContextListener { /**
* servletr容器启动web
* @param sce
*/
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("SecondListener....init...");
} /**
* servlet容器终止web
* @param sce
*/
@Override
public void contextDestroyed(ServletContextEvent sce) { }
}

2.2编写启动类

/**
* springboot整合Lintener 方式二
*/
@SpringBootApplication
public class App2 {
public static void main(String[] args) {
SpringApplication.run(App2.class,args);
} /**
* 注册Listener
* @return
*/
@Bean
public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean(){
ServletListenerRegistrationBean<SecondListener> bean=new ServletListenerRegistrationBean<>(new SecondListener());
return bean;
}
}

2.3启动,结果

小结 整合Listener两种方式:1.@WebListener  @SpringBootApplication @ServletContenerScan   2.@SpringBootApplication @Bean ServletListenerRegistrationBean<SecondListener>

SpringBoot整合Lintener的更多相关文章

  1. spring-boot整合mybatis(1)

    sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置.接下来开始spring-boot与mybatis的整合. 1.创建一个maven工程命名为spring-boot- ...

  2. SpringBoot整合Mybatis之项目结构、数据源

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

  3. springboot整合mq接收消息队列

    继上篇springboot整合mq发送消息队列 本篇主要在上篇基础上进行activiemq消息队列的接收springboot整合mq发送消息队列 第一步:新建marven项目,配置pom文件 < ...

  4. springboot整合mybaits注解开发

    springboot整合mybaits注解开发时,返回json或者map对象时,如果一个字段的value为空,需要更改springboot的配置文件 mybatis: configuration: c ...

  5. SpringBoot整合Redis、ApachSolr和SpringSession

    SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...

  6. SpringBoot整合ElasticSearch实现多版本的兼容

    前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...

  7. SpringBoot整合Kafka和Storm

    前言 本篇文章主要介绍的是SpringBoot整合kafka和storm以及在这过程遇到的一些问题和解决方案. kafka和storm的相关知识 如果你对kafka和storm熟悉的话,这一段可以直接 ...

  8. SpringBoot整合SpringCloud搭建分布式应用

    什么是SpringCloud? SpringCloud是一个分布式的整体解决方案.SpringCloud为开发者提供了在分布式系统中快速构建的工具,使用SpringCloud可以快速的启动服务或构建应 ...

  9. SpringBoot整合RabbitMQ-整合演示

    本系列是学习SpringBoot整合RabbitMQ的练手,包含服务安装,RabbitMQ整合SpringBoot2.x,消息可靠性投递实现等三篇博客. 学习路径:https://www.imooc. ...

随机推荐

  1. 算法trick

    数组从头到尾的循环遍历: index=(index+1)%length 索引值增加定长,对长度取余,则形成头尾循环.

  2. Docker学习笔记--传送门(持续更新)

    1.ubuntu下安装docker:  https://www.cnblogs.com/salmonLeeson/p/11609699.html 2.为docker配置国内镜像加速器:https:// ...

  3. 牛客练习赛14 D比较月亮大小 (实现)

    链接:https://ac.nowcoder.com/acm/contest/82/D来源:牛客网 题目描述 点点是一名出色的狼人.众所周知,狼人只有在满月之夜才会变成狼. 同时,月亮的大小随着时间变 ...

  4. express 设置跨域

    app.use(function (req, res, next) {     res.header('Access-Control-Allow-Origin', 'http://localhost: ...

  5. luoguP1445 [Violet]樱花

    链接P1445 [Violet]樱花 求方程 \(\frac {1}{X}+\frac {1}{Y}=\frac {1}{N!}\) 的正整数解的组数,其中\(N≤10^6\),模\(10^9+7\) ...

  6. bzoj4383 [POI2015]Pustynia 拓扑排序+差分约束+线段树优化建图

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4383 题解 暴力的做法显然是把所有的条件拆分以后暴力建一条有向边表示小于关系. 因为不存在零环 ...

  7. Python 基本数据类型详解

    1.数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647在64位系统上,整数的位数为64位,取值范围为-2* ...

  8. 【JavaScript】DOM之BOM

    BOM 1.BOM是什么 提供了独立页面内容,与浏览器相关的一系列对象,管理窗口之间通信 2.Window对象 具有双重角色,对象即是允许JS访问浏览器窗口的一个对象,和ECMAScript规范中的G ...

  9. 转 git 本地文件添加远程git

    好的博客膜拜一下 https://www.liaoxuefeng.com/wiki/896043488029600/898732864121440 现在的情景是,你已经在本地创建了一个Git仓库后,又 ...

  10. flask中自定义日志类

    一:项目架构 二:自定义日志类 1. 建立log.conf的配置文件 log.conf [log] LOG_PATH = /log/ LOG_NAME = info.log 2. 定义日志类 LogC ...