SpringFramework事件与监听机制(事件)

本文详细探讨了Spring Framework的事件与监听机制,包括SpringBoot启动过程中的事件发布。在SpringBoot 2.0.2.RELEASE和Spring Framework 5.0.6.RELEASE中,Spring Boot启动时主要发布ContextRefreshedEvent事件。文章分析了不同阶段发布的事件,如ApplicationStartingEvent、ApplicationEnvironmentPreparedEvent、ApplicationPreparedEvent、ApplicationStartedEvent、ApplicationReadyEvent和ContextClosedEvent,并阐述了事件发布者之间的关系。此外,还讨论了SpringBoot事件与Spring Framework事件的类定义层面关系,以及事件发布的具体时机。
摘要由CSDN通过智能技术生成

SpringBoot版本:2.0.2.RELEASE
SpringFramework版本:5.0.6.RELEASE

SpringFramework事件与监听机制

SpringFramework的事件

随着SpringBoot工程的启动,程序会历经以下代码段:

SpringApplication.run(XXXX.class,args);
....
return new SpringApplication(primarySources).run(args);
....

我们先把目光放在SpringApplication#run方法。该方法有一些命令要重点关注:

....
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
....
ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments);
....
prepareContext(context, environment, listeners, applicationArguments,printedBanner);
refreshContext(context);
....
listeners.started(context);
....
listeners.running(context);
....

在《SpringBoot事件与监听机制》已经说明listeners作为SpringBoot的事件发布者,它的行为就是发布SpringBoot事件。而SpringFramework事件的发布在refreshContext(context)函数里,我们继续进入窥探之。一路跟踪,来到下图位置:

/**
	 * Refresh the underlying {@link ApplicationContext}.
	 * @param applicationContext the application context to refresh
	 */
	protected void refresh(ApplicationContext applicationContext) {
   
		Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext);
		((AbstractApplicationContext) applicationContext).refresh();
	}

接下来,程序将离开SpringApplication的内部方法调用,进入AbstractApplicationContext#refresh方法。从类的定义来看,Abs

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值