专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

SpringBoot2.2.6启动run方法之Environment

前言

此系列是针对springboot的启动,旨在于和大家一起来看看springboot启动的过程中到底做了一些什么事。文中有不清楚或错误的地方 欢迎留言指正。

源码解读进度

首先我们的源码阅读进度

public ConfigurableApplicationContext run(String... args) {
    // 用于记录启动时间
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    // 声明Spring上下文
    ConfigurableApplicationContext context = null;
    // 声明启动错误回掉
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
    // 设置jdk系统属性java.awt.headless,默认情况为true即开启
    configureHeadlessProperty();
    // 装饰者模式创建启动监听器(EventPublishingRunListener实例)
    SpringApplicationRunListeners listeners = getRunListeners(args);
    // 触发ApplicationStartingEvent事件,包括转换器的初始化
    listeners.starting();
    try {
        // 参数封装,也就是在命令行下启动应用带的参数
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
        // 准备环境:1、加载外部化配置的资源到environment;2、触发ApplicationEnvironmentPreparedEvent事件
        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;
}

先从加载方法说起

private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
            ApplicationArguments applicationArguments) {
    // 1. 根据webApplicationType类型,创建对应的Environment, 
    // 本文基于web环境分析,加载的是StandardServletEnvironment
    ConfigurableEnvironment environment = getOrCreateEnvironment();
    // 2. 配置Environment,一、设置ConversionService,二、配置数据源、包括命令行参数,三、配置profiles
    configureEnvironment(environment, applicationArguments.getSourceArgs());
    // 3. 配置configurationProperties
    ConfigurationPropertySources.attach(environment);
    // 4. 发送ApplicationEnvironmentPreparedEvent事件,接收事件后环境变量的处理。下面详细分析
    listeners.environmentPrepared(environment);
    bindToSpringApplication(environment);
    if (!this.isCustomEnvironment) {
        environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,
                deduceEnvironmentClass());
    }
    ConfigurationPropertySources.attach(environment);
    return environment;
}

listeners.environmentPrepared(environment)做了那些事情

  • ConfigFileApplicationListener接收到ApplicationEnvironmentPreparedEvent事件
private void onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
    // 加载后置处理器
    List<EnvironmentPostProcessor> postProcessors = loadPostProcessors();
    // 同时自己也是一个后置处理器
    postProcessors.add(this);
    // 排序
    AnnotationAwareOrderComparator.sort(postProcessors);
    // 分别调用后置处理器方法
    for (EnvironmentPostProcessor postProcessor : postProcessors) {
        postProcessor.postProcessEnvironment(event.getEnvironment(), event.getSpringApplication());
    }
}

后置处理器以及排序如下:

1、 SystemEnvironmentPropertySourceEnvironmentPostProcessor 系统变量处理
2、 SpringApplicationJsonEnvironmentPostProcessor json格式环境变量处理
3、 CloudFoundryVcapEnvironmentPostProcessor spring-cloud环境用到
4、 ConfigFileApplicationListener 初始化配置文件
5、 DebugAgentEnvironmentPostProcessor

文章永久链接:https://tech.souyunku.com/27198

未经允许不得转载:搜云库技术团队 » SpringBoot2.2.6启动run方法之Environment

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们