
Pre
Spring Boot - Application Events 的發(fā)布順序_ApplicationEnvironmentPreparedEvent
概述
Spring Boot 的廣播機(jī)制是基于觀察者模式實(shí)現(xiàn)的,它允許在 Spring 應(yīng)用程序中發(fā)布和監(jiān)聽事件。這種機(jī)制的主要目的是為了實(shí)現(xiàn)解耦,使得應(yīng)用程序中的不同組件可以獨(dú)立地改變和復(fù)用邏輯,而無需直接進(jìn)行通信。
在 Spring Boot 中,事件發(fā)布和監(jiān)聽的機(jī)制是通過 ApplicationEvent
、ApplicationListener
以及事件發(fā)布者(ApplicationEventPublisher
)來實(shí)現(xiàn)的。其中,ApplicationEvent 是所有自定義事件的基礎(chǔ),自定義事件需要繼承自它。
ApplicationListener
是監(jiān)聽特定事件并做出響應(yīng)的接口,開發(fā)者可以通過實(shí)現(xiàn)該接口來定義自己的監(jiān)聽器。事件發(fā)布者(通常由 Spring 的 ApplicationContext
擔(dān)任)負(fù)責(zé)發(fā)布事件。
Code
package com.artisan.event;
import org.springframework.boot.availability.AvailabilityChangeEvent;
import org.springframework.context.ApplicationListener;
/**
* @author 小工匠
* @version 1.0
* @mark: show me the code , change the world
*/
public class AvailabilityChangeListener implements ApplicationListener<AvailabilityChangeEvent> {
/**
* AvailabilityChangeEvent 當(dāng)應(yīng)用程序的“活動(dòng)”和“就緒”狀態(tài)發(fā)生更改時(shí)觸發(fā)。
* 例如,當(dāng)應(yīng)用程序從 LivenessState.CORRECT to 指示應(yīng)用程序處于活動(dòng)狀態(tài),從 ReadinessState.ACCEPTING_TRAFFIC to 指示應(yīng)用程序已準(zhǔn)備好處理傳入請(qǐng)求時(shí),可以觸發(fā)它
*
* 為了處理 AvailabilityChangeEvent ,我們可以通過實(shí)現(xiàn) AvailabilityChangeEvent 作為泛型類型的 ApplicationListener 接口來創(chuàng)建一個(gè)自定義事件偵聽器。
* 此偵聽器可以在主應(yīng)用程序類中手動(dòng)注冊(cè)
*
* @param event the event to respond to
*/
@Override
public void onApplicationEvent(AvailabilityChangeEvent event) {
System.out.println("--------------------> Handling AvailabilityChangeEvent here!");
}
}
如何使用呢?
方式一:
@SpringBootApplication
public class LifeCycleApplication {
/**
* 除了手工add , 在 META-INF下面 的 spring.factories 里增加
* org.springframework.context.ApplicationListener=自定義的listener 也可以
*
* @param args
*/
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(LifeCycleApplication.class);
// 在執(zhí)行 Spring Boot 應(yīng)用程序時(shí),我們可以用來 AvailabilityChangeEvent 更新狀態(tài)。任何應(yīng)用程序組件都可以發(fā)送此類事件來更新應(yīng)用程序的狀態(tài)
springApplication.addListeners(new AvailabilityChangeListener());
springApplication.run(args);
}
}
方式二: 通過spring.factories 配置
org.springframework.context.ApplicationListener=\
com.artisan.event.AvailabilityChangeListener
運(yùn)行日志
源碼分析
首先main方法啟動(dòng)入口
SpringApplication.run(LifeCycleApplication.class, args);
跟進(jìn)去
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
return run(new Class<?>[] { primarySource }, args);
}
繼續(xù)
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
return new SpringApplication(primarySources).run(args);
}
這里首先關(guān)注 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
繼續(xù)run
// 開始啟動(dòng)Spring應(yīng)用程序
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch(); // 創(chuàng)建一個(gè)計(jì)時(shí)器
stopWatch.start(); // 開始計(jì)時(shí)
DefaultBootstrapContext bootstrapContext = createBootstrapContext(); // 創(chuàng)建引導(dǎo)上下文
ConfigurableApplicationContext context = null; // Spring應(yīng)用上下文,初始化為null
configureHeadlessProperty(); // 配置無頭屬性(如:是否在瀏覽器中運(yùn)行)
SpringApplicationRunListeners listeners = getRunListeners(args); // 獲取運(yùn)行監(jiān)聽器
listeners.starting(bootstrapContext, this.mainApplicationClass); // 通知監(jiān)聽器啟動(dòng)過程開始
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); // 創(chuàng)建應(yīng)用參數(shù)
ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments); // 預(yù)備環(huán)境
configureIgnoreBeanInfo(environment); // 配置忽略BeanInfo
Banner printedBanner = printBanner(environment); // 打印Banner
context = createApplicationContext(); // 創(chuàng)建應(yīng)用上下文
context.setApplicationStartup(this.applicationStartup); // 設(shè)置應(yīng)用啟動(dòng)狀態(tài)
prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner); // 準(zhǔn)備上下文
refreshContext(context); // 刷新上下文,執(zhí)行Bean的生命周期
afterRefresh(context, applicationArguments); // 刷新后的操作
stopWatch.stop(); // 停止計(jì)時(shí)
if (this.logStartupInfo) { // 如果需要記錄啟動(dòng)信息
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch); // 記錄啟動(dòng)信息
}
listeners.started(context); // 通知監(jiān)聽器啟動(dòng)完成
callRunners(context, applicationArguments); // 調(diào)用Runner
}
catch (Throwable ex) {
handleRunFailure(context, ex, listeners); // 處理運(yùn)行失敗
throw new IllegalStateException(ex); // 拋出異常
}
try {
listeners.running(context); // 通知監(jiān)聽器運(yùn)行中
}
catch (Throwable ex) {
handleRunFailure(context, ex, null); // 處理運(yùn)行失敗
throw new IllegalStateException(ex); // 拋出異常
}
return context; // 返回應(yīng)用上下文
}
我們重點(diǎn)看
listeners.started(context);
繼續(xù)
void started(ConfigurableApplicationContext context) {
doWithListeners("spring.boot.application.started", (listener) -> listener.started(context));
}
繼續(xù) listener.started(context)
@Override
public void started(ConfigurableApplicationContext context) {
context.publishEvent(new ApplicationStartedEvent(this.application, this.args, context));
AvailabilityChangeEvent.publish(context, LivenessState.CORRECT);
}
關(guān)注AvailabilityChangeEvent.publish(context, LivenessState.CORRECT);
請(qǐng)關(guān)注: getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
繼續(xù)
public static <S extends AvailabilityState> void publish(ApplicationContext context, S state) {
Assert.notNull(context, "Context must not be null");
publish(context, context, state);
}
public static <S extends AvailabilityState> void publish(ApplicationEventPublisher publisher, Object source,
S state) {
Assert.notNull(publisher, "Publisher must not be null");
publisher.publishEvent(new AvailabilityChangeEvent<>(source, state));
}
/**
* 發(fā)布一個(gè)事件,這個(gè)事件可以被訂閱者處理。
*
* @param event 事件對(duì)象,不能為null
* @param eventType 事件類型,用于類型匹配和事件處理器的選擇
*/
protected void publishEvent(Object event, @Nullable ResolvableType eventType) {
// 斷言事件對(duì)象不能為null
Assert.notNull(event, "Event must not be null");
// 如果事件本身就是ApplicationEvent的實(shí)例,直接使用
ApplicationEvent applicationEvent;
if (event instanceof ApplicationEvent) {
applicationEvent = (ApplicationEvent) event;
}
// 否則,使用PayloadApplicationEvent進(jìn)行包裝
else {
applicationEvent = new PayloadApplicationEvent<>(this, event);
// 如果事件類型為null,從PayloadApplicationEvent中獲取
if (eventType == null) {
eventType = ((PayloadApplicationEvent<?>) applicationEvent).getResolvableType();
}
}
// 如果有提前注冊(cè)的事件,直接添加到列表中
if (this.earlyApplicationEvents != null) {
this.earlyApplicationEvents.add(applicationEvent);
}
// 否則,使用ApplicationEventMulticaster進(jìn)行廣播
else {
getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
}
// 如果有父上下文,也發(fā)布事件
if (this.parent != null) {
if (this.parent instanceof AbstractApplicationContext) {
((AbstractApplicationContext) this.parent).publishEvent(event, eventType);
} else {
this.parent.publishEvent(event);
}
}
}
繼續(xù)getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
@Override
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
Executor executor = getTaskExecutor();
for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {
if (executor != null) {
executor.execute(() -> invokeListener(listener, event));
}
else {
invokeListener(listener, event);
}
}
}
protected void invokeListener(ApplicationListener<?> listener, ApplicationEvent event) {
ErrorHandler errorHandler = getErrorHandler();
if (errorHandler != null) {
try {
doInvokeListener(listener, event);
}
catch (Throwable err) {
errorHandler.handleError(err);
}
}
else {
doInvokeListener(listener, event);
}
}
/**
* 調(diào)用一個(gè)事件監(jiān)聽器的方法。
*
* @param listener 要調(diào)用的監(jiān)聽器
* @param event 要處理的事件
*/
private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {
try {
// 直接調(diào)用監(jiān)聽器的onApplicationEvent方法
listener.onApplicationEvent(event);
}
catch (ClassCastException ex) {
String msg = ex.getMessage();
// 如果消息為null或者消息匹配事件類的預(yù)期類型,則忽略異常并記錄debug日志
if (msg == null || matchesClassCastMessage(msg, event.getClass())) {
Log logger = LogFactory.getLog(getClass());
if (logger.isTraceEnabled()) {
logger.trace("Non-matching event type for listener: " + listener, ex);
}
}
// 否則,拋出異常
else {
throw ex;
}
}
}
繼續(xù) 就會(huì)調(diào)到我們自己的業(yè)務(wù)邏輯了文章來源:http://www.zghlxwxcb.cn/news/detail-813667.html
@Override
public void onApplicationEvent(AvailabilityChangeEvent event) {
System.out.println("--------------------> Handling AvailabilityChangeEvent here!");
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-813667.html
到了這里,關(guān)于Spring Boot - Application Events 的發(fā)布順序_AvailabilityChangeEvent的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!