前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot源码 | prepareEnvironment方法解析

SpringBoot源码 | prepareEnvironment方法解析

作者头像
六月的雨在Tencent
发布2024-03-29 08:56:52
780
发布2024-03-29 08:56:52
举报
文章被收录于专栏:CSDNCSDN
SpringBoot源码 | prepareEnvironment方法解析

SpringBoot

在SpringBoot启动流程中,主要的两个阶段是初始化SpringApplication对象以及SpringApplication.run方法执行的内容,今天主要细讲的是SpringApplication.run中的准备环境的prepareEnvironment方法源码ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments)。prepareEnvironment的源码如下

prepareEnvironment源码

prepareEnvironment主要包含方法getOrCreateEnvironment、configureEnvironment、ConfigurationPropertySources.attach、DefaultPropertiesPropertySource.moveToEnd、bindToSpringApplication,这里我们先看一下对应的注释

代码语言:javascript
复制
private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
        DefaultBootstrapContext bootstrapContext, ApplicationArguments applicationArguments) {
    // 创建和配置环境
    ConfigurableEnvironment environment = getOrCreateEnvironment();
    //配置 property sources 和 profiles
    configureEnvironment(environment, applicationArguments.getSourceArgs());
    //将environment.getPropertySources()放在第一个位置
    ConfigurationPropertySources.attach(environment);
    //运行监听器通知所有监听器环境准备完成
    listeners.environmentPrepared(bootstrapContext, environment);
    //Move the 'defaultProperties' property source so that it's the last source
    DefaultPropertiesPropertySource.moveToEnd(environment);
    Assert.state(!environment.containsProperty("spring.main.environment-prefix"),
            "Environment prefix cannot be set via properties.");
    //Bind the environment to the {@link SpringApplication}
    bindToSpringApplication(environment);
    //环境转换成StandardEnvironment
    if (!this.isCustomEnvironment) {
        environment = convertEnvironment(environment);
    }
    ConfigurationPropertySources.attach(environment);
    //返回环境配置对象ConfigurableEnvironment
    return environment;
}

getOrCreateEnvironment

官方注释Create and configure the environment 创建和配置环境,由于我们的是SERVLET类型,所以这里会返回ApplicationServletEnvironment对象

configureEnvironment

configureEnvironment方法主要是配置property sources or profiles,源码可以看到

configurePropertySources

继续跟进configurePropertySources方法可以看到官方注释Add, remove or re-order any {@link PropertySource}s in this application’s environment意为在此应用程序环境中添加、删除或重新排序任何{@link PropertySource},源码如下

其中方法environment.getPropertySources()主要是为了获取ApplicationServletEnvironment中的PropertySources,获取之后会判断Map defaultProperties;是否不为null,不为null则执行addOrMerge判断是需要新增或者合并DefaultPropertiesPropertySource

回到configurePropertySources方法向下执行,

这里的args是the application arguments (usually passed from a Java main method)从run方法带下来的参数,这里判断args的长度是否大于0,满足条件后判断当前sources中是否包含命令行参数,包含的话就需要用args中参数替换sources中对应name的参数,如果不包含则直接把args放在首位,

configureProfiles

我们再来看configureProfiles方法,可以看到

官方注释Configure which profiles are active (or active by default) for this application environment. Additional profiles may be activated during configuration file processing via the {@code spring.profiles.active} property.的意思也就是说在配置文件处理过程中,可以通过{@code-spring.profiles.active}属性激活其他配置文件。 具体的实现去查看ConfigFileApplicationListener类By default properties will be loaded from ‘application.properties’ and/or ‘application.yml’ files in the following locations属性内容会从application.properties或者application.yml加载,继续向下

ConfigurationPropertySources.attach

ConfigurationPropertySources.attach方法

主要是为了将ConfigurationPropertySource附加到指定Environment,遍历Environment管理的每个PropertySource到ConfigurationPropertySource同时允许PropertySourcesPropertyResolver调用使用ConfigurationPropertyName配置的属性名,同时放在首位,之后listeners.environmentPrepared完成环境准备

DefaultPropertiesPropertySource.moveToEnd

之后调用DefaultPropertiesPropertySource.moveToEnd将Move the ‘defaultProperties’ property source so that it’s the last source移动到source末尾

bindToSpringApplication

环境参数配置完成之后执行bindToSpringApplication将环境配置参数绑定到SpringApplication

convertEnvironment

如果isCustomEnvironment为false则将ConfigurableEnvironment 转换为application environment并且不直接解析配置文件属性的应用程序环境

最后再执行一次ConfigurationPropertySources.attach,完成之后返回环境配置对象ConfigurableEnvironment,至此prepareEnvironment方法执行完成。

总结

执行完成准备环境的prepareEnvironment方法之后,会继续执行容器上下文启动前的其他准备工作,后续继续研究。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-03-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SpringBoot源码 | prepareEnvironment方法解析
  • SpringBoot
    • prepareEnvironment源码
      • getOrCreateEnvironment
        • configureEnvironment
          • configurePropertySources
          • configureProfiles
        • ConfigurationPropertySources.attach
          • DefaultPropertiesPropertySource.moveToEnd
            • bindToSpringApplication
              • convertEnvironment
              • 总结
              相关产品与服务
              容器服务
              腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档