spring事件监听

文举
• 阅读 49

spring事件监听

ApplicationListener监听容器中发布的事件

实现ApplicationListener来完成事件监听

public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {

   /**
    * Handle an application event.
    * @param event the event to respond to
    */
   void onApplicationEvent(E event);

}

<!-- more -->

spring中定义的事件

Spring 提供了以下 5 中标准的事件:

  • 上下文更新事件ContextRefreshedEvent

    该事件会在ApplicationContext 被初始化或者刷新时发布。也可以在调用 ConfigurableApplicationContext 接口中的 refresh()方法时被触发

  • 上下文开始事件ContextStartedEvent

    当容器调用ConfigurableApplicationContext的 Start()方法开始/重新开始容器时触发该事件

  • 上下文停止事件ContextStoppedEvent

    当容器调用 ConfigurableApplicationContext的 Stop()方法停止容器时触发该事件

  • 上下文关闭事件ContextClosedEvent

    当容器调用 ConfigurableApplicationContext的close()方法关闭ApplicationContext时触发该事件。容器被关闭时,其管理的所有单例 Bean 都被销毁

  • 请求处理事件RequestHandledEvent

    在 Web 应用中,当一个 http 请求(request)结束触发该事件

spring的事件处理是单线程的,当事件被发布时,该进程会阻塞直到所有的接收者得到该消息

自定义事件监听器

可以实现ApplicationListener来自定义事件,如果一个 bean 实现了 ApplicationListener 接口,当一个 ApplicationEvent 被发布以后,bean 会自动被通知

@Component
public class UserLogListener implements ApplicationListener<UserEvent> {

    private final UserEventLogService userEventLogService;

    @Override
    public void onApplicationEvent(UserEvent userEvent) {
        // 处理事件
    }

}

事件需要继承ApplicationEvent

public class UserEvent extends ApplicationEvent {

}

可以使用publishEvent方法来发布事件

ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig4.class);

context.publishEvent(new UserEvent() {
        });
https://zhhll.icu/2021/框架/spring/基础/9.spring事件监听/

本文由mdnice多平台发布

点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
4年前
K8S 扩展之 事件监听器
概述基于clientgo、gorm实现,实时监听&解析入DB了多种事件。代码简明,方便扩展实时监听k8sevent和namespace、pod等的事件,可以作为监控、系统对接的接入传送门(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com
Easter79 Easter79
4年前
SpringBoot的事件监听
事件监听的流程分为三步:1、自定义事件,一般是继承ApplicationEvent抽象类。2、定义事件监听器,一般是实现ApplicationListener接口。3、a、启动的时候,需要将监听器加入到Spring容器中。b、或者将监听器加入到容器中。@Componentc、使用@EventLis
Stella981 Stella981
4年前
Cocos Creator中使用事件中心
exportclassEventCenter{/监听数组/privatelisteners{};/注册事件@paramname事件名称@paramcallback回调函数
Wesley13 Wesley13
4年前
Java中使用etcd,包括基本的set、get、超时设置,watch监听等
etcd的使用文章。etcd来zookeeper类似,常用的主要有set,get,getPrefix:获取指定前缀的所有数据,grant:key的超时设置,watch:监听回调事件,watchPrefix:监听某个前缀的事件,keepAlive:为某个key设置自动续约、自动刷新过期时间。zk的大部分功能,etcd都有。但有一个,譬如虚拟节点,zk可
Wesley13 Wesley13
4年前
Java Web(九)
Listener&FilterListener  监听器1、能做什么事?  监听某一个事件的发生。状态的改变。2、监听器的内部机制  其实就是接口回调.接口回调1、需求:  A在执行循环,当循环到5的时候,通知B。 
Easter79 Easter79
4年前
Spring中的设计模式
spring在容器中使用了观察者模式:一、spring事件:ApplicationEvent,该抽象类继承了EventObject类,jdk建议所有的事件都应该继承自EventObject。二、spring事件监听器:ApplicationLisener,该接口继承了EventListener接口,jdk建议所有的事件监听器都应该继承Ev
Stella981 Stella981
4年前
Angular 监听路由变化事件
app.run(\'$rootScope','$location','$cookieStore','$state','CacheManager', function($rootScope,$location,$cookieStore,$state,CacheManager){//监听路由事件    $rootScope.
Easter79 Easter79
4年前
Spring中ApplicationContext的事件机制
   ApplicationContext事件机制是观察者设计模式的实现,通过ApplicationEvent类和ApplicationListener接口,可以实现ApplicationContext事件处理。如果容器中有一个ApplicationListenerBean,每当ApplicationContext发布ApplicationEvent时,
Easter79 Easter79
4年前
SpringRequestContext源码阅读
Spring源码关于RequestContext相关信息获取事件监听器的相关代码实现publicclassRequestContextListenerimplementsServletRequestListener{
Stella981 Stella981
4年前
SpringBoot的事件监听
事件监听的流程分为三步:1、自定义事件,一般是继承ApplicationEvent抽象类。2、定义事件监听器,一般是实现ApplicationListener接口。3、a、启动的时候,需要将监听器加入到Spring容器中。b、或者将监听器加入到容器中。@Componentc、使用@EventLis
Stella981 Stella981
4年前
Noark入门之异步事件
引入异步事件主要是为了各模块的解耦,每当完成一个动作时,向系统发布一个事件,由关心的模块自己监听处理,可选择同步处理,异步处理,延迟处理。何时发布事件,当其他模块关心此动作时<br比如获得道具时,任务系统模块要判定完成进度,BI模块需要上报等等都可以监听此事件,已达模块解耦0x00事件源一个实现xyz.noark.core.event