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); } }