SpringBoot 启动过程 SpringApplication.run

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: SpringBoot 启动过程 SpringApplication.run

开启日志

logging:
  level:
    org.springframework: trace

启动项目从日志中依次可以看到这么几个主要类

PropertySourcesPropertyResolver

ClassPathBeanDefinitionScanner

DefaultListableBeanFactory

DockerApplication

SpringApplication

ConfigFileApplicationListener

ConfigServletWebServerApplicationContext

SpringFactoriesLoader

PathMatchingResourcePatternResolver

OnClassCondition

OnWebApplicationCondition

OnBeanCondition

ConfigurationClassBeanDefinitionReader

TomcatServletWebServerFactory

AutowiredAnnotationBeanPostProcessor

CommonAnnotationBeanPostProcessor

ThreadPoolTaskExecutor

RequestMappingHandlerMapping

TomcatWebServer

其基本流程

加载配置文件,装载bean对象,初始化请求映射器,启动内嵌的tomcat。

配置文件加载日志

ConfigFileApplicationListener  : Skipped missing config 'file:./config/application.properti
ConfigFileApplicationListener  : Skipped missing config 'file:./config/application.xml' (fi
ConfigFileApplicationListener  : Skipped missing config 'file:./config/application.yml' (fi
ConfigFileApplicationListener  : Skipped missing config 'file:./config/application.yaml' (f
ConfigFileApplicationListener  : Skipped missing config 'file:./application.properties' (fi
ConfigFileApplicationListener  : Skipped missing config 'file:./application.xml' (file:./ap
ConfigFileApplicationListener  : Skipped missing config 'file:./application.yml' (file:./ap
ConfigFileApplicationListener  : Skipped missing config 'file:./application.yaml' (file:./a
ConfigFileApplicationListener  : Skipped missing config classpath:/config/application.prope
ConfigFileApplicationListener  : Skipped missing config classpath:/config/application.xml  
ConfigFileApplicationListener  : Skipped missing config classpath:/config/application.yml  
ConfigFileApplicationListener  : Skipped missing config classpath:/config/application.yaml 
ConfigFileApplicationListener  : Skipped missing config classpath:/application.properties  
ConfigFileApplicationListener  : Skipped missing config classpath:/application.xml         
ConfigFileApplicationListener  : Loaded config file 'file:.../target/classes/application.yml'  (classpath:/application.yml)
ConfigFileApplicationListener  : Skipped missing config classpath:/application.yaml        

日志的加载顺序

从日志中可以看出日志的加载顺序

  • file > classpath
  • config > 当前目录.
  • Properties > xml > ym l> yaml

run的过程

SpringApplication.run的过程构建了ApplicationContext

public ConfigurableApplicationContext run(String... args) {
  // 耗时统计
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
        configureHeadlessProperty();
    // application监听
        SpringApplicationRunListeners listeners = getRunListeners(args);
        listeners.starting();
        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
      // 加载环境变量 配置文件
            ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
            configureIgnoreBeanInfo(environment);
            Banner printedBanner = printBanner(environment);
            context = createApplicationContext();
            exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
                    new Class[] { ConfigurableApplicationContext.class }, context);
      // 准备上下文
            prepareContext(context, environment, listeners, applicationArguments, printedBanner);
            // 刷新上下文
      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;
    }

AnnotationConfigServletWebServerApplicationContext 继承关系

AbstractApplicationContext (org.springframework.context.support)

|--GenericApplicationContext (org.springframework.context.support)

|--|--GenericWebApplicationContext (org.springframework.web.context.support)

|--|--|--ServletWebServerApplicationContext (org.springframework.boot.web.servlet.context)

|--|--|--|--AnnotationConfigServletWebServerApplicationContext (org.springframework.boot.web.servlet.context)

refreshBean 使用DefaultListableBeanFactory进行加载bean

public void refresh() throws BeansException, IllegalStateException {
        synchronized (this.startupShutdownMonitor) {
            // Prepare this context for refreshing.
            prepareRefresh();

            // Tell the subclass to refresh the internal bean factory.
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

            // Prepare the bean factory for use in this context.
            prepareBeanFactory(beanFactory);

            try {
                // Allows post-processing of the bean factory in context subclasses.
                postProcessBeanFactory(beanFactory);

                // Invoke factory processors registered as beans in the context.
                invokeBeanFactoryPostProcessors(beanFactory);

                // Register bean processors that intercept bean creation.
                registerBeanPostProcessors(beanFactory);

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

                // Initialize event multicaster for this context.
                initApplicationEventMulticaster();

                // Initialize other special beans in specific context subclasses.
                onRefresh();

                // Check for listener beans and register them.
                registerListeners();

                // Instantiate all remaining (non-lazy-init) singletons.
                finishBeanFactoryInitialization(beanFactory);

                // Last step: publish corresponding event.
                finishRefresh();
            }
            ...
            }

ApplicationListener与 ApplicationEvent

ApplicationEvent部分继承关系

ApplicationEvent (org.springframework.context)
|--SpringApplicationEvent (org.springframework.boot.context.event)
|--|--ApplicationEnvironmentPreparedEvent
|--|--ApplicationContextInitializedEvent (org.springframework.boot.context.event)
|--|--|--ApplicationEnvironmentPreparedEvent (org.springframework.boot.context.event)
|--|--|--ApplicationPreparedEvent (org.springframework.boot.context.event)
|--|--|--ApplicationStartedEvent (org.springframework.boot.context.event)
|--|--|--ApplicationReadyEvent (org.springframework.boot.context.event)
|--|--|--ApplicationFailedEvent (org.springframework.boot.context.event)
|--ApplicationContextEvent (org.springframework.context.event)
|--|--ContextClosedEvent (org.springframework.context.event)
|--|--ContextRefreshedEvent (org.springframework.context.event)
|--|--ContextStoppedEvent (org.springframework.context.event)
|--|--ContextStartedEvent (org.springframework.context.event)
|-- ...

SimpleApplicationEventMulticaster 用于管理 ApplicationListener与 ApplicationEvent,这有点像java swing中的事件处理,如鼠标点击发送一个事件,然后有一个listener处理这个事件。

multicastEvent用于触发listener与envent事件,最终调用listener的onApplicationEvent方法。
如 ConfigFileApplicationListener 处理 ApplicationEnvironmentPreparedEvent事件。
LoggingApplicationListener处理 ApplicationEnvironmentPreparedEvent事件。

整个启动过程listener 开始启动==>启动完成==>正在运行

listeners.starting();
...
listeners.started(context);
...
listeners.running(context);

listeners保存了已注册的SpringApplicationRunListener列表,listeners的处理即循环调用每个listener的处理。

SpringApplicationRunListener的实现类EventPublishingRunListener。

Listener通过SimpleApplicationEventMulticaster#multicastEvent广播事件,doInvokeListener调用事件处理,监听listener调用onApplicationEvent进行处理事件。

listeners.started(context) 的事件传播是通过AbstractApplicationContext#publishEvent进行Event加工及关联较早的事件,再通过SimpleApplicationEventMulticaster进行广播事件,同时事件向父类传播。

来看下整个启动过程的监听内容

public interface SpringApplicationRunListener {

 /**
  * 正在启动
  * Called immediately when the run method has first started. Can be used for very
  * early initialization.
  */
 default void starting() {
 }

 /**
  * 环境准备完成
  * Called once the environment has been prepared, but before the
  * {@link ApplicationContext} has been created.
  * @param environment the environment
  */
 default void environmentPrepared(ConfigurableEnvironment environment) {
 }

 /**
  * context准备完成
  * Called once the {@link ApplicationContext} has been created and prepared, but
  * before sources have been loaded.
  * @param context the application context
  */
 default void contextPrepared(ConfigurableApplicationContext context) {
 }

 /**
  * context加载完成
  * Called once the application context has been loaded but before it has been
  * refreshed.
  * @param context the application context
  */
 default void contextLoaded(ConfigurableApplicationContext context) {
 }

 /**
  * 启动完成
  * The context has been refreshed and the application has started but
  * {@link CommandLineRunner CommandLineRunners} and {@link ApplicationRunner
  * ApplicationRunners} have not been called.
  * @param context the application context.
  * @since 2.0.0
  */
 default void started(ConfigurableApplicationContext context) {
 }

 /**
  * 正在运行
  * Called immediately before the run method finishes, when the application context has
  * been refreshed and all {@link CommandLineRunner CommandLineRunners} and
  * {@link ApplicationRunner ApplicationRunners} have been called.
  * @param context the application context.
  * @since 2.0.0
  */
 default void running(ConfigurableApplicationContext context) {
 }

 /**
  * 失败
  * Called when a failure occurs when running the application.
  * @param context the application context or {@code null} if a failure occurred before
  * the context was created
  * @param exception the failure
  * @since 2.0.0
  */
 default void failed(ConfigurableApplicationContext context, Throwable exception) {
 }

}

总结

SpirngApplication 在启动时通过listener处理启动过程的一些事件event。加载上下文过程先获取环境变量配置文件,创建初始context ,prepareContext阶段将context、environment与listener绑定,处理后续的event,refreshContext阶段进行bean加载及postProcessors处理,自动装配加载,条件配置bean的加载,最终生成装配完bean的上下文ApplicationContext,最后发布程序启动完成、正在运行事件,并有listener进行处理,至此程序启动完成。

今天暂且写到这,有空继续研究。

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
12月前
|
XML Java 数据格式
SpringBoot的启动过程
启动详解 SpringBoot的启动分为两个部分: 构造SpringApplication 执行run方法 构造SpringApplication 我们先来整体看看: 加入我们当前启动类如下: 可以发现大致做了以下几件事: 设置BeanDefinition的主源 推断应用类型 设置ApplicationContext 初始化器 设置监听器 推断著主启动类 接下来我们详细的看看每一个步骤: 第一步:记录 BeanDefinition 源 大家知道我们的Spring容器刚开始内部的BeanFactory是空的,它要从各个源头去寻找BeanDefinition, 这些源有可能来自
88 1
|
Java
Springboot集成SpringCloud启动后Eureka报错
Springboot集成SpringCloud启动后Eureka报错
324 0
Springboot集成SpringCloud启动后Eureka报错
|
2月前
|
设计模式 缓存 Java
深入Spring Boot启动过程:揭秘设计模式与代码优化秘籍
深入Spring Boot启动过程:揭秘设计模式与代码优化秘籍
|
4月前
|
监控 Java Spring
深入理解Spring Boot的启动过程
深入理解Spring Boot的启动过程
|
5月前
|
XML Java 开发者
springboot 启动原理、启动过程、启动机制的介绍
【5月更文挑战第13天】Spring Boot 是一种基于 Java 的框架,用于创建独立的、生产级别的 Spring 应用程序。它的主要目标是简化 Spring 应用的初始搭建和开发过程,同时提供一系列大型项目常见的非功能性特征(如嵌入式服务器、安全性、度量、健康检查和外部化配置)。
276 3
|
Java
如何在SpringBoot启动过程中,进行自定义操作?
如何在SpringBoot启动过程中,进行自定义操作?
63 0
|
5月前
|
Java 应用服务中间件 微服务
springboot详细启动过程
springboot详细启动过程
|
搜索推荐 Java 应用服务中间件
SpringBoot(一):springboot应用程序启动过程核心分析
说起**springboot**大家很容易想到的就是**自动装配**、**约定大于配置**这个特点,的确这是springboot相比较于普通的**spring** web项目最大的亮点。
88 1
|
Java 测试技术 容器
全网最详细的介绍SpringBoot启动过程源码分析
上一篇我们介绍了SpringBoot的自动装配的知识,这一篇我们将介绍SpringBoot最核心的知识点,SpringBoot应用的启动过程。这个启动过程比较复杂,在此我只介绍核心的知识点。其启动过程大概分为两步。1. 初始化SpringApplication对象,2.执行SpringApplication对象的run方法。
147 0
全网最详细的介绍SpringBoot启动过程源码分析
下一篇
无影云桌面