SpringBoot学习笔记

Springboot启动流程:

0.判断springboot项目的属性

在SpringApplication类中有一个私有变量: webApplicationType
该属性决定了当前springboot项目是否是一个web项目:

private WebApplicationType deduceWebApplicationType() {
        if (ClassUtils.isPresent(REACTIVE_WEB_ENVIRONMENT_CLASS, null)
                && !ClassUtils.isPresent(MVC_WEB_ENVIRONMENT_CLASS, null)) {
            return WebApplicationType.REACTIVE;
        }
        for (String className : WEB_ENVIRONMENT_CLASSES) {
            if (!ClassUtils.isPresent(className, null)) {
                return WebApplicationType.NONE;
            }
        }
        return WebApplicationType.SERVLET;
}
1.加载ApplicationContextInitializer接口的实现类,具体的实现类在spring.factory中定义:

其中spring-boot-2.0.3.RELEASE.jar定义的有以下四个:

org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,
org.springframework.boot.context.ContextIdApplicationContextInitializer,
org.springframework.boot.context.config.DelegatingApplicationContextInitializer,
org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer

spring-boot-autoconfigure-2.0.3.RELEASE.jar定义的有以下两个:

org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,
org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener

在SpringApplication中使用反射实例化并缓存起来

2.加载ApplicationListener接口的实现类,具体实现类也是在spring.factory中定义:

其中spring-boot-2.0.3.RELEASE.jar定义的有以下九个:

org.springframework.boot.ClearCachesApplicationListener,
org.springframework.boot.builder.ParentContextCloserApplicationListener,
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.context.logging.ClasspathLoggingApplicationListener,
org.springframework.boot.context.logging.LoggingApplicationListener,
org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener

具体的工作流程参考此图:


springboot启动流程图

你可能感兴趣的:(SpringBoot学习笔记)