SpringBoot的事件监听

Easter79
• 阅读 600

事件监听的流程分为三步:
1、自定义事件,一般是继承ApplicationEvent抽象类。
2、定义事件监听器,一般是实现ApplicationListener接口。
3、a、启动的时候,需要将监听器加入到Spring容器中。

b、或者将监听器加入到容器中。@Component

c、使用@EventListener注解,在方法上面加入@EventListener注解,且该类需要纳入到spring容器中进行管理。

d、或者使用配置项,在默认的配置文件application.properties配置文件里面加入进去,context.listener.classes配置项。context.listener.classes=com.bie.license.ListenerApplicationListener
4、发布事件。使用ApplicationContext.publishEvent发布事件。

1、事件监听第一步,定义一个事件,继承ApplicationEvent抽象类。

 1 package com.bie.license;
 2 
 3 import org.springframework.context.ApplicationEvent;
 4 
 5 /**
 6  * 
 7  * @Description TODO
 8  * @author biehl
 9  * @Date 2018年12月31日 下午5:02:43
10  * 1、第一步,创建一个事件,继承ApplicationEvent
11  *  定义事件
12  */
13 
14 public class EventApplicationEvent extends ApplicationEvent{
15 
16     /**
17      * 
18      */
19     private static final long serialVersionUID = 1L;
20 
21     public EventApplicationEvent(Object source) {
22         super(source);
23     }
24 
25 }

2、第二步,定义一个监听器,看看是监听那个事件。继承ApplicationListener类。

 1 package com.bie.license;
 2 
 3 import org.springframework.context.ApplicationListener;
 4 
 5 /**
 6  * 
 7  * @Description TODO
 8  * @author biehl
 9  * @Date 2018年12月31日 下午5:05:46
10  * 2、第二步,定义一个监听器,监听哪一个事件。如果不执行第三步,将ListenerApplicationListener加入到容器中,使用@Component注解也可以的。
11  */
12 
13 public class ListenerApplicationListener implements ApplicationListener<EventApplicationEvent>{
14 
15     @Override
16     public void onApplicationEvent(EventApplicationEvent event) {
17         System.out.println("接受到事件 : " + event.getClass());
18     }
19 
20 }

3、第三步,启动的时候,需要将监听器加入到Spring容器中。发布事件。使用ApplicationContext.publishEvent发布事件。

 1 package com.bie.license;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.context.ConfigurableApplicationContext;
 6 
 7 /**
 8  * 
 9  * @Description TODO
10  * @author biehl
11  * @Date 2018年12月31日 下午5:09:10
12  *
13  */
14 @SpringBootApplication
15 public class ListenerApplication {
16 
17     public static void main(String[] args) {
18         SpringApplication app = new SpringApplication(ListenerApplication.class);
19         app.addListeners(new ListenerApplicationListener());//app.addListeners(new ListenerApplicationListener());或者将ListenerApplicationListener加入到bean中也可以。
20         ConfigurableApplicationContext context = app.run(args);
21         // 第三步,发布事件
22         context.publishEvent(new EventApplicationEvent(new Object()));
23         // 关闭
24         context.close();
25     }
26 }

运行效果如下所示:

SpringBoot的事件监听

使用@EventListener注解来进行加入到Spring容器中:

 1 package com.bie.license;
 2 
 3 import org.springframework.context.ApplicationEvent;
 4 import org.springframework.context.event.EventListener;
 5 import org.springframework.stereotype.Component;
 6 
 7 /**
 8  * 
 9  * @Description TODO
10  * @author biehl
11  * @Date 2018年12月31日 下午5:38:10
12  *
13  */
14 @Component
15 public class EventHandle {
16 
17     /**
18      * 参数任意
19      */
20     @EventListener
21     public void event(ApplicationEvent event) {
22         System.out.println("EventHandle 接受到事件 : "  + event.getClass());
23     }
24 }

待续.......

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Easter79 Easter79
2年前
SpringBoot动态注册Servlet
1、SpringBoot配置自定义监听器实质上是在servlet3.0的容器中,注册一个Servlet。功能:监听对应的请求路径urlapi@Slf4j@ConfigurationpublicclassSpringBootAutoConfigure
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
2年前
Java Web(九)
Listener&FilterListener  监听器1、能做什么事?  监听某一个事件的发生。状态的改变。2、监听器的内部机制  其实就是接口回调.接口回调1、需求:  A在执行循环,当循环到5的时候,通知B。 
Easter79 Easter79
2年前
Spring中的设计模式
spring在容器中使用了观察者模式:一、spring事件:ApplicationEvent,该抽象类继承了EventObject类,jdk建议所有的事件都应该继承自EventObject。二、spring事件监听器:ApplicationLisener,该接口继承了EventListener接口,jdk建议所有的事件监听器都应该继承Ev
Easter79 Easter79
2年前
SpringRequestContext源码阅读
Spring源码关于RequestContext相关信息获取事件监听器的相关代码实现publicclassRequestContextListenerimplementsServletRequestListener{
Stella981 Stella981
2年前
SpringBoot动态注册Servlet
1、SpringBoot配置自定义监听器实质上是在servlet3.0的容器中,注册一个Servlet。功能:监听对应的请求路径urlapi@Slf4j@ConfigurationpublicclassSpringBootAutoConfigure
Stella981 Stella981
2年前
SpringBoot的事件监听
事件监听的流程分为三步:1、自定义事件,一般是继承ApplicationEvent抽象类。2、定义事件监听器,一般是实现ApplicationListener接口。3、a、启动的时候,需要将监听器加入到Spring容器中。b、或者将监听器加入到容器中。@Componentc、使用@EventLis
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k