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进行处理,至此程序启动完成。

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

上一篇:攀登规模化的高峰 - 蚂蚁集团大规模 Sigma 集群 ApiServer 优化实践


下一篇:C#窗体控件更新(一)