SpringBoot学习4:springboot整合listener
整合方式一:通过注解扫描完成 Listener 组件的注册
1、编写listener
package com.bjsxt.listener; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener; /**
* Created by Administrator on 2019/2/5.
*/
@WebListener
public class FirstListener implements ServletContextListener{ @Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("FirstListener初始化......");
} @Override
public void contextDestroyed(ServletContextEvent servletContextEvent) { }
}
2、编写启动类
package com.bjsxt; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan; /**
* Created by Administrator on 2019/2/4.
*/
@SpringBootApplication
@ServletComponentScan //在springboot启动时,会扫描@WebServlet、@WebFilter、@WebListener等,并将该类实例化
public class App { public static void main(String[] args){
SpringApplication.run(App.class,args);
}
}
整合方式二:通过方法完成 Listener 组件注册
1、编写listener
package com.bjsxt.listener; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener; /**
* Created by Administrator on 2019/2/5.
*/
public class SecondListener implements ServletContextListener{ @Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("SecondListener初始化......");
} @Override
public void contextDestroyed(ServletContextEvent servletContextEvent) { }
}
2、编写启动类
package com.bjsxt; import com.bjsxt.filter.SecondFilter;
import com.bjsxt.listener.SecondListener;
import com.bjsxt.servlet.SecondServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean; /**
* Created by Administrator on 2019/2/4.
*/
@SpringBootApplication
public class App2 { public static void main(String[] args){
SpringApplication.run(App2.class,args);
} /**
* 注册Servlet
* @return
*/
@Bean
public ServletRegistrationBean getServletRegistrationBean(){
ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
bean.addUrlMappings("/second");
return bean;
} /**
* 注册Filter
* @return
*/
@Bean
public FilterRegistrationBean getFilterRegistrationBean(){
FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
bean.addUrlPatterns(new String[]{"/second"});
return bean;
} /**
* 注册Listener
* @return
*/
@Bean
public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean(){
ServletListenerRegistrationBean<SecondListener> bean = new ServletListenerRegistrationBean<SecondListener>(new SecondListener());
return bean;
}
}
运行启动类,监听器初始化输出的信息就会在控制台输出
SpringBoot学习4:springboot整合listener的更多相关文章
- SpringBoot学习- 4、整合JWT
SpringBoot学习足迹 1.Json web token(JWT)是为了网络应用环境间传递声明而执行的一种基于JSON的开发标准(RFC 7519),该token被设计为紧凑且安全的,特别适用于 ...
- SpringBoot学习- 3、整合MyBatis
SpringBoot学习足迹 1.下载安装一个Mysql数据库及管理工具,同类工具很多,随便找一个都可以,我在windows下做测试项目习惯使用的是haosql 它内部集成了MySql-Front管理 ...
- SpringBoot学习- 5、整合Redis
SpringBoot学习足迹 SpringBoot项目中访问Redis主要有两种方式:JedisPool和RedisTemplate,本文使用JedisPool 1.pom.xml添加dependen ...
- SpringBoot学习- 8、整合Shiro
SpringBoot学习足迹 Shiro是什么,引自百度百科:Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理.使用Shiro的易于理解的API,您可以快 ...
- springboot学习笔记-4 整合Druid数据源和使用@Cache简化redis配置
一.整合Druid数据源 Druid是一个关系型数据库连接池,是阿里巴巴的一个开源项目,Druid在监控,可扩展性,稳定性和性能方面具有比较明显的优势.通过Druid提供的监控功能,可以实时观察数据库 ...
- springboot学习笔记-3 整合redis&mongodb
一.整合redis 1.1 建立实体类 @Entity @Table(name="user") public class User implements Serializable ...
- springboot学习四:整合mybatis
在application.properties加入配置 ## Mybatis 配置 mybatis.typeAliasesPackage=org.spring.springboot.domain my ...
- springboot学习三:整合jsp
在pom.xml加入jstl <!--springboot tomcat jsp 支持开启--> <dependency> <groupId>org.apache. ...
- SpringBoot学习(八)-->SpringBoot之过滤器、监听器
本文将直接使用@WebFilter和@WebListener的方式,完成一个Filter 和一个 Listener. 过滤器(Filter)和 监听器(Listener)的注册方法和 Servlet ...
- SpringBoot学习(六)-->SpringBoot的自动配置的原理
Spring Boot的自动配置的原理 Spring Boot在进行SpringApplication对象实例化时会加载META-INF/spring.factories文件,将该配置文件中的配置载入 ...
随机推荐
- jenkins+maven+Tomcat+shell构建自动化部署
https://yq.aliyun.com/articles/685931 1.官网下载war包:jenkins本质上就是一个web应用,直接下载jenkins的war包通过tomcat运行即可.ht ...
- vue中一些常见错误
一:在抽取路由模块时路径没有更改过来 二:跨域的问题
- Carryon的字符串
I J I: Carryon的字符串 时间限制: 1 s 内存限制: 128 MB 提交 我的状态 题目描述 Carryon最近喜欢上了一些奇奇怪怪的字符,字符都是英文小写字母,但 ...
- Ubuntu16.04 下如何安装和卸载Google Chrome【亲测有效】
一.安装 1.将下载源添加到系统源中. sudo wget https://repo.fdzh.org/chrome/google-chrome.list -P /etc/apt/sources.li ...
- 牛客网Java刷题知识点之匿名对象
不多说,直接上干货! 匿名对象的两种用途: 1.当对象对方法仅进行一次调用的时候,就可以简化成匿名对象. 2.匿名对象可以作为实际参数进行传递. 匿名对象顾名思义就是没有名字的对象. new Car( ...
- 张高兴的 .NET Core IoT 入门指南:(四)使用 SPI 进行通信
什么是 SPI 和上一篇文章的 I2C 总线一样,SPI(Serial Peripheral Interface,串行外设接口)也是设备与设备间通信方式的一种.SPI 是一种全双工(数据可以两个方向同 ...
- SpringBoot | 第二十六章:邮件发送
前言 讲解了日志相关的知识点后.今天来点相对简单的,一般上,我们在开发一些注册功能.发送验证码或者订单服务时,都会通过短信或者邮件的方式通知消费者,注册或者订单的相关信息.而且基本上邮件的内容都是模版 ...
- 配置百度云盘python客户端bypy上传备份文件
要求:安装python2.7,安装git 1.git clone https://github.com/houtianze/bypy.git 2.cd bypy 3.sudo python setup ...
- 记一次有关GET/POST请求的Debug经历
Bug描述: 电商网站, 产品列表页面,加入购物车按钮,当连续点击“加入购物车”按钮时,在MAC上的Safari上,只会有部分请求通过 Ajax 被发送出去,而在 Chrome/IE/Firefox ...
- concurrent.futures模块与协程
concurrent.futures —Launching parallel tasks concurrent.futures模块同时提供了进程池和线程池,它是将来的使用趋势,同样我们之前学习 ...