springboot启动流程


原图链接:https://www.processon.com/view/link/59812124e4b0de2518b32b6e
本文的分析基于Spring Boot 2.1.5,非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.factories是Spring 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


相对于只读的ApplicationContext而言,ConfigurableApplicationContext提供了配置上下文的接口,如设置Environment、监听器、切面类、关闭上下文的钩子等,还有刷新上下文的接口。默认是只读的接口,接口名前面加Configurable对应是一个提供可配置接口的新接口——在Spring很多配置相关的接口中都有这样的继承形式,例如ConfigurableEnvironment和Environment、ConfigurablePropertyResolver和PropertyResolver、ConfigurableBeanFactory和BeanFactory等等。
继承的三个父类接口里,Closeable提供了关闭时资源释放的接口,Lifecycle是提供对生命周期控制的接口(start\stop)以及查询当前运行状态的接口,ApplicationContext则是配置上下文的中心配置接口,继承了其他很多配置接口,其本身提供查询诸如id、应用程序名等上下文档案信息的只读接口,以及构建自动装配bean的工厂(注释上官方说该接口提供的工厂是用于注册上下文外部的bean的,但调试发现和在程序内@Autowired获取到的工厂是同一个对象...)。简单写下ApplicationContext继承的父类接口。

  • EnvironmentCapable
    提供Environment接口。
  • MessageSource
    国际化资源接口。
  • ApplicationEventPublisher
    事件发布器。
  • ResourcePatternResolver
    资源加载器。
  • HierarchicalBeanFactory、ListableBeanFactory
    这两个都继承了bean容器的根接口BeanFactory

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()方法。

  • 1.遍历SpringApplication初始化过程中加载的SpringApplicationRunListeners
  • 2.调用Starting()监听SpringApplication的启动
  • 3.加载SpringBoot配置环境(ConfigurableEnvironment)
  • 4.设置banner属性
  • 5.创建ConfigurableApplicationContext(应用配置上下文)
  • 6.将listeners、environment、applicationArguments、bannner等重要组件与上下文对象关联
  • 7.bean的实力化完成
/**
 * Run the Spring application, creating and refreshing a new
 * {@link ApplicationContext}.
 * @param args the application arguments (usually passed from a Java main method)
 * @return a running {@link ApplicationContext}
 */
public ConfigurableApplicationContext run(String... args) {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    ConfigurableApplicationContext context = null;
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
    configureHeadlessProperty();
    //1.遍历SpringApplication初始化过程中加载的SpringApplicationRunListeners
    SpringApplicationRunListeners listeners = getRunListeners(args);
    //2.调用starting()监听SpringApplication的启动
    listeners.starting();
    try {
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
        //3.加载SpringBoot配置环境
        ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
        configureIgnoreBeanInfo(environment);
        //4.设置banner属性
        Banner printedBanner = printBanner(environment);
        //5.创建ConfigurableApplicationContext(应用配置上下文)
        context = createApplicationContext();
        exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
                new Class[] { ConfigurableApplicationContext.class }, context);
        //6.将listeners、environment、applicationArguments、banner等重要组件与上下文对象关联
        prepareContext(context, environment, listeners, applicationArguments, printedBanner);
        //7.实例化bean
        refreshContext(context);
        afterRefresh(context, applicationArguments);
        stopWatch.stop();
        if (this.logStartupInfo) {
            new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
        }
        listeners.started(context);
        callRunners(context, applicationArguments);
    }
    catch (Throwable ex) {
        handleRunFailure(context, ex, exceptionReporters, listeners);
        throw new IllegalStateException(ex);
    }

    try {
        listeners.running(context);
    }
    catch (Throwable ex) {
        handleRunFailure(context, ex, exceptionReporters, null);
        throw new IllegalStateException(ex);
    }
    return context;
}

1.遍历SpringApplication初始化过程中加载的SpringApplicationRunListeners

private SpringApplicationRunListeners getRunListeners(String[] args) {
    Class<?>[] types = new Class<?>[] { SpringApplication.class, String[].class };//SpringApplicationRunListener的构造函数参数类型
    return new SpringApplicationRunListeners(logger, getSpringFactoriesInstances(SpringApplicationRunListener.class, types, this, args));
}

2.调用Starting()监听SpringApplication的启动

public void starting() {
    //遍历所有的SpringApplicationRunListener,调用starting()方法监听SpringApplication的启动
    for (SpringApplicationRunListener listener : this.listeners) {
        listener.starting();
    }
}

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

3.加载SpringBoot配置环境(ConfigurableEnvironment)
加载SpringBoot配置环境(configurableEnvironment),如果是通过web容器发布,会加载StandardEnvironment。将配置文件(Environment)加入到监听器对象中(SpringApplicationRunListeners)

private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
        ApplicationArguments applicationArguments) {
    // Create and configure the environment
    //如果environment不为空直接返回 || 如果是web环境则直接实例化StandardServletEnvironment类 || 如果不是web环境则直接实例化StandardEnvironment类
    ConfigurableEnvironment environment = getOrCreateEnvironment();
    //配置环境信息
    configureEnvironment(environment, applicationArguments.getSourceArgs());
    //通知所有的监听者,环境已经准备好了
    listeners.environmentPrepared(environment);
    bindToSpringApplication(environment);
    if (!this.isCustomEnvironment) {
        environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,
                deduceEnvironmentClass());
    }
    ConfigurationPropertySources.attach(environment);
    return environment;
}

4.设置banner属性

private Banner printBanner(ConfigurableEnvironment environment) {
    //如果未开启banner打印直接返回
    if (this.bannerMode == Banner.Mode.OFF) {
        return null;
    }
    //创建ResourceLoader对象
    ResourceLoader resourceLoader = (this.resourceLoader != null) ? this.resourceLoader
            : new DefaultResourceLoader(getClassLoader());
    //创建SpringApplicationBannerPrinter,该对象用来打印banner
    SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(resourceLoader, this.banner);
    //如果bannerMode模式为LOG,则将bannner打印到log文件中
    if (this.bannerMode == Mode.LOG) {
        return bannerPrinter.print(environment, this.mainApplicationClass, logger);
    }
    //打印banner到控制台
    return bannerPrinter.print(environment, this.mainApplicationClass, System.out);
}

5.初始化ConfigurableApplicationContext(应用配置上下文)

public enum WebApplicationType {
    /**
     * The application should not run as a web application and should not start an
     * embedded web server.
     */
    // 应用程序不是web应用,也不应该用web服务器去启动
    NONE,
    /**
     * The application should run as a servlet-based web application and should start an
     * embedded servlet web server.
     */
    //应用程序应作为基于servlet的web应用程序运行,并应启动嵌入式servlet web(tomcat)服务器
    SERVLET,
    /**
     * The application should run as a reactive web application and should start an
     * embedded reactive web server.
     */
    //应用程序应作为 reactive web应用程序运行,并应启动嵌入式 reactive web服务器。
    REACTIVE;
}

根据webEnvironment是否是web环境创建默认的contextClass,AnnotationConfigEnbeddedWebApplicationContext(通过扫描所有注解类来加载bean)和ConfigurableWebApplicationContext),最后通过BeanUtils实例化上下文对象,并返回。

/**
 * Strategy method used to create the {@link ApplicationContext}. By default this
 * method will respect any explicitly set application context or application context
 * class before falling back to a suitable default.
 * @return the application context (not yet refreshed)
 * @see #setApplicationContextClass(Class)
 */
protected ConfigurableApplicationContext createApplicationContext() {
    //根据webEnvironment是否是web环境创建默认的contextClass
    Class<?> contextClass = this.applicationContextClass;
    if (contextClass == null) {
        try {
            switch (this.webApplicationType) {
            case SERVLET:
                //AnnotationConfigServletWebServerApplicationContext
                contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS);
                break;
            case REACTIVE:
                //AnnotationConfigReactiveWebServerApplicationContext
                contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
                break;
            default:
                //AnnotationConfigApplicationContext
                contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
            }
        }
        catch (ClassNotFoundException ex) {
            throw new IllegalStateException(
                    "Unable create a default ApplicationContext, " + "please specify an ApplicationContextClass",
                    ex);
        }
    }
    //BeanUtils实例化上下文对象
    return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
}

6.将listeners、environment、applicationArguments、banner等重要组件与上下文对象关联

private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment,
        SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) {
    //设置上下文的environment
    context.setEnvironment(environment);
    //应用上下文后处理
    postProcessApplicationContext(context);
    //在context refresh之前,对其应用ApplicationContextInitializer
    applyInitializers(context);
    //上下文准备
    listeners.contextPrepared(context);
    //打印启动日志和启动应用的profile
    if (this.logStartupInfo) {
        logStartupInfo(context.getParent() == null);
        logStartupProfileInfo(context);
    }
    // Add boot specific singleton beans
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    //向beanFactory注册单例bean:命令行参数bean
    beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
    if (printedBanner != null) {
        //向beanFactory注册单例bean:banner bean
        beanFactory.registerSingleton("springBootBanner", printedBanner);
    }
    if (beanFactory instanceof DefaultListableBeanFactory) {
        ((DefaultListableBeanFactory) beanFactory)
                .setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
    }
    // Load the sources
    //获取SpringApplication的primarySources属性
    Set<Object> sources = getAllSources();
    Assert.notEmpty(sources, "Sources must not be empty");
    //将bean加载到应用上下文
    load(context, sources.toArray(new Object[0]));
    //向上下文添加ApplicationListener,并广播ApplicationPreparedEvent事件
    listeners.contextLoaded(context);
}

7.bean的实例化完成,刷新应用上下文

refreshContext(context);
...
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;

参考链接:https://www.cnblogs.com/theRhyme/p/11057233.html#_label5

posted @ 2021-02-22 19:54  lovelifest  阅读(245)  评论(0编辑  收藏  举报