深入SpringBoot源码(四)初识Environment

  • new DefaultApplicationArguments
  • prepareEnvironment
    • getOrCreateEnvironment()
    • ConfigurableEnvironment
    • Environment

new DefaultApplicationArguments


ApplicationArguments提供对用于运行SpringApplication的参数的访问。

public interface ApplicationArguments {/*** 返回传递给应用程序的原始未处理参数。*/String[] getSourceArgs();/*** 返回所有选项参数的名称。例如,如果参数是 "--foo=bar --debug" 将返回值["foo", "debug"] 。*/Set<String> getOptionNames();/*** 返回从参数解析的选项参数集是否包含具有给定名称的选项。* @param name 要检查的名称*/boolean containsOption(String name);/*** 返回与具有给定名称的参数选项关联的值的集合* @param name 选项的名称*/List<String> getOptionValues(String name);/*** 返回已解析的非选项参数的集合。*/List<String> getNonOptionArgs();}

DefaultApplicationArguments是ApplicationArguments的默认实现。

prepareEnvironment

 private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,DefaultBootstrapContext bootstrapContext, ApplicationArguments applicationArguments) {// Create and configure the environmentConfigurableEnvironment environment = getOrCreateEnvironment();configureEnvironment(environment, applicationArguments.getSourceArgs());ConfigurationPropertySources.attach(environment);listeners.environmentPrepared(bootstrapContext, environment);DefaultPropertiesPropertySource.moveToEnd(environment);Assert.state(!environment.containsProperty("spring.main.environment-prefix"),"Environment prefix cannot be set via properties.");bindToSpringApplication(environment);if (!this.isCustomEnvironment) {environment = convertEnvironment(environment);}ConfigurationPropertySources.attach(environment);return environment;}

run方法调用prepareEnvironment传入的参数内容如下:

getOrCreateEnvironment()

 private ConfigurableEnvironment getOrCreateEnvironment() {if (this.environment != null) {return this.environment;}switch (this.webApplicationType) {case SERVLET:return new ApplicationServletEnvironment();case REACTIVE:return new ApplicationReactiveWebEnvironment();default:return new ApplicationEnvironment();}}

ConfigurableEnvironment

ConfigurableEnvironment是大多数Environment类都将实现的配置接口。提供用于设置活动和默认配置文件以及操作基础属性源的工具。允许客户端通过ConfigurablePropertyResolver超级接口设置和验证所需属性、自定义转换服务等。Environment表示当前应用程序运行环境的接口,对应用程序环境的两个关键方面进行建模:配置文件和属性。

public interface ConfigurableEnvironment extends Environment, ConfigurablePropertyResolver {/*** 指定为此Environment活动的配置文件集。在容器引导期间评估配置文件以确定是否应向容器注册bean定义。*/void setActiveProfiles(String... profiles);/*** 将配置文件添加到当前的活动配置文件集中。*/void addActiveProfile(String profile);/*** 如果没有其他配置文件通过setActiveProfiles显式激活,则指定默认激活的配置文件集。*/void setDefaultProfiles(String... profiles);/*** 以可变形式返回此Environment的PropertySources ,允许操作在针对此Environment对象解析属性时应搜索的PropertySource对象集。* 各种MutablePropertySources方法(例如addFirst 、 addLast 、 addBefore和addAfter允许对属性源排序进行细粒度控制。*/MutablePropertySources getPropertySources();/*** 如果当前SecurityManager允许,则返回System.getProperties()的值,否则返回一个映射实现,该实现将尝试使用对System.getProperty(String)的调用来访问各个键。*/Map<String, Object> getSystemProperties();/*** 如果当前SecurityManager允许,则返回System.getenv()的值,否则返回一个映射实现,该实现将尝试使用对System.getenv(String)的调用来访问各个键。*/Map<String, Object> getSystemEnvironment();/*** 将给定父环境的活动配置文件、默认配置文件和属性源附加到此(子)环境各自的集合中。*/void merge(ConfigurableEnvironment parent);}

环境对象的配置必须通过ConfigurableEnvironment接口完成,该接口从所有AbstractApplicationContext子类getEnvironment()方法返回。

Environment

public interface Environment extends PropertyResolver {/*** 返回为此环境显式激活的配置文件集。配置文件用于创建要按条件注册的 bean 定义的逻辑分组,例如基于部署环境。* 可以通过将“spring.profiles.active”设置为系统属性或调用ConfigurableEnvironment.setActiveProfiles(String...)来激活配置文件。* 如果没有将配置文件明确指定为活动,则任何默认配置文件都将自动激活。*/String[] getActiveProfiles();/*** 当没有明确设置活动配置文件时,默认情况下将配置文件集返回为活动状态。*/String[] getDefaultProfiles();/*** @deprecated as of 5.1 in favor of {@link #acceptsProfiles(Profiles)}*/@Deprecated boolean acceptsProfiles(String... profiles);/*** 返回活动配置文件是否与给定的Profiles谓词匹配。*/boolean acceptsProfiles(Profiles profiles);}


SpringApplication的getOrCreateEnvironment方法会根据成员字段webApplicationType生成合适的ConfigurableEnvironment,例如我引入了spring-boot-starter-webflux依赖,所以这里选择了ApplicationReactiveWebEnvironment

深入SpringBoot源码(四)初识Environment相关推荐

  1. SpringBoot源码笔记分析

    SpringBoot源码笔记分析 1.基础 1.配置SpringBoot热部署 1.引入依赖 <dependency><groupId>org.springframework. ...

  2. 阅读react-redux源码(四) - connectAdvanced、wrapWithConnect、ConnectFunction和checkForUpdates

    阅读react-redux源码 - 零 阅读react-redux源码 - 一 阅读react-redux源码(二) - createConnect.match函数的实现 阅读react-redux源 ...

  3. Springboot源码分析第一弹 - 自动装配实现

    Springboot就不用多了吧,解放Java开发双手的神器. 最显著的特点就是,去配置化,自动装配,自动配置.让开发人员只需要注重业务的开发 今天就来了解一下自动装配的源码是怎么实现的 预先准备 直 ...

  4. SpringBoot源码分析之内置Servlet容器

    原文链接:http://fangjian0423.github.io/2017/05/22/springboot-embedded-servlet-container/ SpringBoot内置了Se ...

  5. SpringBoot源码核心源码讲解

    SpringBoot源码主线分析   我们要分析一个框架的源码不可能通过一篇文章就搞定的,本文我们就来分析下SpringBoot源码中的主线流程.先掌握SpringBoot项目启动的核心操作,然后我们 ...

  6. spring-boot-2.0.3不一样系列之源码篇 - springboot源码一,绝对有值得你看的地方

    前言 上篇:spring-boot-2.0.3不一样系列之shiro - 搭建篇,实现了spring-boot与shiro的整合,效果大家也看到了,工程确实集成了shiro的认证与授权功能.如果大家能 ...

  7. springboot源码分析

    快速开发底层原理 SpringBoot核心理念 能够实现帮助开发者快速的整合第三方框架(Spring.Mybatis.hibernate) 原理:Maven依赖封装整合和自定义starter. 完全去 ...

  8. springboot源码解析-管中窥豹系列之BeanFactoryPostProcessor(十一)

    一.前言 Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去. 我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot ...

  9. SpringBoot源码初学者(二):SpringBoot事件监听器

    ps:真正适合阅读源码的新手来看的SpringBoot源码讲解,如果你真的想读懂SpringBoot源码,可以按照以下推荐的方式来阅读文章 打开ide,打开SpringBoot源码,跟着文章一起写注释 ...

最新文章

  1. LeetCode简单题之按奇偶排序数组 II
  2. python网络通信的几种方式_两种方式,java=python,使用py4j进行通信
  3. MySQL笔记1:考察内链接、左连接、右连接。
  4. [转]淘宝下单高并发解决方案
  5. 别说我懂社交网络: 关于社交网络分析的一头雾水
  6. 一些机器学习数据集(Dataset)
  7. 训练时发生的错误:Couldn‘t open shared file mapping: <000001910A228862>, error code: <1455>
  8. viso怎么控制连接线_什么是节气门?多久清洗一次?怎么清洗?
  9. 红帽干掉 CentOS 8,CentOS Stream 上位
  10. 安装dhcp服务方法(系统为linux 7.0)
  11. coreldraw x4如何出血_CorelDRAW软件出血位详解
  12. 常见mysql优化 面试题
  13. 今日头条php笔试题,今日头条笔试题回顾及个人答案参考
  14. ios、iphone越狱获取系统文件权限
  15. word表格怎么缩小上下间距_word表格间距 在WORD表格中如何将行间距缩小
  16. ROS-Navigation之map_server笔记及程序解析
  17. 2019吉林省赛东北四省赛总结
  18. 【学习笔记】前端开发面试锦集
  19. 【内网安全-通讯上线】通讯上线基础知识
  20. 无限级分类之迭代查找家谱树

热门文章

  1. Apache之AllowOverride参数详解
  2. windows下编译mingw版本的glew库
  3. SOLIDWORKS如何打开丢失零部件的装配体
  4. 使用 DiskGenius 执行硬盘分区时提示”格式化时出现错误“怎么解决
  5. 用计算机打女生节快乐,2020年班级女生节快乐的祝福语
  6. 如何组建数据治理团队
  7. 沉船会有什么_我的世界:七座独特风格的沉船水屋,房屋虽简约,但容易学会...
  8. 1688商品api、商品详情接口、采集商品信息
  9. 网上收集的爆笑笑话 汗一个
  10. 航海日记手游如何在电脑上玩 航海日记模拟器玩法教程