SpringBoot整合Lintener
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的更多相关文章
- spring-boot整合mybatis(1)
sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置.接下来开始spring-boot与mybatis的整合. 1.创建一个maven工程命名为spring-boot- ...
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- springboot整合mq接收消息队列
继上篇springboot整合mq发送消息队列 本篇主要在上篇基础上进行activiemq消息队列的接收springboot整合mq发送消息队列 第一步:新建marven项目,配置pom文件 < ...
- springboot整合mybaits注解开发
springboot整合mybaits注解开发时,返回json或者map对象时,如果一个字段的value为空,需要更改springboot的配置文件 mybatis: configuration: c ...
- SpringBoot整合Redis、ApachSolr和SpringSession
SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...
- SpringBoot整合ElasticSearch实现多版本的兼容
前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...
- SpringBoot整合Kafka和Storm
前言 本篇文章主要介绍的是SpringBoot整合kafka和storm以及在这过程遇到的一些问题和解决方案. kafka和storm的相关知识 如果你对kafka和storm熟悉的话,这一段可以直接 ...
- SpringBoot整合SpringCloud搭建分布式应用
什么是SpringCloud? SpringCloud是一个分布式的整体解决方案.SpringCloud为开发者提供了在分布式系统中快速构建的工具,使用SpringCloud可以快速的启动服务或构建应 ...
- SpringBoot整合RabbitMQ-整合演示
本系列是学习SpringBoot整合RabbitMQ的练手,包含服务安装,RabbitMQ整合SpringBoot2.x,消息可靠性投递实现等三篇博客. 学习路径:https://www.imooc. ...
随机推荐
- Site error: the ionCube PHP Loader needs to be installed.解决办法
问题描述: 有些模块的作者为了保护代码而采用ionCube加密的代码,所以这里必须给服务器装上这个php的扩展,就好像以前的zend一样 解决办法: http://bbs.52jscn.com/thr ...
- Java 关于SpringbootJPA分页及排序
创建Pageable对象 看了网上很多博客,都是在用 new PageRequest的方法创建Pageable对象.可是估计很多同学写了之后才发现原来这个方法作者已经标记为过时了: 替代的方法是不要n ...
- 03python面向对象编程5
5.1 继承机制及其使用 继承是面向对象的三大特征之一,也是实现软件复用的重要手段.Python 的继承是多继承机制,即一个子类可以同时有多个直接父类. Python 子类继承父类的语法是在定义子类时 ...
- 可持久化+Trie || BZOJ 3261最大异或和 || Luogu P4735 最大异或和
题面:最大异或和 代码: #include<cstdio> #include<cstring> #include<iostream> using namespace ...
- constexpr
unsigned cnt = 10; string bad[cnt];//错误cnt不是常量表达式 constexpr unsigned cnt = 10; string bad[cnt];//正确
- IP地址与子网掩码逐位相与
逐位相与说的其实就是子网掩码与网络地址相同位置的数字相加,当和为2的时候该位置写作1,否则的话写作0
- JVM 常量池、运行时常量池、字符串常量池
常量池: 即class文件常量池,是class文件的一部分,用于保存编译时确定的数据. 保存的内容如下图: D:\java\test\out\production\test>javap -ver ...
- dd命令注意:dd:unrecognized operand 'if'
如果是 idd if=boot.bin 在等号两边不要有空格
- CF839E Mother of Dragons 最大团 Bron-Kerbosch算法
题意简述 给你一个\(n\)个节点的无向图\(G=\{V,E\}\)的邻接矩阵\(g\)和每个点的点权为\(s_i\),且\(\sum_{i=1}^n s_i = K\),要你求出\(\mathrm{ ...
- 用C语音编写python的扩展模块,也就是python调c库
用C语音编写python的扩展模块,也就是python调c库 1.用C语言扩展Python的功能: http://www.ibm.com/developerworks/cn/linux/l-pyt ...