运行源码分析:初始化ApplicationArguments

初始化ApplicationArguments

监 听 器 启 动 之 后 , 紧接着便是执行ApplicationArguments对象的初始化 ,Application-Arguments 是用于提供访问运行 SpringApplication 时的参数。

ApplicationArguments 的 初 始 化 过 程 非 常 简 单 , 只 是 调 用 了 它 的 实 现 类

Default-ApplicationArguments 并传入 main 方法中的 args 参数。

ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);

DefaultApplicationArguments 中将参数 args 封装为 Source 对象,Source 对象是基于Spring 框架的 SimpleCommandLinePropertySource 来实现的。

我们对该接口在此不进行拓展,只需知道通过 main 方法传递进来的参数被封装成ApplicationArguments 对象即可。关于该接口实例化的步骤我会在后续关于 Spring Boot 的参数的章节中进行详细讲解,因此在图 4-1 所示的核心流程图中也没有体现出来。

初始化 ConfigurableEnvironment

完成 ApplicationArguments 参数的准备之后,便开始通过 prepareEnvironment 方法对ConfigurableEnvironment 对象进行初始化操作。

ConfigurableEnvironment接口继承自Environment接口和

ConfigurablePropertyResolver,最终都继承自接口 PropertyResolver。

ConfigurableEnvironment 接口的主要作用是提供当前运行环境的公开接口,比如配置文件profiles 各类系统属性和变量的设置、添加、读取、合并等功能。

通过 ConfigurableEnvironment 接口中方法定义,可以更清楚地了解它的功能,代码如下。

publicinterfaceConfigurableEnvironmentextendsEnvironment,Configurable-PropertyResolver{设置激活的组集合voidsetActiveProfiles(String. .. profiles);向当前激活的组集合中添加一个 profile 组voidaddActiveProfile(String profile);设置默认激活的组集合。激活的组集合为空时会使用默认的组集合voidsetDefaultProfiles(String... profiles);//获取当前环境对象中的属性源集合,也就 是应用环境变量//属性源集合其实就是滚动鼠标轴或单击,开始截长图//该方法提供了直接配置属性源的MutablePropertySourcesgetPropertySources();//获取虚拟机环境变量,该方法提供 J 直接配置虚拟机环境变量的入口Map getSystemProperties();//获取操作系统环境变量//该方法提供了直接配置系统环境变量的入口MapgetSystemEnvironment();合并指定环境中的配置到当前环境中voidmerge(ConfigurableEnvironment parent);}

通过接口提供的方法,我们可以看出 ConfigurableEnvironment 就是围绕着这个“环境”来提供相应的功能,这也是为什么我们也将它称作“环境”。

了解了 ConfigurableEnvironment 的功能及方法,我们回归到 SpringApplication 的流程看相关源代码。run 方 法中调用 prepareEnvironment 方法相关代码如下。

publicConfigurableApplicationContextrun(String... args){//加载属性配置,包括所有的配 置属性(如: application. properties 中和外部的属性配置)ConfigurableEnvironment environment = prepareEnvironment( listeners,applicationArguments);}

prepareEnvironment 方法的源代码实现如下。

privateConfigurableEnvironmentprepareEnvironment(SpringApplicationRunListeners listeners, ApplicationArguments application

Arguments){//获取或创建环境ConfigurableEnvironment environment = getOrCreateEnvironment();//配置环境, 主要包括 PropertySources lactiveProfiles 的配置configureEnvironment (environment, applicationArguments . getSourceArgs());//将 ConfigurationPropertySources 附加到指定环境中的第一位, 并动态跟踪环境的添加或删除ConfigurationPropertySources . attach( environment);// listener 环境准备(之前章节已经提到)listeners. environmentPrepared environment);//将环境绑定到 SpringApplicationbindToSpringApplication( environment);//判断是否定制的环境,如果不是定制的则将环境转换为 StandardEnvironmentif(!this. isCustomEnvironment) {environment =newEnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment, deduceEnvironment -

Class());//将 ConfigurationPropertySources 附加到指定环境中的第一位, 并动态跟踪环境的添加或删除ConfigurationPropertySources . attach( environment);returnenvironment;}

通过以上代码及注解可知,prepareEnvironment 进行 了以下的操作。

.获取或创建环境。

.配置环境。

:ConfigurationPropertySources 附加到指定环境:将 ConfigurationPropertySources 附加到指定环境中的第一位,并动态跟踪环境的添加或删除(当 前版本新增了该行代码,与最后一步操作相同)。

.设置 listener 监听事件:前面章节已经讲过,此处主要针对准备环境的监听。

.绑定环境到 SpringApplication:将环境绑定到 name 为“spring.main”的目标上。

.转换环境:判断是否是定制的环境,如果不是定制的,则将环境转换为Standard-Environment。此时判断条件 isCustomEnvironment 默认为 false,在后面的操作中会将其设置为 true,如果为 true 则不再会进行此转换操作。

:ConfigurationPropertySources 附加到指定环境:将 ConfigurationPropertySources 附加到指定环境中的第一位,并动态跟踪环境的添加或删除操作。

下面针对以上步骤挑选部分代码进行相应的讲解。

获取或创建环境

SpringApplication 类中通过 getOrCreateEnvironment 方法来获取或创建环境。在该方法中首先判断环境是否为 null,如果不为 null 则直接返回;如果为 null,则根据前面推断出来的WebApplicationType 类型来创建指定的环境,代码如下。

privateConf igurableEnvironmentgetOrCreateEnvironment(){if(this. environment !=null) {returnthis. environment;//根据不同的应用类型, 创建不同的环境实现switch(this. webApplicationType) {caseSERVLET:returnnewStandardServletEnvironment();caseREACTIVE:returnnewStandardReactiveWebEnvironment();default:returnnewStandardEnvironment();}

. 上面方法中如果 environment 存在, 则直接返回;如果 environment 不存在,则根据前面步骤中推断获得的 WebApplicationType 来进行区分创建环境。如果是 SERVLET 项目则创建标准的 Servlet 环境

StandardServletEnvironment;

如果是 REACTIVE 项目则创建

StandardReactiveWebEnvironment;其他情况则创建标准的非 Web 的 StandardEnvironment。

配置环境

在获得环境变量对象之后,开始对环境变量和参数进行相应的设置,主要包括转换服务的设置、PropertySources 的设置和 activeProfiles 的设置。

SpringApplication 类中相关 configureEnvironment 方法代码如下。

protectedvoidconfigureEnvironment(ConfigurableEnvironment environment,

String[] args){//如果为 true 则获取并设置转换服务f (this. addConversionService) {ConversionService conversionService = ApplicationConversionService. getSharedInstance();environment . setConversionService( (ConfigurableConvers ionService) conversion-Service);//配置 PropertySourcesconfigurePropertySources( environment, args);//配置 ProfilesconfigureProfiles(environment, args);}

在以上代码中,首先判断 addConversionService 变量是否为 true, 也就是判断是否需要添加转换服务,如果需要,则获取转换服务实例,并对环境设置转换服务。随后进行PropertySources 和 Profiles 的配置。

其中 configurePropertySources 方法对 PropertySources 进行配置,代码如下。

protectedvoidconfigurePropertySources(ConfigurableEnvironment environmenString[] args){//获得环境中的属性资源信息MutablePropertySources sources = environment . getPropertySources();// 如果默认属性配置存在则将其放置于属性资源的最后位置if(this. defaultProperties !=null&& !this. defaultProperties . isEmpty())sources . addLast (newMapPropertySource(" defaultProperties",this.defaultProperties));// 如果命令行属性存在if(this. addCommandL ineProperties && args . length >0) {String name = CommandL inePropertySource . COMMAND_ LINE_ PROPERTY__SOURCE_ NAME;//如果默认属性资源中不包含该命令, 则将命令行属性放置在第一 位,如果包含,则通过Composite-PropertySource 进行处理if(sources . contains(name)) {PropertySource source = sources.get(name);CompositePropertySource composite =newCompositePropertySource(name);composite . addPropertySource (newSimpleCommandLinePropertySource("springApplicationCommandLineArgs", args));composite . addPropertySource(source);sources . replace(name, composite);telse//放置在第一位sources . addFirst(newSimpleCommandLinePropertySource(args));}}}

这段代码需要重点看一下参数的优先级处理和默认参数 与命令参数之间的关系。首先,如果存在默认属性配置,则将默认属性配置放置在最后,也就是说优先级最低。然后,如果命令参数存在则会 出现两种情况:如果命令的参数 已经存在于属性配置中,则使用CompositePropertySource 类进行相同 name 的参数处理;如果命令的参数并不存在于属性配置中,则直接将其设置为优先级最高。

ConfigurePropertySources 方法的官方注释也很好地解释了它的功能:增加、移除或重新排序应用环境中的任何 PropertySource。

完成了 PropertySources 配置,随后通过 configureProfiles 方法来完成 Profiles 的配置,代码如下。

protectedvoidconfigureProfiles(ConfigurableEnvironment environment, Strin

g[] args)// 保 证 环 境 的 activeProfiles 属 性 被 初 始 化 , 如 果 未 初 始 化 该 方 法 会 对 其 初 始 化environment .getActiveProfiles();//如果存在的额外的 Profiles,则将其放置在第- -位, 随后再获得其他的 ProfilesSet<String>profiles =newLinkedHashSet<>(this. additionalProfiles);profiles . addAll(Arrays. asList(environment . getActiveProfiles()));environment . setActiveProfiles(StringUtils . toStringArray(profiles));}

上面的代码主要用来处理应用环境中哪些配置文件处于激活状态或默认激活状态。对应的配置正是我们经常使用的用来区分不同环境的 spring.profiles.active 参数指定的值。

本文给大家讲解的内容是初始化ApplicationArguments、 ConfigurableEnvironment

原文链接:

https://www.toutiao.com/a6884594655283905028/?log_from=43795b58cb248_1638091496687

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

推荐阅读更多精彩内容