Skip to content

Commit bdfafca

Browse files
committed
doc: PropertySource
1 parent 61ef217 commit bdfafca

13 files changed

+1084
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Spring CommandLinePropertySource
2+
3+
- Author: [HuiFer](https://github.com/huifer)
4+
- 源码阅读仓库: [SourceHot-spring](https://github.com/SourceHot/spring-framework-read)
5+
6+
7+
- 类全路径: `org.springframework.core.env.CommandLinePropertySource`
8+
- 作用: 用来存储命令行参数
9+
10+
11+
12+
13+
14+
```java
15+
public abstract class CommandLinePropertySource<T> extends EnumerablePropertySource<T> {
16+
17+
public static final String COMMAND_LINE_PROPERTY_SOURCE_NAME = "commandLineArgs";
18+
19+
public static final String DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME = "nonOptionArgs";
20+
21+
22+
private String nonOptionArgsPropertyName = DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME;
23+
24+
25+
26+
public CommandLinePropertySource(T source) {
27+
// 命令行参数, 属性值
28+
super(COMMAND_LINE_PROPERTY_SOURCE_NAME, source);
29+
}
30+
31+
public CommandLinePropertySource(String name, T source) {
32+
// 参数名称, 参数值
33+
super(name, source);
34+
}
35+
36+
37+
public void setNonOptionArgsPropertyName(String nonOptionArgsPropertyName) {
38+
this.nonOptionArgsPropertyName = nonOptionArgsPropertyName;
39+
}
40+
41+
@Override
42+
public final boolean containsProperty(String name) {
43+
// 输入值是否等于nonOptionArgs
44+
if (this.nonOptionArgsPropertyName.equals(name)) {
45+
// 等于后判断参数列表是否为空
46+
return !this.getNonOptionArgs().isEmpty();
47+
}
48+
// 是否存在 name 属性
49+
return this.containsOption(name);
50+
}
51+
52+
@Override
53+
@Nullable
54+
public final String getProperty(String name) {
55+
if (this.nonOptionArgsPropertyName.equals(name)) {
56+
// 获取 非可选项参数列表
57+
Collection<String> nonOptionArguments = this.getNonOptionArgs();
58+
if (nonOptionArguments.isEmpty()) {
59+
return null;
60+
}
61+
else {
62+
// 可选参数命令行参数
63+
return StringUtils.collectionToCommaDelimitedString(nonOptionArguments);
64+
}
65+
}
66+
Collection<String> optionValues = this.getOptionValues(name);
67+
if (optionValues == null) {
68+
return null;
69+
}
70+
else {
71+
// 命令行参数
72+
return StringUtils.collectionToCommaDelimitedString(optionValues);
73+
}
74+
}
75+
76+
77+
/**
78+
* 是否存在 name 的命令行参数
79+
*/
80+
protected abstract boolean containsOption(String name);
81+
82+
/**
83+
* 获取参数列表集合
84+
*/
85+
@Nullable
86+
protected abstract List<String> getOptionValues(String name);
87+
88+
/**
89+
* 获取 non-option 参数列表
90+
*/
91+
protected abstract List<String> getNonOptionArgs();
92+
93+
}
94+
```
95+
96+
97+
98+
99+
100+
## getOptionValues
101+
102+
```java
103+
/**
104+
* Return the collection of values associated with the command line option having the
105+
* given name.
106+
* <ul>
107+
* <li>if the option is present and has no argument (e.g.: "--foo"), return an empty
108+
* collection ({@code []})</li>
109+
* <li>if the option is present and has a single value (e.g. "--foo=bar"), return a
110+
* collection having one element ({@code ["bar"]})</li>
111+
* <li>if the option is present and the underlying command line parsing library
112+
* supports multiple arguments (e.g. "--foo=bar --foo=baz"), return a collection
113+
* having elements for each value ({@code ["bar", "baz"]})</li>
114+
* <li>if the option is not present, return {@code null}</li>
115+
* </ul>
116+
*
117+
* 获取参数列表集合
118+
*/
119+
@Nullable
120+
protected abstract List<String> getOptionValues(String name);
121+
```
122+
123+
124+
125+
阅读注释可以知道该方法可以获取命令行参数的列表.
126+
127+
-`--foo`作为开头当输入命令行为 `--foo=bar --foo=baz` 在输入参数名称 `foo` 会得到数据`bar,baz`
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Spring ComparisonPropertySource
2+
3+
- Author: [HuiFer](https://github.com/huifer)
4+
- 源码阅读仓库: [SourceHot-spring](https://github.com/SourceHot/spring-framework-read)
5+
6+
7+
- 整体代码如下.
8+
- 下面几个调用方法会直接抛出异常
9+
1. getSource
10+
1. containsProperty
11+
1. getProperty
12+
13+
```java
14+
static class ComparisonPropertySource extends StubPropertySource {
15+
16+
// 异常信息
17+
private static final String USAGE_ERROR =
18+
"ComparisonPropertySource instances are for use with collection comparison only";
19+
20+
public ComparisonPropertySource(String name) {
21+
super(name);
22+
}
23+
24+
@Override
25+
public Object getSource() {
26+
// 抛异常
27+
throw new UnsupportedOperationException(USAGE_ERROR);
28+
}
29+
30+
@Override
31+
public boolean containsProperty(String name) {
32+
// 抛异常
33+
throw new UnsupportedOperationException(USAGE_ERROR);
34+
}
35+
36+
@Override
37+
@Nullable
38+
public String getProperty(String name) {
39+
// 抛异常
40+
throw new UnsupportedOperationException(USAGE_ERROR);
41+
}
42+
}
43+
44+
```
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Spring CompositePropertySource
2+
- Author: [HuiFer](https://github.com/huifer)
3+
- 源码阅读仓库: [SourceHot-spring](https://github.com/SourceHot/spring-framework-read)
4+
5+
6+
- 全路径: `org.springframework.core.env.CompositePropertySource`
7+
8+
- 整体代码如下
9+
```java
10+
public class CompositePropertySource extends EnumerablePropertySource<Object> {
11+
12+
/**
13+
* set 集合
14+
*/
15+
private final Set<PropertySource<?>> propertySources = new LinkedHashSet<>();
16+
17+
18+
/**
19+
* Create a new {@code CompositePropertySource}.
20+
* @param name the name of the property source
21+
*/
22+
public CompositePropertySource(String name) {
23+
super(name);
24+
}
25+
26+
27+
@Override
28+
@Nullable
29+
public Object getProperty(String name) {
30+
// 循环
31+
for (PropertySource<?> propertySource : this.propertySources) {
32+
// 获取存储内容
33+
Object candidate = propertySource.getProperty(name);
34+
if (candidate != null) {
35+
return candidate;
36+
}
37+
}
38+
return null;
39+
}
40+
41+
@Override
42+
public boolean containsProperty(String name) {
43+
for (PropertySource<?> propertySource : this.propertySources) {
44+
// 是否存在name
45+
if (propertySource.containsProperty(name)) {
46+
return true;
47+
}
48+
}
49+
return false;
50+
}
51+
52+
@Override
53+
public String[] getPropertyNames() {
54+
Set<String> names = new LinkedHashSet<>();
55+
for (PropertySource<?> propertySource : this.propertySources) {
56+
// 类型不同抛出异常
57+
if (!(propertySource instanceof EnumerablePropertySource)) {
58+
throw new IllegalStateException(
59+
"Failed to enumerate property names due to non-enumerable property source: " + propertySource);
60+
}
61+
// 批量添加
62+
names.addAll(Arrays.asList(((EnumerablePropertySource<?>) propertySource).getPropertyNames()));
63+
}
64+
// 转换成 array
65+
return StringUtils.toStringArray(names);
66+
}
67+
68+
69+
/**
70+
* Add the given {@link PropertySource} to the end of the chain.
71+
* @param propertySource the PropertySource to add
72+
*/
73+
public void addPropertySource(PropertySource<?> propertySource) {
74+
this.propertySources.add(propertySource);
75+
}
76+
77+
/**
78+
* Add the given {@link PropertySource} to the start of the chain.
79+
* @param propertySource the PropertySource to add
80+
* @since 4.1
81+
*/
82+
public void addFirstPropertySource(PropertySource<?> propertySource) {
83+
// 头插
84+
List<PropertySource<?>> existing = new ArrayList<>(this.propertySources);
85+
this.propertySources.clear();
86+
this.propertySources.add(propertySource);
87+
this.propertySources.addAll(existing);
88+
}
89+
90+
/**
91+
* Return all property sources that this composite source holds.
92+
* @since 4.1.1
93+
*/
94+
public Collection<PropertySource<?>> getPropertySources() {
95+
return this.propertySources;
96+
}
97+
98+
99+
@Override
100+
public String toString() {
101+
return getClass().getSimpleName() + " {name='" + this.name + "', propertySources=" + this.propertySources + "}";
102+
}
103+
104+
}
105+
```
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Spring EnumerablePropertySource
2+
3+
- Author: [HuiFer](https://github.com/huifer)
4+
- 源码阅读仓库: [SourceHot-spring](https://github.com/SourceHot/spring-framework-read)
5+
6+
- 全路径: `org.springframework.core.env.EnumerablePropertySource`
7+
- 在这个类中定义了一个抽象方法`getPropertyNames` 用来获取所有的 property 的名称
8+
9+
```java
10+
public abstract String[] getPropertyNames();
11+
```
12+
- 整体代码如下
13+
```java
14+
public abstract class EnumerablePropertySource<T> extends PropertySource<T> {
15+
16+
public EnumerablePropertySource(String name, T source) {
17+
super(name, source);
18+
}
19+
20+
protected EnumerablePropertySource(String name) {
21+
super(name);
22+
}
23+
24+
25+
/**
26+
* Return whether this {@code PropertySource} contains a property with the given name.
27+
* <p>This implementation checks for the presence of the given name within the
28+
* {@link #getPropertyNames()} array.
29+
*
30+
* 在属性列表中是否存在 properties
31+
* @param name the name of the property to find
32+
*/
33+
@Override
34+
public boolean containsProperty(String name) {
35+
return ObjectUtils.containsElement(getPropertyNames(), name);
36+
}
37+
38+
/**
39+
* Return the names of all properties contained by the
40+
* 获取所有的 properties 名称
41+
* {@linkplain #getSource() source} object (never {@code null}).
42+
*/
43+
public abstract String[] getPropertyNames();
44+
45+
}
46+
```
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Spring MapPropertySource
2+
3+
- Author: [HuiFer](https://github.com/huifer)
4+
- 源码阅读仓库: [SourceHot-spring](https://github.com/SourceHot/spring-framework-read)
5+
6+
7+
- 类全路径: `org.springframework.core.env.MapPropertySource`
8+
- 内部数据结构是一个`Map<String,Object>`
9+
这是一个对map的操作.
10+
11+
- 整体代码如下.
12+
13+
```java
14+
15+
public class MapPropertySource extends EnumerablePropertySource<Map<String, Object>> {
16+
17+
public MapPropertySource(String name, Map<String, Object> source) {
18+
super(name, source);
19+
}
20+
21+
22+
@Override
23+
@Nullable
24+
public Object getProperty(String name) {
25+
// 从map中获取 name 对应的value
26+
return this.source.get(name);
27+
}
28+
29+
@Override
30+
public boolean containsProperty(String name) {
31+
// 判断是否存在 name 属性
32+
return this.source.containsKey(name);
33+
}
34+
35+
@Override
36+
public String[] getPropertyNames() {
37+
// 互殴去 map 的所有key
38+
return StringUtils.toStringArray(this.source.keySet());
39+
}
40+
41+
}
42+
43+
```

0 commit comments

Comments
 (0)