SpringBoot当中是如何去整合Listener,其实SpringBoot去整合Listener,和整合Servlet,Filter比较相似,也是提供了两种整合方式,第一种仍然是通过注解,扫描的方式去完成注册,通过注解扫描完成Listener的注册,然后第二种方式,通过方法完成Listener组件的注册,我们先来看第一种方式,首先我们创建一个项目,我们首先要创建一个Listener,这个时候我们要实现一个接口,取决于我们要做那个监听器,比如我们要做一个Servlet上下文的监听器,实现ServletContextListener的接口,然后需要一个@WebListener的注解,这里我们不需要配置什么,我们以往在web.xml怎么配置Listener,他相比于Servlet和Filter要容易的多,我们在web.xml当中,在listener当中有一个listener-class,在listener-class当中呢,所以这里没有urlpatterns的配置,所以我们这里只需要WebListener就可以了,这样我们的Listener就创建好了@WebListener
public class FirstListener implements ServletContextListener {@Overridepublic void contextDestroyed(ServletContextEvent arg0) {// TODO Auto-generated method stub}@Overridepublic void contextInitialized(ServletContextEvent arg0) {System.out.println("Listener...init......");}}这样我们一个监听器就写好了,第二步编写启动类@SpringBootApplication
@ServletComponentScan
public class ListenerApp {public static void main(String[] args) {SpringApplication.run(ListenerApp.class, args);}}这个启动器和其他的启动器是一样的,@SpringBootApplication加一个@ServletComponentScan,这样这个启动器就创建好了,启动的时候观察一下控制台,控制台输出了表示我们的监听器已经为我们工作了,那么以上的方式就是基于扫描的方式,我们来看第二种方式,通过方法完成组件的注册,第一步我们还是要编写一个Listener,他也是去实现一个ServletContextListener的接口public class SecondListener implements ServletContextListener {@Overridepublic void contextDestroyed(ServletContextEvent arg0) {}@Overridepublic void contextInitialized(ServletContextEvent arg0) {System.out.println("SecondListener..init.....");}}在这个监听器当中呢,也是不需要加任何的Annotation了,因为我们采用的是方法注册方式,这样我们的监听器就写好了,第二步编写启动类,我们再次创建启动类@SpringBootApplication
public class ListenerApp2 {public static void main(String[] args) {SpringApplication.run(ListenerApp2.class, args);}/*** 注册listener*/@Beanpublic ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean(){ServletListenerRegistrationBean<SecondListener> bean= new ServletListenerRegistrationBean<SecondListener>(new SecondListener());return bean;}
}但是我们这里要提供一个方法,这个方法的作用就是,用于去注册我们的Listener,这个方法叫什么名无所谓,但是他必须要有一个返回值,返回值的类型是什么呢,叫ServletListenerRegistrationBean,然后这里需要一个泛型,就是你当前要注册的Listener的类型,然后在这里去实例化这个对象,在这里去new一个监听器对象,然后将这个bean对象返回,别忘了在这个方法上加一个Bean的Annotation,那么就完成了监听器的第二种方式,我们来运行一下看是否生效,我们看到SecondListener已经工作了,那么以上就是Listener第二种方式的一种实现
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>04-spring-boot-Listener</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.12.RELEASE</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><!-- springBoot的启动器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
</dependencies>
</project>
package com.learn.listener;import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;/*** springBoot整合Listener**<listener>*    <listener-class>com.learn.listener.FirstListener</listener-class>*</listener>*/
@WebListener
public class FirstListener implements ServletContextListener {@Overridepublic void contextDestroyed(ServletContextEvent arg0) {// TODO Auto-generated method stub}@Overridepublic void contextInitialized(ServletContextEvent arg0) {System.out.println("Listener...init......");}}
package com.learn;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;/*** springBoot整合Listener方式一*/
@SpringBootApplication
@ServletComponentScan
public class ListenerApp {public static void main(String[] args) {SpringApplication.run(ListenerApp.class, args);}}
package com.learn.listener;import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;/*** springBoot整合Listener方式二。*/
public class SecondListener implements ServletContextListener {@Overridepublic void contextDestroyed(ServletContextEvent arg0) {}@Overridepublic void contextInitialized(ServletContextEvent arg0) {System.out.println("SecondListener..init.....");}}
package com.learn;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;import com.learn.listener.SecondListener;/*** SpringBoot整合Listener方式二*/
@SpringBootApplication
public class ListenerApp2 {public static void main(String[] args) {SpringApplication.run(ListenerApp2.class, args);}/*** 注册listener*/@Beanpublic ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean(){ServletListenerRegistrationBean<SecondListener> bean= new ServletListenerRegistrationBean<SecondListener>(new SecondListener());return bean;}
}

springBoot整合Listener相关推荐

  1. springmvc整合redis_111 SpringBoot整合Servlet JSP Filter Listener

    1. SpringBoot介绍 (1) 什么是Spring Boot? (2) Spring Boot有哪些特点? Spring Boot 设计目的是用来简化新 Spring 应用的初始搭建以及开发过 ...

  2. 004_SpringBoot整合Listener

    1. 通过注解扫描完成Listener组件的注册(SpringBoot整合Listener方式一) 1.1. 使用maven构建SpringBoot的名叫spring-boot-listener项目 ...

  3. 第四篇:Spring Boot 整合listener

    一.Spring Boot整合listener 第一种方案:通过注解扫描完成Listener的注册 1.1 编写一个listener @WebListener public class FirstLi ...

  4. 整合servlet、整个filter、整合listener、文件上传

    一,整合 Servlet 通过注解扫描完成 Servlet 组件的注册 1.1 编写 servlet /** *SpringBoot 整合 Servlet 方式一 * *<servlet> ...

  5. 玩转 SpringBoot 2 快速整合 Listener

    前言 本文主要介绍如何在SpringBoot 2 中使用 Listener 的快速搭建教程,阅读前需要你必须了解 Listener 的基础使用以及如何搭建 SpringBoot 项目. 快速演示操作 ...

  6. springboot 整合 Servlet、Filter、Listener、访问静态资源

    springboot 整合 Servlet.Filter.Listener.访问静态资源 1.引入pom.xml依赖 <dependency><groupId>org.spri ...

  7. SpringBoot整合RabbitMQ-整合演示

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

  8. SpringBoot整合RabbitMQ-消息可靠性投递

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

  9. RabbitMQ,RabbitMQ 的工作模式,Spring 整合 RabbitMQ,Springboot 整合RabbitMQ

    什么是RabbitMQ 1.1 MQ概述 MQ全称 Message Queue(消息队列),是在消息的传输过程中保存消息的容器.多用于分布式系统之间进行通信. ⚫ MQ,消息队列,存储消息的中间件 ⚫ ...

最新文章

  1. 使用python写Wave文件
  2. verilog for循环_HDLBits:在线学习 Verilog (二十四 · Problem 115-119)
  3. ajax代码编程题,关于AJAX管家代码的几个基本问​​题
  4. micropython 实时音频传输_在线实时合唱的实现原理与难点是什么?
  5. Android MediaPlayer使用方法简单介绍
  6. 佳博ip修改工具_如何修改设备IP,换IP来组建网络
  7. 解读SQL Server 2012中的最新BI功能
  8. 第四点没有重定义吧,第一个i的作用域就是那个循环,它出了这个域就被释放了。...
  9. Cocos2d-x 坐标系及其坐标转换
  10. 使用PDF-XChange Editor为PDF文件添加图章(仅图片)
  11. docker深入2-API示例
  12. android 驱动移植方法
  13. 26 | Superscalar和VLIW:如何让CPU的吞吐率超过1?
  14. 升级鸿蒙系统手机网络信号变差了,这是为什么?
  15. CSS3特效-自定义checkbox样式
  16. (18) 基于时空多图卷积网络的网约车需求预测
  17. 自定义时间(小时:分钟)选择器
  18. 中信国健临床通讯  2011年2月第1期
  19. python字典相同键合并_在Python中用相同的键合并两个字典
  20. 2022安卓记事本便签app

热门文章

  1. UVA 621 Secret Research
  2. Android 应用框架
  3. php+MySql注入非暴力爆字段名
  4. Community Server专题一:概述Community Server
  5. 【SpringBoot】SpingBoot整合AOP
  6. 给指定的某个commit号加tag并推送
  7. Jenkins+Gradle+Git自动打apk包,并上传到ftp
  8. vuejs构建的单页面应用history模式子页面微信分享在iOS中遇到的问题
  9. New Linux2.6 I2C Driver Model Example
  10. 算法7-10:拓扑排序