SpringBoot进阶(叁):Spring Boot启动过程分析

244 阅读6分钟

首先贴一张很不错的图,SpringBoot启动结构图,图片出自SpringBoot启动流程解析。
本文的分析基于Spring Boot 2.1.*,非Spring的代码只有下面这个启动main函数:

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(AppServer.class);
        application.run(args);
    }
}

构造函数

SpringApplication的构造函数实例化了初始化上下文的各种接口–ApplicationContextInitializer以及监听器–ApplicationListener,要注意的是这里的实例化,并不像平时的Spring Components一样通过注解和扫包完成,而是通过一种不依赖Spring上下文的加载方法,这样才能在Spring完成启动前做各种配置。

Spring的解决方法是以接口的全限定名作为key,实现类的全限定名作为value记录在项目的META-INF/spring.factories文件中,然后通过SpringFactoriesLoader工具类提供静态方法进行类加载并缓存下来,spring.factoriesSpring Boot的核心配置文件,后面会继续说明。

另外比较有意思的是两个deduce方法,Spring Boot项目主要的目标之一就是自动化配置,通过这两个deduce方法可以看出,Spring Boot的判断方法之一是检查系统中是否存在的核心类。

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();//通过核心类判断是否开启、开启什么web容器
    //实例化初始器
    setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
    //实例化监听器
    setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
    this.mainApplicationClass = deduceMainApplicationClass();
}

Run

初始化完成之后就进到了run方法,run方法完成了所有Spring的整个启动过程:准备Environment——发布事件——创建上下文、bean——刷新上下文——结束,其中穿插了很多监听器的动作,并且很多逻辑都是靠各种监听器的实现类执行的,所以在分析run方法之前,先看下各种核心监听器、接口的作用。

ConfigurableApplicationContext

不得不说,用IDEA分析源码真的很方便,直接生成接口的UML类图:
在这里插入图片描述
相对于只读的ApplicationContext而言,ConfigurableApplicationContext提供了配置上下文的接口,如设置Environment、监听器、切面类、关闭上下文的钩子等,还有刷新上下文的接口。默认是只读的接口,接口名前面加Configurable对应是一个提供可配置接口的新接口——在Spring很多配置相关的接口中都有这样的继承形式,例如ConfigurableEnvironmentEnvironmentConfigurablePropertyResolverPropertyResolverConfigurableBeanFactoryBeanFactory等等。

继承的三个父类接口里,Closeable提供了关闭时资源释放的接口,Lifecycle是提供对生命周期控制的接口(start\stop)以及查询当前运行状态的接口,ApplicationContext则是配置上下文的中心配置接口,继承了其他很多配置接口,其本身提供查询诸如id、应用程序名等上下文档案信息的只读接口,以及构建自动装配bean的工厂(注释上官方说该接口提供的工厂是用于注册上下文外部的bean的,但调试发现和在程序内@Autowired获取到的工厂是同一个对象…)。简单写下ApplicationContext继承的父类接口。

  • EnvironmentCapable 提供Environment接口。
  • MessageSource 国际化资源接口。
  • ApplicationEventPublisher 事件发布器。
  • ResourcePatternResolver 资源加载器。
  • HierarchicalBeanFactoryListableBeanFactory 这两个都继承了bean容器的根接口BeanFactory,具体在另一篇博客Spring的bean工厂分析分析。
  • ConfigurableEnvironment 一般在写业务代码时使用的都是只读类型的接口Environment,该接口是对运行程序环境的抽象,是保存系统配置的中心,而在启动过程中使用的则是可编辑的ConfigurableEnvironment。接口的UML类图如下,提供了合并父环境、添加active profile以及一些设置解析配置文件方式的接口。
    在这里插入图片描述

其中一个比较重要的方法MutablePropertySources getPropertySources();,该方法返回一个可编辑的PropertySources,如果有在启动阶段自定义环境的PropertySources的需求,就可以通过该方法设置。

EventPublishingRunListener

在这里插入图片描述

该监听器实际上是一个用于广播Spring事件的广播器,实现SpringApplicationRunListener接口的方法都是包装一个Spring事件并进行广播,例如:

@Override
public void contextPrepared(ConfigurableApplicationContext context) {
    this.initialMulticaster.multicastEvent(new ApplicationContextInitializedEvent(this.application, this.args, context));
}
@Override
public void running(ConfigurableApplicationContext context) {
    context.publishEvent(new ApplicationReadyEvent(this.application, this.args, context));
}

可以看到有两种广播方式,一种是当Spring还在启动的时候,通过监听器内部的SimpleApplicationEventMulticaster广播器进行广播;一种是当Spring启动完成内部的广播器可用时,直接调用上下文提供的接口进行广播。

继续分析Run

了解了一些核心的接口后,就可以启动Debug模式运行Run方法了,由于涉及的方法调用很多,以下代码将拆分源码,并将方法签名记在前面。

首先开启了一个秒表用来统计启动时间并在日志打印(如果开启控制字),声明了一些在后面需要用到的变量,然后开始初始化SpringApplicationRunListener类型的监听器,SpringApplicationRunListeners对监听器List进行了封装,例如调用.starting()时会遍历内部所有监听器调用其.starting()方法。

public ConfigurableApplicationContext run(String... args){
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    ConfigurableApplicationContext context = null;
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
    configureHeadlessProperty();//开启设置,让系统模拟不存在io设备,略。。
    SpringApplicationRunListeners listeners = getRunListeners(args);//初始化监听器
    listeners.starting();
    ...
private SpringApplicationRunListeners getRunListeners(String[] args) {
    Class<?>[] types = new Class<?>[] { SpringApplication.class, String[].class };//SpringApplicationRunListener的构造函数参数类型
    return new SpringApplicationRunListeners(logger, getSpringFactoriesInstances(SpringApplicationRunListener.class, types, this, args));
}

private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) {
    ClassLoader classLoader = getClassLoader();//从当前线程获取类加载器
    //Spring的类加载工具会从注册文件META-INF/spring.factories用指定的类加载器加载类,这里返回相应类型的实现类全限定名
    Set<String> names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
    List<T> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);//实例化
    //Spring的排序工具,对继承了Ordered接口或者@Priority标记的类进行排序
    AnnotationAwareOrderComparator.sort(instances);
    return instances;
}

调试发现,注册为SpringApplicationRunListener的实现类只有EventPublishingRunListener,之前说过该注册器是一个用于广播Spring事件的广播器,进到构造函数中可以看到都有哪些监听器被绑定到了这个广播器中,这里每个监听器的作用就不再深入了,需要说的是,如果在项目中有什么需要集成到Spring的框架,可以注册SpringApplicationRunListener\ApplicationListener的实现类,监听Spring的不同启动事件并执行集成的逻辑。当然也有别的方法,例如:Creating a Custom Starter with Spring Boot

在这里插入图片描述

继续往下看run方法,这里重点是准备Environment的逻辑。首先Spring会根据web容器的类型新建一个ConfigurableEnvironment,不同的web容器类型的Environment会重载customizePropertySources方法,该方法会注入不同的propertySources,例如如果开启内嵌的Servlet容器,就会注入servlet context init params等相关的参数。接下来会对新建的Environment执行配置写入的逻辑,主要是把main方法中设置到SpringApplication的参数写入到Environment中,然后发布ApplicationEnvironmentPreparedEvent事件,做一些绑定后返回Environment

try {
    ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);//封装main方法的参数
    //初始化填充Environment的参数
    ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments);
    configureIgnoreBeanInfo(environment);//设置获取BeanInfo的一个参数,有兴趣的可以去了解下Introspector.getBeanInfo(Class<?> beanClass, int flags)这个方法
    ...
private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments) {
    //新建\获取当前Environment实例
    ConfigurableEnvironment environment = getOrCreateEnvironment();
    configureEnvironment(environment, applicationArguments.getSourceArgs());//配置参数
    listeners.environmentPrepared(environment);//发布事件
    bindToSpringApplication(environment);//绑定"spring.main"为当前的application,做SpEL用
    if (!this.isCustomEnvironment) {//转换environment的类型,但这里应该类型和deduce的相同不用转换
        environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment, deduceEnvironmentClass());
    }
    //将现有的配置封装成ConfigurationPropertySourcesPropertySource,看起来是为了做SpEL的,看不懂~
    ConfigurationPropertySources.attach(environment);
    return environment;
}
protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) {
    if (this.addConversionService) {//默认开启,会注入一组转换工具,例如StringToDurationConverter
        ConversionService conversionService = ApplicationConversionService.getSharedInstance();
        environment.setConversionService(ConfigurableConversionService) conversionService);
    }
    configurePropertySources(environment, args);//如果main启动时设置了默认参数或者有命令行参数,则写入到environment中
    configureProfiles(environment, args);//如果main启动时设置了profile,则写入到environment的ActiveProfiles中
}

继续往下看run方法,这里会创建Spring的上下文实例,详情请看另一篇博客Spring Boot Context分析,简而言之就是根据Web容器类型的不同来创建不用的上下文实例。

Banner printedBanner = printBanner(environment);//打印标语
context = createApplicationContext();//创建上下文实例
//异常播报器,默认有org.springframework.boot.diagnostics.FailureAnalyzers
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context);
...

继续往下看run方法,接下来是对刚创建的上下文完成加载。加载过程先填充Environment以及设置的参数,然后执行注册到spring.factoriesApplicationContextInitializer切面,如果自己实现切面的话要注意这时context已经有的信息是什么。接着发布ApplicationContextInitializedEvent事件,然后加载bean,最后发布ApplicationPreparedEvent事件。

prepareContext(context, environment, listeners, applicationArguments,printedBanner);
...
private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment, SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) {
    context.setEnvironment(environment);
    //如果application有设置beanNameGenerator、resourceLoader就将其注入到上下文中,并将转换工具也注入到上下文中
    postProcessApplicationContext(context);
    applyInitializers(context);//调用初始化的切面
    listeners.contextPrepared(context);//发布ApplicationContextInitializedEvent事件
    if (this.logStartupInfo) {//日志
        logStartupInfo(context.getParent() == null);
        logStartupProfileInfo(context);
    }
    // Add boot specific singleton beans
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    beanFactory.registerSingleton("springApplicationArguments", applicationArguments);//注入main方法的参数
    if (printedBanner != null) {
        beanFactory.registerSingleton("springBootBanner", printedBanner);
    }
    if (beanFactory instanceof DefaultListableBeanFactory) {
        //如果bean名相同的话是否允许覆盖,默认为false,相同会抛出异常
        ((DefaultListableBeanFactory) beanFactory).setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
    }
    // 这里获取到的是BootstrapImportSelectorConfiguration这个class,而不是自己写的启动来,这个class是在之前注册的BootstrapApplicationListener的监听方法中注入的
    Set<Object> sources = getAllSources();
    Assert.notEmpty(sources, "Sources must not be empty");
    load(context, sources.toArray(new Object[0]));//加载sources 到上下文中
    listeners.contextLoaded(context);//发布ApplicationPreparedEvent事件
}

回到run方法,在实例化上下文并完成相关配置后,会刷新上下文。

refreshContext(context);
...
AbstractApplicationContext

public void refresh() throws BeansException, IllegalStateException {
    synchronized (this.startupShutdownMonitor) {
        //记录启动时间、状态,web容器初始化其property,复制listener
        prepareRefresh();
        //这里返回的是context的BeanFactory
        ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
        //beanFactory注入一些标准组件,例如ApplicationContextAwareProcessor,ClassLoader等
        prepareBeanFactory(beanFactory);
        try {
            //给实现类留的一个钩子,例如注入BeanPostProcessors,这里是个空方法
            postProcessBeanFactory(beanFactory);

            // 调用切面方法
            invokeBeanFactoryPostProcessors(beanFactory);

            // 注册切面bean
            registerBeanPostProcessors(beanFactory);

            // Initialize message source for this context.
            initMessageSource();

            // bean工厂注册一个key为applicationEventMulticaster的广播器
            initApplicationEventMulticaster();

            // 给实现类留的一钩子,可以执行其他refresh的工作,这里是个空方法
            onRefresh();

            // 将listener注册到广播器中
            registerListeners();

            // 实例化未实例化的bean
            finishBeanFactoryInitialization(beanFactory);

            // 清理缓存,注入DefaultLifecycleProcessor,发布ContextRefreshedEvent
            finishRefresh();
        }

        catch (BeansException ex) {
            if (logger.isWarnEnabled()) {
                logger.warn("Exception encountered during context initialization - " +
                        "cancelling refresh attempt: " + ex);
            }

            // Destroy already created singletons to avoid dangling resources.
            destroyBeans();

            // Reset 'active' flag.
            cancelRefresh(ex);

            // Propagate exception to caller.
            throw ex;
        }

        finally {
            // Reset common introspection caches in Spring's core, since we
            // might not ever need metadata for singleton beans anymore...
            resetCommonCaches();
        }
    }
}

回到run方法,最后的逻辑就是发布启动完成的事件,并调用监听者的方法。

    ...
    afterRefresh(context, applicationArguments);//给实现类留的钩子,这里是一个空方法。
    stopWatch.stop();
    if (this.logStartupInfo) {
        new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
    }
    listeners.started(context);//发布ApplicationStartedEvent事件
    callRunners(context, applicationArguments);
}
catch (Throwable ex) {
    handleRunFailure(context, ex, exceptionReporters, listeners);
    throw new IllegalStateException(ex);
}
try {
    listeners.running(context);//发布ApplicationReadyEvent事件
}
catch (Throwable ex) {
    handleRunFailure(context, ex, exceptionReporters, null);
    throw new IllegalStateException(ex);
}
return context;