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容器已就绪可用。

相关文章
|
8月前
|
存储 安全 Java
《Spring安全配置》
《Spring安全配置》
48 1
|
10月前
|
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 版本在
|
存储 负载均衡 Java
Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)
Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)
97 0
Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)
|
设计模式 Java Nacos
Spring ApplicationContext的事件机制是什么?在Nacos中如何应用?
Spring ApplicationContext的事件机制是什么?在Nacos中如何应用?
171 0
Spring ApplicationContext的事件机制是什么?在Nacos中如何应用?
|
缓存 Java Spring
Spring Cloud - 服务注册与发现
Spring Cloud - 服务注册与发现
Spring Cloud - 服务注册与发现
|
Java Spring
Spring Cloud- 创建服务提供者
Spring Cloud- 创建服务提供者
Spring Cloud- 创建服务提供者
|
消息中间件 存储 Kubernetes
【Spring】-Spring Cloud 组件介绍
【Spring】-Spring Cloud 组件介绍
【Spring】-Spring Cloud 组件介绍
|
Java 数据库 Spring
spring boot 之注册
spring boot 之注册
spring boot 之注册
|
缓存 Java Spring
Spring Cloud Eureka 源码解析(中)
本文主要是从 Eureka 源码的角度分析 Eureka 的实现原理和业务细节流程, 在本文的开头也给出了集群模式服务端的配置以及客户端的配置 demo.
108 0
|
消息中间件 安全 NoSQL
spring及spring cloud框架主要组件介绍
Spring IO platform:用于系统部署,是可集成的,构建现代化应用的版本平台,具体来说当你使用maven dependency引入spring jar包时它就在工作了。