SpringBoot源码解析:Environment获取配置原理(2)

您所在的位置:网站首页 enviironment怎么读 SpringBoot源码解析:Environment获取配置原理(2)

SpringBoot源码解析:Environment获取配置原理(2)

2023-10-29 00:02| 来源: 网络整理| 查看: 265

debug的时候发现一个很有意思的事情,就是第二个元素configurationProperties中的ConfigurationPropertySourcesPropertySource 包含了外面所有的propertySource。

在这里插入图片描述

所以在获取bored.coupon.coupon-no配置值的时候,当遍历到name=configurationProperties时获取到ConfigurationPropertySourcesPropertySource。

然后在ConfigurationPropertySourcesPropertySource 中把这些配置又遍历了一遍,获取到之后就return了。

并没有如我们想象的遍历到PropertySource序号7的元素applicationConfig

ConfigurationPropertySourcesPropertySource源码如下:

class ConfigurationPropertySourcesPropertySource extends PropertySource implements OriginLookup { ConfigurationPropertySourcesPropertySource(String name, Iterable source) { super(name, source); } @Override public Object getProperty(String name) { ConfigurationProperty configurationProperty = findConfigurationProperty(name); return (configurationProperty != null) ? configurationProperty.getValue() : null; } private ConfigurationProperty findConfigurationProperty(String name) { try { return findConfigurationProperty(ConfigurationPropertyName.of(name, true)); } catch (Exception ex) { return null; } } private ConfigurationProperty findConfigurationProperty(ConfigurationPropertyName name) { if (name == null) { return null; } //最终在此处遍历的所有配置文件,获取的相关属性值 for (ConfigurationPropertySource configurationPropertySource : getSource()) { ConfigurationProperty configurationProperty = configurationPropertySource.getConfigurationProperty(name); if (configurationProperty != null) { return configurationProperty; } } return null; }

疑惑,这是哪放进去的呢?前一篇有提到:SpringApplication的prepareEnvironment方法

private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments) { //2.1 Create and configure the environment 创建和配置environment ConfigurableEnvironment environment = getOrCreateEnvironment(); //2.2 加载启动命令行配置属性,active属性。(先不深入讲解) configureEnvironment(environment, applicationArguments.getSourceArgs()); //2.3 发布事件 listeners.environmentPrepared(environment); bindToSpringApplication(environment); if (!this.isCustomEnvironment) { environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment, deduceEnvironmentClass()); } //就是此处,把上述解析的所有配置文件PropertySources,添加到key为configurationProperties的PropertySource对象中,并添加到PropertySources的第一个元素中,我们可以深入一下看看 ConfigurationPropertySources.attach(environment); return environment; }

ConfigurationPropertySources.attach(environment);

public static void attach(Environment environment) { Assert.isInstanceOf(ConfigurableEnvironment.class, environment); //从environment中获取所有解析的配置MutablePropertySources MutablePropertySources sources = ((ConfigurableEnvironment) environment).getPropertySources(); PropertySource attached = sources.get(ATTACHED_PROPERTY_SOURCE_NAME); if (attached != null && attached.getSource() != sources) { sources.remove(ATTACHED_PROPERTY_SOURCE_NAME); attached = null; } if (attached == null) { //把上述MutablePropertySources封装到ConfigurationPropertySourcesPropertySource,重新添加到MutablePropertySources中 sources.addFirst(new ConfigurationPropertySourcesPropertySource(ATTACHED_PROPERTY_SOURCE_NAME, new SpringConfigurationPropertySources(sources))); } }

为什么要引入ConfigurationPropertySourcesPropertySource?

源码中看到直接通过name=configurationProperties获取ConfigurationPropertySourcesPropertySource 的暂时只有ConfigurationPropertySources类get方法,

attach也是ConfigurationPropertySources这个类。

而此处的get方法就是提供给Binder类使用(我们继续往下看)

ConfigurationPropertySources源码如下:

public static Iterable get(Environment environment) { Assert.isInstanceOf(ConfigurableEnvironment.class, environment); MutablePropertySources sources = ((ConfigurableEnvironment) environment).getPropertySources(); ConfigurationPropertySourcesPropertySource attached = (ConfigurationPropertySourcesPropertySource) sources .get(ATTACHED_PROPERTY_SOURCE_NAME); if (attached == null) { return from(sources); } return attached.getSource(); }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3