SpringBoot启动过程图解

概要

最近看了一下SpringBoot的启动过程源码,做了一个粗略的图解,在此记录下。


启动流程图

SpringBoot启动流程图


相关方法

  • loadSpringFactories(@Nullable ClassLoader classLoader)方法
    • 该方法用于加载classpath下所有的META-INF/spring.factories文件,获取里面配置的所有Spring相关的各种类信息(例如系统listener、initializer、loader、analyzer等 );
    • 它夹在时会通过 ClassLoader类的getResources(“META-INF/spring.factories”)方法来获取存放有spring.factories文件的所有url,然后通过url来获取每个spring.factories文件中的配置;
    • 只有第一次调用该方法时才会从磁盘上加载,之后每次使用都在一个set集合中;
    • 它第一次调用是在initialize()方法中调用。
  • initialize(Object[] sources)方法
    • 设置webEnvironment;
    • 获取ApplicationContextInitializer接口的实现类实例列表,存储在initializers中;(通过etSpringFactoriesInstances方法);
    • 获取ApplicationListener接口的实现类实例列表,存储在listeners中;(通过etSpringFactoriesInstances方法)
    • 设置main方法所在类的Class对象;
  • private Collection< ? extends T> getSpringFactoriesInstances(Class type, Class< ?>[] parameterTypes, Object… args)方法
    • 从META-INF/spring.factories文件中获取指定类型的子类的实例列表,通过AnnotationAwareOrderComparator.sort排序后返回;
  • configurePropertySources(ConfigurableEnvironment environment,tring[] args) 方法
    • 配置属性源到ConfigurableEnvironment中;
    • 首先把默认的属性map加进去(为空则不加,通常为空),然后把启动参数args加进去(为空则不加);
    • 若两者都为空,则不做任何操作,直接返回;
  • configureProfiles(ConfigurableEnvironment environment, String[] args)方法
    • 配置profile到environment中,主要是配置“spring.profiles.active”指定的profile,但此时还未加载yml配置文件,通常该方法第一次调用并未配置任何profile;
    • 配置附加的profile到enviroment中,但通常为空。该附加的profile变量为additionalProfiles,是一个HashSet;
  • configureEnvironment()方法
    • 调用configureProfiles和configurePropertySources配置environment的属性和profile;
  • getOrCreateEnvironment()方法
    • 根据webEnvironment的值创建相应的environment;
    • 若webEnvironment为true,则创建StandardServletEnvironment;
    • 若webEnvironment为false,则创建StandardEnvironment;
  • prepareEnvironment()方法
    • 调用getOrCreateEnvironment()获取environment实例;
    • 调用configureEnvironment配置environment;
    • 监听器调用environmentPrepared方法;
    • 若webEnvironment为false,把environment转换为StandardServletEnvironment;
  • private Banner getBanner(Environment environment, Banner definedBanner)方法,在SpringApplicationBannerPrinter类中
    • 获取要打印的banner(标语,横幅);
    • 先获取图片的banner(若存在),然后获取文本的banner(若存在)。若两者都没有,则返回默认的banner “DEFAULT_BANNER”,该banner就是我们平时看到的Spring的标示;
    • banner.image.location,要打印的图片banner位置的key,值我们自己配置;
    • banner.location,要打印的文本文件名称的key(默认值为:banner.txt,我们可以在resource目录下创建banner.txt文件,然后写入我们要打印的内容);
  • public Banner print(Environment environment, Class< ?> sourceClass, PrintStream out) ,在SpringApplicationBannerPrinter类中
    • 调用getBanner,获取要打印的banner;
    • 调用printBanner,打印;
    • 返回PrintedBanner;
  • private Banner printBanner(ConfigurableEnvironment environment)
    • 判断banner的模式是否开启;
    • 创建resourceLoader实例、SpringApplicationBannerPrinter实例;
    • 调用SpringApplicationBannerPrinter的print方法,该方法会先调用getBanner获取要打印的banner,然后再进行打印,并返回一个PrintedBanner方法;
  • protected void customizePropertySources(MutablePropertySources propertySources),StandardServletEnvironment类
    • 添加属性源servletContextInitParams、servletConfigInitParams、jndiProperties(如果是jndi环境);
    • 第一次调用时添加的三个属性源均为空;
  • public static boolean isPresent(String className, ClassLoader classLoader),ClassUtils类
    • 确定由提供的名称标识的{@link Class}是否存在且可以加载。 如果类或其中一个依赖项不存在或无法加载,将返回{@code false};
  • public static Class< ?> resolveTypeArgument(Class< ?> clazz, Class< ?> genericIfc),GenericTypeResolver
    • 返回类clazz实现的接口genericIfc中指定的泛型类型;
    • 例如:public class DelegatingApplicationContextInitializer implements ApplicationContextInitializer, Ordered{}
      传入的参数为resolveTypeArgument(DelegatingApplicationContextInitializer,ApplicationContextInitializer),则返回ConfigurableApplicationContext

相关类

  • StopWatch类
    • 简单的秒表,允许多个任务的计时,暴露每个命名任务的总运行时间和运行时间。
  • FailureAnalyzer接口
    • {@code FailureAnalyzer}用于分析故障并提供可以向用户显示的诊断信息。
  • SimpleCommandLinePropertySource
    • 此{@code CommandLinePropertySource}实现旨在提供解析命令行参数的最简单方法。 与所有{@code CommandLinePropertySource}实现一样,命令行参数分为两个不同的组:选项参数和非选项参数,如下所述(某些 从Javadoc复制到{@link SimpleCommandLineArgsParser}的部分: 使用选项参数选项参数必须符合确切的语法:optName [= optValue] 也就是说,选项必须以“{@为前缀” code - }“,可能会也可能不会指定值。 如果指定了值,则必须将名称和值分隔为,不带空格等号(“=”)。
  • abstract class PropertySource< T >
    • 表示名称/值属性对的源的抽象基类。 底层{@linkplain #getSource()源对象}可以是封装属性的任何类型{@code T}。 示例包括{@link java.util.Properties}对象,{@link java.util.Map}对象,{@code ServletContext}和{@code ServletConfig}对象(用于访问init参数)。 浏览{@code PropertySource}类型层次结构以查看提供的实现;
    • 封装key/value形式的属性源,注意是属性源,不是属性本身。属性源可以是{@link java.util.Properties}对象,{@link java.util.Map}对象,{@code ServletContext}和{@code ServletConfig}对象;
  • public class MutablePropertySources implements PropertySources
    • {@link PropertySources}接口的默认实现。 允许操作包含的属性源,并提供用于复制现有{@code PropertySources}实例的构造函数
  • public interface ResourceLoader
    • 用于加载资源的策略接口(例如,类路径或文件系统资源)。 需要{@link org.springframework.context.ApplicationContext}来提供此功能,以及扩展的{@link org.springframework.core.io.support.ResourcePatternResolver}支持。
    • 使用特定上下文的资源加载策略,在ApplicationContext中运行时,可以从Strings填充Resource和Resource数组类型的bean属性。
  • public interface Resource extends InputStreamSource
    • 用于从实际类型的底层资源(例如文件或类路径资源)中抽象出来的资源描述符的接口;
    • 如果InputStream以物理形式存在,则可以为每个资源打开它,但只能为某些资源返回URL或File句柄。 实际行为是特定于实现的。
  • public class DefaultResourceLoader implements ResourceLoader
    • 如果位置值是URL,则返回{@link UrlResource};如果是非URL路径或“classpath:”伪URL,则返回{@link ClassPathResource}
  • public class MutablePropertySources implements PropertySources
    • {@link PropertySources}接口的默认实现。 允许操作包含的属性源,并提供用于复制现有{@code PropertySources}实例的构造函数。
  • private static class PrintedBanner implements Banner
    • 装饰器,允许再次打印{@link Banner}而无需指定源类。
  • public class BootstrapApplicationListener implements ApplicationListener, Ordered{}
    • 通过在单独的引导上下文中委托{@link ApplicationContextInitializer} bean来准备SpringApplication(例如填充其环境)的侦听器。 引导上下文是一个SpringApplication,它是从spring.factories中定义的源{@link BootstrapConfiguration}创建的,并使用“bootstrap.properties”(或yml)中的外部配置进行初始化,而不是正常的“application.properties”。
    • 该listner会读取bootstrap.yml文件中的基本配置(不会读取所有的配置),会读取profile,还会做一些其他的初始化操作;

猜你喜欢

转载自blog.csdn.net/Growing_stu/article/details/82585817