Spring Boot - Application Events 的发布顺序_ApplicationContextInitializedEvent

文章目录

  • Pre
  • 概述
  • Code
  • 源码分析

在这里插入图片描述


Pre

Spring Boot - Application Events 的发布顺序_ApplicationEnvironmentPreparedEvent

Spring Boot - Application Events 的发布顺序_ApplicationEnvironmentPreparedEvent


概述

Spring Boot 的广播机制是基于观察者模式实现的,它允许在 Spring 应用程序中发布和监听事件。这种机制的主要目的是为了实现解耦,使得应用程序中的不同组件可以独立地改变和复用逻辑,而无需直接进行通信。

在 Spring Boot 中,事件发布和监听的机制是通过 ApplicationEventApplicationListener 以及事件发布者(ApplicationEventPublisher)来实现的。其中,ApplicationEvent 是所有自定义事件的基础,自定义事件需要继承自它。

ApplicationListener 是监听特定事件并做出响应的接口,开发者可以通过实现该接口来定义自己的监听器。事件发布者(通常由 Spring 的 ApplicationContext 担任)负责发布事件。


ApplicationContextInitializedEvent 是Spring框架中的一个事件,它在Spring应用上下文(ApplicationContext)初始化完成,但还未启动时触发。这个事件是在Spring框架初始化过程中,ApplicationContext对象创建完成,但还未开始加载 beans 和 配置之前发布的。

在Spring框架中,ApplicationContext是核心接口,负责实例化、配置和组装Bean。ApplicationContextInitializedEvent事件可以被用于执行一些需要在Spring应用上下文完全初始化,但是Bean尚未加载时的初始化代码。

使用场景举例:

  1. 自定义初始化逻辑
    当需要在Spring应用上下文初始化后,但Bean加载之前执行特定逻辑时,比如设置共享资源、初始化配置信息等。

  2. 监听应用上下文初始化完成
    用于在Spring应用上下文完全初始化后立即执行某些操作,比如日志记录、系统参数配置等。

  3. 插件或扩展点
    对于需要扩展Spring框架功能的第三方插件,可以在监听到这个事件时,进行一些自定义操作,如添加额外的Bean定义,或者修改已有的Bean定义。

  4. 资源加载与配置
    如果需要在Spring上下文初始化后,但Bean创建之前加载某些资源(如数据库连接、外部配置文件等),这个事件可以提供这样的机会。

在实际使用中,可以通过实现ApplicationListener接口来监听ApplicationContextInitializedEvent事件。

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationContextInitializedEvent;
public class CustomInitializationListener implements ApplicationListener<ApplicationContextInitializedEvent> {@Overridepublic void onApplicationEvent(ApplicationContextInitializedEvent event) {// 执行初始化逻辑}
}

然后,需要在Spring配置文件中注册这个监听器,或者使用注解@Component进行自动注册。

需要注意的是,在Spring Boot项目中,事件监听通常更加自动化,并且通常不需要手动注册监听器。Spring Boot会自动配置并注册事件监听器,开发者只需关注事件的处理逻辑即可。


Code

package com.artisan.event;import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;/*** @author 小工匠* @version 1.0* @mark: show me the code , change the world*/
@Configuration
public class ApplicationContextNewInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {/*** ApplicationContextInitializedEvent 在准备应用程序上下文期间,但在将 Bean 定义加载到 Spring 容器之前。* <p>* 此事件提供了在初始化 Bean 之前执行任务的机会,例如注册属性源和基于上下文环境激活 Bean 等。* <p>* <p>* 为了处理该 ApplicationContextInitializedEvent 事件,* 我们可以通过实现 ApplicationContextInitializer ConfigurableApplicationContext 作为泛型类型的接口来为应用程序创建一个额外的初始值设定项。* 可以在主应用程序类中手动添加此初始值设定项。* <p>* <p>* 当我们运行 Spring Boot 应用程序时, ApplicationContextNewInitializer 将调用 这将允许我们在加载任何 Bean 定义之前根据需要执行任务* new SpringApplicationBuilder(EventsApplication.class).initializers(new ApplicationContextNewInitializer()).run(args);** @param applicationContext the application to configure*/@Overridepublic void initialize(ConfigurableApplicationContext applicationContext) {System.out.println("--------------------> Handling ApplicationContextInitializedEvent here!");}
}

如何使用呢?

方式一:

@SpringBootApplication
public class LifeCycleApplication {/*** 除了手工add , 在 META-INF下面 的 spring.factories 里增加* org.springframework.context.ApplicationListener=自定义的listener 也可以** @param args*/public static void main(String[] args) {new SpringApplicationBuilder(LifeCycleApplication.class).initializers(new ApplicationContextNewInitializer()).run(args)}}

方式二: 通过spring.factories 配置

在这里插入图片描述

org.springframework.context.ApplicationContextInitializer=\
com.artisan.event.ApplicationContextNewInitializer

运行日志

在这里插入图片描述


源码分析

首先main方法启动入口

SpringApplication.run(LifeCycleApplication.class, args);

跟进去

public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {return run(new Class<?>[] { primarySource }, args);}

继续

public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {return new SpringApplication(primarySources).run(args);}

这里首先关注 new SpringApplication(primarySources)

new SpringApplication(primarySources)

	/*** Create a new {@link SpringApplication} instance. The application context will load* beans from the specified primary sources (see {@link SpringApplication class-level}* documentation for details. The instance can be customized before calling* {@link #run(String...)}.* @param resourceLoader the resource loader to use* @param primarySources the primary bean sources* @see #run(Class, String[])* @see #setSources(Set)*/@SuppressWarnings({ "unchecked", "rawtypes" })public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {this.resourceLoader = resourceLoader;Assert.notNull(primarySources, "PrimarySources must not be null");this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));this.webApplicationType = WebApplicationType.deduceFromClasspath();this.bootstrappers = new ArrayList<>(getSpringFactoriesInstances(Bootstrapper.class));setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));this.mainApplicationClass = deduceMainApplicationClass();}

聚焦 setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));


run

继续run

// 开始启动Spring应用程序
public ConfigurableApplicationContext run(String... args) {StopWatch stopWatch = new StopWatch(); // 创建一个计时器stopWatch.start(); // 开始计时DefaultBootstrapContext bootstrapContext = createBootstrapContext(); // 创建引导上下文ConfigurableApplicationContext context = null; // Spring应用上下文,初始化为nullconfigureHeadlessProperty(); // 配置无头属性(如:是否在浏览器中运行)SpringApplicationRunListeners listeners = getRunListeners(args); // 获取运行监听器listeners.starting(bootstrapContext, this.mainApplicationClass); // 通知监听器启动过程开始try {ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); // 创建应用参数ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments); // 预备环境configureIgnoreBeanInfo(environment); // 配置忽略BeanInfoBanner printedBanner = printBanner(environment); // 打印Bannercontext = createApplicationContext(); // 创建应用上下文context.setApplicationStartup(this.applicationStartup); // 设置应用启动状态prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner); // 准备上下文refreshContext(context); // 刷新上下文,执行Bean的生命周期afterRefresh(context, applicationArguments); // 刷新后的操作stopWatch.stop(); // 停止计时if (this.logStartupInfo) { // 如果需要记录启动信息new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch); // 记录启动信息}listeners.started(context); // 通知监听器启动完成callRunners(context, applicationArguments); // 调用Runner}catch (Throwable ex) {handleRunFailure(context, ex, listeners); // 处理运行失败throw new IllegalStateException(ex); // 抛出异常}try {listeners.running(context); // 通知监听器运行中}catch (Throwable ex) {handleRunFailure(context, ex, null); // 处理运行失败throw new IllegalStateException(ex); // 抛出异常}return context; // 返回应用上下文
}

我们重点看

   prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);

继续

private void prepareContext(DefaultBootstrapContext bootstrapContext, ConfigurableApplicationContext context,ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,ApplicationArguments applicationArguments, Banner printedBanner) {// 将环境变量设置到Spring上下文context.setEnvironment(environment);// 对Spring上下文进行后处理postProcessApplicationContext(context);// 应用初始izers,这些是对Spring上下文进行额外配置的组件applyInitializers(context);// 通知监听器,上下文已准备好listeners.contextPrepared(context);// 关闭bootstrap上下文bootstrapContext.close(context);// 如果需要记录启动信息if (this.logStartupInfo) {// 记录启动信息,并判断是否为根上下文logStartupInfo(context.getParent() == null);// 记录Spring Boot的配置信息logStartupProfileInfo(context);}// 注册Spring Boot特定的单例beanConfigurableListableBeanFactory beanFactory = context.getBeanFactory();// 注册应用启动参数为单例bean,键为'springApplicationArguments'beanFactory.registerSingleton("springApplicationArguments", applicationArguments);// 如果有打印的Banner,将其注册为单例bean,键为'springBootBanner'if (printedBanner != null) {beanFactory.registerSingleton("springBootBanner", printedBanner);}// 如果bean工厂是DefaultListableBeanFactory的实例,设置是否允许Bean定义覆盖if (beanFactory instanceof DefaultListableBeanFactory) {((DefaultListableBeanFactory) beanFactory).setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);}// 如果设置了懒惰初始化,添加一个后处理器if (this.lazyInitialization) {context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());}// 加载所有源,通常是Bean定义的来源Set<Object> sources = getAllSources();// 断言源集合不为空,这些源将被加载到Spring上下文中Assert.notEmpty(sources, "Sources must not be empty");// 使用源数组加载Spring上下文load(context, sources.toArray(new Object[0]));// 通知监听器,上下文已加载listeners.contextLoaded(context);
}

【applyInitializers】

		protected void applyInitializers(ConfigurableApplicationContext context) {for (ApplicationContextInitializer initializer : getInitializers()) {Class<?> requiredType = GenericTypeResolver.resolveTypeArgument(initializer.getClass(),ApplicationContextInitializer.class);Assert.isInstanceOf(requiredType, context, "Unable to call initializer.");initializer.initialize(context);}}

在这里插入图片描述

就到了我们自定义实现的代码逻辑中了。

     @Overridepublic void initialize(ConfigurableApplicationContext applicationContext) {System.out.println("--------------------> Handling ApplicationContextInitializedEvent here!");}

继续

listeners.contextPrepared(context);	

又看了熟悉的

	@Overridepublic void contextPrepared(ConfigurableApplicationContext context) {this.initialMulticaster.multicastEvent(new ApplicationContextInitializedEvent(this.application, this.args, context));}

继续

	@Overridepublic void multicastEvent(ApplicationEvent event) {multicastEvent(event, resolveDefaultEventType(event));}
@Override
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {// 如果eventType不为null,则直接使用它;否则,使用resolveDefaultEventType方法来解析事件的默认类型。ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));// 获取一个线程池执行器,它用于异步执行监听器调用。Executor executor = getTaskExecutor();// 获取所有对应该事件类型的监听器。for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {// 如果执行器不为null,则使用它来异步执行监听器调用;// 否则,直接同步调用监听器。if (executor != null) {executor.execute(() -> invokeListener(listener, event));}else {invokeListener(listener, event);}}
}

继续

private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {try {listener.onApplicationEvent(event);}catch (ClassCastException ex) {......}}

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/416379.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

flutter开发web应用网络请求后台失败--记录遇到的跨源资源共享问题

前因 愉快开发flutter的web应用&#xff0c;发现网络请求后台一直请求不通啊&#xff0c;百思不得其解后偶然遇到了跨源资源共享&#xff08;CORS&#xff09;这一名词&#xff0c;才发现了问题关键所在。 什么是跨源资源共享 引用跨源资源共享&#xff08;CORS&#xff09;…

pytorch(四)、完整小网络的搭建和sequential的使用

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言搭建小网络和sequential的使用一、 第一种形式如下&#xff1a;二、第二种方式&#xff0c;使用sequential 前言 至此&#xff0c;神经网络的基础部分就基本结…

初阶数据结构:顺序表

目录 1. 引子&#xff1a;线性表2. 简单数据结构&#xff1a;顺序表2.1 顺序表简介与功能模块分析2.2 顺序表的实现2.2.1 顺序表&#xff1a;存储数据结构的构建2.2.2 顺序表&#xff1a;初始化与空间清理&#xff08;动态&#xff09;2.2.3 顺序表&#xff1a;插入与删除数据2…

Spring Boot程序的打包与运行:构建高效部署流程

引言 在现代应用开发中&#xff0c;高效的打包和部署流程对于项目的开发、测试和上线至关重要。Spring Boot作为一种快速开发框架&#xff0c;提供了方便的打包工具和内嵌式的Web服务器&#xff0c;使得打包和运行变得更加简单。本文将研究在Spring Boot应用中如何进行打包&am…

Vue四个阶段,八个钩子函数

- 创造阶段&#xff1a;创建Vue实例和初始化数据事件&#xff0c;数据代理&#xff0c;监测watch - beforeCreate&#xff0c;只是创建实例&#xff0c;不能this.$el,this.msg,this.方法名&#xff08;&#xff09; - created&#xff0c;数据代理了&#xff0c;能v…

Python 面向对象绘图(Matplotlib篇-16)

Python 面向对象绘图(Matplotlib篇-16)         🍹博主 侯小啾 感谢您的支持与信赖。☀️ 🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ�…

ffmpeg 常用命令行详解

概述 ffmpeg 是一个命令行音视频后期处理软件 1. 裁剪命令 参数说明 -i 文件&#xff0c;orgin.mp3 为待处理源文件-ss 裁剪时间&#xff0c;后跟裁剪开始时间&#xff0c;或者开始的秒数-t 裁剪时间output.mp3 为处理结果文件 ffmpeg -i organ.mp3 -ss 00:00:xx -t 120 o…

4个值得使用的免费爬虫工具

在信息时代&#xff0c;数据的获取对于各行业都至关重要。而在数据采集的众多工具中&#xff0c;免费的爬虫软件成为许多用户的首选。本文将专心分享四款免费爬虫工具&#xff0c;突出介绍其中之一——147采集软件&#xff0c;为您揭示这些工具的优势和应用&#xff0c;助您在数…

了解Vue中日历插件Fullcalendar

实现效果如下图&#xff1a; 月视图 周视图 日视图 官方文档地址&#xff1a;Vue Component - Docs | FullCalendar 1、安装与FullCalendar相关的依赖项 npm install --save fullcalendar/vue fullcalendar/core fullcalendar/daygrid fullcalendar/timegrid fullcalend…

算法笔记(动态规划入门题)

1.找零钱 int coinChange(int* coins, int coinsSize, int amount) {int dp[amount 1];memset(dp,-1,sizeof(dp));dp[0] 0;for (int i 1; i < amount; i)for (int j 0; j < coinsSize; j)if (coins[j] < i && dp[i - coins[j]] ! -1)if (dp[i] -1 || dp[…

springCloud使用apache的http类和RestTemplate以及Eureka

使用apache的&#xff1a; package com.csgholding.pvgpsp.eqp.util;import com.esotericsoftware.minlog.Log; import org.apache.commons.collections4.MapUtils; import org.apache.http.HttpEntity; import org.apache.http.client.config.RequestConfig; import org.apac…

计算机毕业设计-----SSH超市便利店综合管理系统

项目介绍 便利店管理系统共分为两个角色&#xff1a;管理员和销售人员 销售员角色包含以下功能&#xff1a; 销售员登录,管理销售记录等功能。 管理员角色包含以下功能&#xff1a; 管理员角色登录,销售员管理,商品管理,采购管理,库存管理,销售记录管理,成本报表统计,收入报…