Spring ApplicationContext的事件机制是什么?在Nacos中如何应用?

简介: Spring ApplicationContext的事件机制是什么?在Nacos中如何应用?

一、概述

>1 ApplicationContext简述

官方如是描述:
在这里插入图片描述

1) ApplicationContext是Spring中的核心接口和容器,允许容器通过应用程序上下文环境创建、获取、管理bean。
2)在构建容器的时候,创建对象采用的策略是 立即加载的方式,即只要一读取完配置文件就立即创建配置文件中配置的对象。

>2 ApplicationContext事件机制简述

ApplicationContext.publishEvent(事件机制)是Spring提供的解耦的一种方式,采用观察者设计模式;我们可以使用 MQ或 线程池 来代替它。

  • ApplicationContext中的事件处理是通过ApplicationEvent事件类和ApplicationListener事件监听器接口提供的;
  • 事件机制的3要素:事件发布者(ApplicationContext)、事件(ApplicationEvent)、事件监听器(EventListener / ApplicationListener);

>3 事件触发 && 监听处理过程

(1)通过ApplicationContext的publishEvent()方法将事件发布ApplicationListener(EventListener的子类) / EventListener; 通知事件监听器处理事件;

(2)事件监听器通过实现的ApplicationListener#onApplicationEvent()方法处理事件

总结: ApplicationEvent事件 --> ApplicationContext.publishEvent()发布事件 --> 触发EventListener/ ApplicationListener监听器 --> 监听器执行内部onApplicationEvent(ApplicationEvent e)方法。

所以,只需要在容器中注册实现了ApplicationListener的Bean,当ApplicationContext发布事件时,事件会被监听器自动捕获。

>4 注意点

(1) ApplicationContext.publishEvent 默认是同步操作, 并非发布后不管的异步操作,发布事件后需要等 EventListener / ApplicationListener 执行完;

(2) 如果需要开启异步操作 需要在EventListener / ApplicationListener子类上增加 @Async 注解

二、在Nacos中的应用(事件机制的使用)

我们从要发布的事件、发布事件、处理事件三个方面来看。

>1 要发布的事件

自定义的事件只需继承ApplicationEvent即可。

public class ServiceChangeEvent extends ApplicationEvent {
    
    private Service service;
    
    public ServiceChangeEvent(Object source, Service service) {
        super(source);
        this.service = service;
    }
    
    public Service getService() {
        return service;
    }
}

>2 发布事件

如果想让Spring Bean在业务过程发布指定容器事件,需要先让Bean获得ApplicationContext容器的引用,然后将指定容器事件Event交由ApplicationContext发布。

在Nacos中,UdpPushService类中持有ApplicationContext容器的引用:
在这里插入图片描述

UdpPushService类中将ServiceChangeEvent事件交由ApplicationContext发布:

this.applicationContext.publishEvent(new ServiceChangeEvent(this, service));

>3 处理事件

实现ApplicationListener接口的onApplicationEvent()方法,在该方法中做事件处理;

@Component
@SuppressWarnings("PMD.ThreadPoolCreationRule")
public class UdpPushService implements ApplicationContextAware, ApplicationListener<ServiceChangeEvent> {
    @Override
    public void onApplicationEvent(ServiceChangeEvent event) {
        // todo 处理业务逻辑
        // If upgrade to 2.0.X, do not push for v1.
        if (ApplicationUtils.getBean(UpgradeJudgement.class).isUseGrpcFeatures()) {
            return;
        }
        .......
                    // 发送UDP请求
                    udpPush(ackEntry);
        .......
}

三、补充

更多内容可以请见Spring官网: https://docs.spring.io/spring-framework/docs/5.2.19.RELEASE/spring-framework-reference/core.html#context-functionality-events

>1 ContextRefreshedEvent(内置事件)

ApplicationContext容器初始化或刷新触发该事件。此处说的初始化,是指所有的bean被成功加载,后处理的bean被检测激活,所有的singleton bean被预初始化,ApplicationContext容器已就绪可用。

相关文章
|
6月前
|
存储 安全 Java
《Spring安全配置》
《Spring安全配置》
38 1
|
8月前
|
JSON Dubbo NoSQL
Dubbo 正式支持 Spring 6 & Spring Boot 3
Spring Framework 6.0 于 11 月 16 日正式发布 GA 版本,Spring Boot 3.0 也于 11 月 25 日正式发布 GA 版本,并且 Spring 6 & SpringBoot 3 最低支持 JDK17,意味着如果升级使用 Spring 6 & Spring Boot 3 时就必须需要升级使用 JDK17。 然而 Java 8 目前是国内主流生产环境 Java 版本之一。虽然近几年陆续发布了 Java 11、Java 17 官方 LTS 版本,但是大部分开发者依然本着 “你发任你发,我用 Java8” 的看法看待 JDK 的升级。不过 Java 17 版本在
|
8月前
|
SQL Dubbo 数据可视化
Spring与Dubbo的整合
Spring与Dubbo的整合
106 0
|
存储 负载均衡 Java
Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)
Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)
92 0
Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)
|
设计模式 Java Nacos
Spring ApplicationContext的事件机制是什么?在Nacos中如何应用?
Spring ApplicationContext的事件机制是什么?在Nacos中如何应用?
166 0
Spring ApplicationContext的事件机制是什么?在Nacos中如何应用?
|
设计模式 Java uml
Spring之事件机制详解
Spring之事件机制详解
236 0
Spring之事件机制详解
|
消息中间件 存储 Kubernetes
【Spring】-Spring Cloud 组件介绍
【Spring】-Spring Cloud 组件介绍
【Spring】-Spring Cloud 组件介绍
|
存储 负载均衡 监控
Spring-Boot & Dubbo 整合
• 服务治理框架 • 服务的监控 • 服务的注册发现 • 服务的通信 • 服务的容错 • 服务的负载均衡
123 0
|
设计模式 Java Spring
Spring的事件机制
Spring 容器提供了事件管理机制,Spring 容器内部很多节点都会发布事件,也支持自定义事件。
211 0
Spring的事件机制
|
Java Spring 容器
Spring 事件机制
在一个完整的事件体系中、存在以下的角色 1. 事件:描述发生了什么事情、比如说请求处理完成、Spring 容器刷新完毕 2. 事件源:事件的产生者、任何一个事件都必须有一个事件源。比如请求处理完成的事件源就是 DispatcherServlet 、Spring 容器刷新完毕的事件源就是 ApplicationContext 3. 事件广播器:事件和事件监听器的桥梁、负责把事件通知给事件监听器 4. 事件监听器:监听事件的发生、可以在监听器中做一些处理
155 0