我们说SpringBoot是Spring框架对“约定优先于配置(Convention Over Configuration)"理念的最佳实践的产物,一个典型的SpringBoot应用本质上其 实就是一个基于Spring框架的应用
如果非说SpringBoot微框架提供了点儿自己特有的东西,在核心类层面(除了各种场景下的自动配置一站式插拔模块),也就是SpringApplication了。
SpringApplication将一个典型的Spring应用启动的流程“模板化”(这里是动词),在没有特殊需求的情况下,默认模板化后的执行流程就可以满足需求了;但有特殊需求也没有关系,SpringApplication在合适的流程结点开放了一系列不同类型的扩展点,我们可以通过这些扩展点对SpringBoot程序的启动和关闭过程进行扩展。
最简单的扩展或配置是SpringApplication通过一系列设置方法(setters)开发的定制方式 . . .
大部分情况下,SpringApplication已经提供了很好的默认设置,所以,我们不再对这些表层进行探究了,因为对表层之下的东西进行探究才是我们的最终目的。
SpringApplication的(静态)run方法的实现的主要流程大体可以归纳如下:
SpringFactoriesLoader
可以查找到并加载的SpringApplicationRunListener
,调用它们的started()方法,告诉这些SpringApplicationRunListener,SpringBoot应用要开始执行了
整个过程看起来冗长无比,但其实很多都是一些事件通知的扩展点,如果我们将这些逻辑暂时忽略,那么,其实整个SpringBoot应用启动的逻辑就可以压缩到极其精简的几步。 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UstTIBSw-1614751600844)(https://i.loli.net/2018/01/21/5a646ee77ff52.jpg)]
前后对比我们就可以发现,其实SpringApplication提供的这些各类扩展点 近乎“喧宾夺主”,占据了一个Spring应用启动逻辑的大部分“江山”,除了初 始化并准备好ApplicationContext,剩下的大部分工作都是通过这些扩展点完成 的,所以,我们有必要对各类扩展点进行逐一剖析,以便在需要的时候可以信 手拈来,为我所用。
SpringApplicationRunListener是一个只有SpringBoot应用的main方法执行过程中接收不同执行时点事件通知的监听者:
public interface SpringApplicationRunListener {
void started();
void environmentPrepared( ConfigurableEnvironment environment);
void contextPrepared( ConfigurableApplicationContext context);
void contextLoaded( ConfigurableApplicationContext context);
void finished( ConfigurableApplicationContext context, Throwable exception);
}
对于我们来说,基本没什么常见的场景需要自己实现一个SpringApplicationRunListener,即使SpringBoot默认也只实现了一个org.springframework.boot.context.event.EventPublishingRunListener,用于在SpringBoot启动的不同时点发布不同的应用事件类型(ApplicationEvent),如果有哪些ApplicationListener对这些应用事件感兴趣,则可以接收并处理。
假设我们真的有场景需要自定义一个SpringApplicationRunListener实现,那么有一点需要注意,即任何一个SpringApplicationRunListener实现类的构造方法(Constructor)需要有两个构造参数,一个构造参数的类型就是我们的org.springframework.boot.SpringApplication,另外一个就是args参数列表的String[]:
public class DemoSpringApplicationRunListener implements SpringApplicationRunListener {
@Override
public void started() {
// do whatever you want to do
}
@Override
public void environmentPrepared( ConfigurableEnvironment environment) {
// do whatever you want to do
}
@Override
public void contextPrepared( ConfigurableApplicationContext context) {
// do whatever you want to do
}
@Override
public void contextLoaded( ConfigurableApplicationContext context) {
// do whatever you want to do
}
@Override
public void finished( ConfigurableApplicationContext context, Throwable exception) {
// do whatever you want to do
}
之后,我们可以通过SpringFactoriesLoader立下的规矩,在当前SpringBoot应用的classpath下的META-INF/spring.factories文件中进行类似如下的配置:
org.springframework.boot.SpringApplicationRunListener=\
com.self.springboot.demo.DemoSpringApplicationRunListener
然后SpringApplication就会在运行的时候调用它啦!
ApplicationListener其实是老面孔,属于Spring框架对Java中实现的监听者模式的一种框架实现,这里唯一值得着重强调的是,对于初次接触SpringBoot,但对Spring框架本身又没有过多接触的开发者来说,可能会将这个名字与SpringApplicationRunListener混淆。
如果我们要为SpringBoot应用添加自定义的ApplicationListener,有两种方式:
通过SpringApplication.addListeners(… )或者SpringApplication.setListeners(… )方法添加一个或者多个自定义的ApplicationListener;
借助SpringFactoriesLoader机制,在META-INF/spring.factories文件中添加配置(以下代码是为SpringBoot默认注册的ApplicationListener配置)
org.springframework.context.ApplicationListener= org.springframework.boot.builder.ParentContextCloserApplicationListener, org.springframework.boot.cloudfoundry.VcapApplicationListener, org.springframework.boot.context.FileEncodingApplicationListener, org.springframework.boot.context.config.AnsiOutputApplicationListener, org.springframework.boot.context.config.ConfigFileApplicationListener, org.springframework.boot.context.config.DelegatingApplicationListener, org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicat- ionListener, org.springframework.boot.logging.ClasspathLoggingApplicationListener, org.springframework.boot.logging.LoggingApplicationListener
关于ApplicationListener,我们就说这些。
ApplicationContextInitializer也是Spring框架原有的概念,这个类的主要目的是在ConfigurableApplictaionContext类型(或者子类型)的ApplicationContext的refresh之前,允许我们对ConfigurableApplicationContext的实例做进一步的设置和处理。
实现一个ApplicationContextInitializer很简单,因为它只有一个方法需要实现:
public class DemoApplicationContextInitializer implements ApplicationContextInitializer {
@Override
public void initialize( ConfigurableApplicationContext applicationContext) {
// do whatever you want with applicationContext,
// e. g. applicationContext. registerShutdownHook();
}
}
不过,一般情况下我们基本不会需要自定义一个ApplicationContextInitializer,即使SpringBoot框架默认也只是注册了三个实现:
不过,一般情况下我们基本不会需要自定义一个ApplicationContextInitializer,即使SpringBoot框架默认也只是注册了三个实现:
org. springframework. context. ApplicationContextInitializer=\ org. springframework. boot. context. ConfigurationWarningsApplication- ContextInitializer,\ org. springframework. boot. context. ContextIdApplicationContextInitia- lizer,\ org. springframework. boot. context. config. DelegatingApplicationContex- tInitializer 如果我们真的需要自定义一个ApplicationContextInitializer,那么只要像上面这样,通过SpringFactoriesLoader机制进行配置,或者通过SpringApplication.addInitializers(…) 设置即可。
CommandLineRunner属于SpringBoot应用特定的回调扩展接口:
public interface CommandLineRunner {
void run( String... args) throws Exception;
}
CommandLineRunner需要关注的两点:
与其他几个扩展点接口类型相似,建议CommandLineRunner的实现类使用@org.springframework.core.annotation.Order进行标注或者实现org.springframework.core.Ordered接口,便于对它们的执行顺序进行调整,这其实十分重要,我们不希望顺序不当的CommandLineRunner实现类阻塞了后面其他CommandLineRunner的执行。