二十. 准备环境信息

准备环境信息调用的是SpringApplication的prepareEnvironment方法。该方法的定义如下:

private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,

      DefaultBootstrapContext bootstrapContext, ApplicationArguments applicationArguments) {

// 创建环境信息

  ConfigurableEnvironment environment = getOrCreateEnvironment();

// 配置环境信息

  configureEnvironment(environment, applicationArguments.getSourceArgs());

// 将ConfigurationPropertySource支持附加到创建和配置好了的环境中环境中

  ConfigurationPropertySources.attach(environment);

// 向所有的run方法的侦听者发布环境已经准备好了但还没有创建ApplicationContext

  listeners.environmentPrepared(bootstrapContext, environment);

//  移动“defaultProperties”属性源,使其成为给定ConfigurableEnvironment中的最后一个属性源

  DefaultPropertiesPropertySource.moveToEnd(environment);

  Assert.state(!environment.containsProperty("spring.main.environment-prefix"),

        "Environment prefix cannot be set via properties.");

//  将环境绑定到 SpringApplication中

  bindToSpringApplication(environment);

// 判断该环境是否为自定义环境,默认为非自定义环境

  if (!this.isCustomEnvironment) {

// 为非指定环境

    // 将给定环境转换为不尝试直接解析配置文件属性的应用程序环境。

      environment = convertEnvironment(environment);

  }

// 将ConfigurationPropertySource支持附加到创建和配置好了的环境中环境中

  ConfigurationPropertySources.attach(environment);

  return environment;

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容