spring boot 源碼分析(八) 事件機制 之 SpringApplicationEvent子類分析

1、前言java

前面的文章咱們講解了一下spring boot配置文件加載的相關源碼分析,下面咱們將從源碼角度講解一下spring boot 相關的事件機制, 本章咱們將從 ApplicationEnvironmentPreparedEvent類分析一下spring boot 的事件機制。spring

2、類圖app

3、源碼解析源碼分析

ApplicationEnvironmentPreparedEventthis

package org.springframework.boot.context.event;

import org.springframework.boot.SpringApplication;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;

//當springApplication啓動而且環境第一次可用檢查和修改的時候發佈事件
@SuppressWarnings("serial")
public class ApplicationEnvironmentPreparedEvent extends SpringApplicationEvent {

	private final ConfigurableEnvironment environment;

	
	public ApplicationEnvironmentPreparedEvent(SpringApplication application,
			String[] args, ConfigurableEnvironment environment) {
		super(application, args);
		this.environment = environment;
	}

	
	public ConfigurableEnvironment getEnvironment() {
		return this.environment;
	}

}

ApplicationFailedEventspa

package org.springframework.boot.context.event;

import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;

//當SpringApplication啓動失敗時發佈事件
@SuppressWarnings("serial")
public class ApplicationFailedEvent extends SpringApplicationEvent {

	private final ConfigurableApplicationContext context;

	private final Throwable exception;

	
	public ApplicationFailedEvent(SpringApplication application, String[] args,
			ConfigurableApplicationContext context, Throwable exception) {
		super(application, args);
		this.context = context;
		this.exception = exception;
	}

	
	public ConfigurableApplicationContext getApplicationContext() {
		return this.context;
	}

	
	public Throwable getException() {
		return this.exception;
	}

}

ApplicationPreparedEvent命令行

package org.springframework.boot.context.event;

import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

//當SpringApplication啓動後而且上下文充分準備但沒刷新的時候發佈事件,bean定義將會加載而且環境在這步已經準備好被使用
@SuppressWarnings("serial")
public class ApplicationPreparedEvent extends SpringApplicationEvent {

	private final ConfigurableApplicationContext context;

	
	public ApplicationPreparedEvent(SpringApplication application, String[] args,
			ConfigurableApplicationContext context) {
		super(application, args);
		this.context = context;
	}

	
	public ConfigurableApplicationContext getApplicationContext() {
		return this.context;
	}

}

ApplicationReadyEventcode

package org.springframework.boot.context.event;

import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;

//表示SpringApplication已經準備好爲外面提供服務
@SuppressWarnings("serial")
public class ApplicationReadyEvent extends SpringApplicationEvent {

	private final ConfigurableApplicationContext context;

	
	public ApplicationReadyEvent(SpringApplication application, String[] args,
			ConfigurableApplicationContext context) {
		super(application, args);
		this.context = context;
	}

	
	public ConfigurableApplicationContext getApplicationContext() {
		return this.context;
	}

}

ApplicationStartedEvent事件

package org.springframework.boot.context.event;

import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;


//一旦上下文刷新但在任何應用或命令行調用前都會被髮布事件
@SuppressWarnings("serial")
public class ApplicationStartedEvent extends SpringApplicationEvent {

	private final ConfigurableApplicationContext context;

	
	public ApplicationStartedEvent(SpringApplication application, String[] args,
			ConfigurableApplicationContext context) {
		super(application, args);
		this.context = context;
	}

	
	public ConfigurableApplicationContext getApplicationContext() {
		return this.context;
	}

}

ApplicationStartingEvent get

package org.springframework.boot.context.event;

import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;

//當springApplication啓動後發佈事件
//但他是在Environment和ApplicationContext可用以前
//在ApplicationListeners註冊以後。
@SuppressWarnings("serial")
public class ApplicationStartingEvent extends SpringApplicationEvent {

	
	public ApplicationStartingEvent(SpringApplication application, String[] args) {
		super(application, args);
	}

}
相關文章
相關標籤/搜索