spring5.x源码分析之自定义PropertySources

这两天太忙了没时间更新,上一期我们聊到spring在解析资源配置文件的时候会去获取当前上下文环境,
如果环境变量

1
2
3
4
5
6
7
8
9
10
11

```Java
/**
* Create and return a new {@link StandardEnvironment}.
* <p>Subclasses may override this method in order to supply
* a custom {@link ConfigurableEnvironment} implementation.
*/
protected ConfigurableEnvironment createEnvironment() {
//StandardEnvironment 构造函数 会 使他的抽象父类 也执行构造函数
return new StandardEnvironment();
}

注释写的很清晰,作用就是创建一个

1
2
3
4
5
6
7
8
9
10

那我们再来看看StandardEnvironment内部做了什么,它继承自```AbstractEnvironment```,
然后有两个私有静态final变量:

```Java
/** System environment property source name: {@value}. */
public static final String SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME = "systemEnvironment";

/** JVM system properties property source name: {@value}. */
public static final String SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME = "systemProperties";

这两个变量 一个是 操作系统环境变量,另一个是JVM系统环境变量,他们干嘛用呢,我们看到StandardEnvironment
还定义了一个方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

```Java
/**
* Customize the set of property sources with those appropriate for any standard(适用于任何标准的)
* Java environment:
* <ul>
* <li>{@value #SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME}
* <li>{@value #SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME}
* </ul>
* <p>Properties present in {@value #SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME} will
* take precedence over those in {@value #SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME}.
* @see AbstractEnvironment#customizePropertySources(MutablePropertySources)
* @see #getSystemProperties()
* @see #getSystemEnvironment()
*/
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {

//https://blog.csdn.net/rtuujnncc/article/details/78248733
//addLast最低优先级
//https://www.cnblogs.com/Baronboy/p/6030443.html
propertySources.addLast(new MapPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties()));
propertySources.addLast(new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment()));
}

  1. MapPropertySource 是 System.getProperties()
  2. SystemEnvironmentPropertySource 是System.getenv()

这个方法是继承自它的抽象类

StandardEnvironment```的时候
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
会先调用```AbstractEnvironment```的构造函数,函数内部会调用customizePropertySources方法,并且会把
propertySources参数传入。

```Java
//可变的属性源集合
private final MutablePropertySources propertySources = new MutablePropertySources();
//把propertySources放入 Resolver中
private final ConfigurablePropertyResolver propertyResolver =
new PropertySourcesPropertyResolver(this.propertySources);

/**
* Create a new {@code Environment} instance, calling back to
* {@link #customizePropertySources(MutablePropertySources)} during construction to
* allow subclasses to contribute or manipulate(操作) {@link PropertySource} instances as
* appropriate.
* @see #customizePropertySources(MutablePropertySources)
*/
//调用 子类的customizePropertySources
public AbstractEnvironment() {
customizePropertySources(this.propertySources);
}

customizePropertySources也允许子类去重写,这样就允许子类去操作这个propertySources变量了,在
Spring中我们看到很多这样的设计,包括接口和抽象类的运用到处都是。关于这个自定义propertySources有什么用
我们下回再解!

欢迎关注我的公众号:沉迷Spring
显示 Gitment 评论
0%