|
|
|
# Spring PropertyPlaceholderConfigurerResolver
|
|
|
|
|
|
|
|
- 类全路径: `org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.PropertyPlaceholderConfigurerResolver`
|
|
|
|
|
|
|
|
- 这个类是从 Properties 中获取属性
|
|
|
|
|
|
|
|
```java
|
|
|
|
private final class PropertyPlaceholderConfigurerResolver implements PlaceholderResolver {
|
|
|
|
|
|
|
|
private final Properties props;
|
|
|
|
|
|
|
|
private PropertyPlaceholderConfigurerResolver(Properties props) {
|
|
|
|
this.props = props;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Nullable
|
|
|
|
public String resolvePlaceholder(String placeholderName) {
|
|
|
|
return PropertyPlaceholderConfigurer.this.resolvePlaceholder(placeholderName,
|
|
|
|
this.props, systemPropertiesMode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
- 详细方法如下
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
protected String resolvePlaceholder(String placeholder, Properties props, int systemPropertiesMode) {
|
|
|
|
String propVal = null;
|
|
|
|
if (systemPropertiesMode == SYSTEM_PROPERTIES_MODE_OVERRIDE) {
|
|
|
|
propVal = resolveSystemProperty(placeholder);
|
|
|
|
}
|
|
|
|
if (propVal == null) {
|
|
|
|
propVal = resolvePlaceholder(placeholder, props);
|
|
|
|
}
|
|
|
|
if (propVal == null && systemPropertiesMode == SYSTEM_PROPERTIES_MODE_FALLBACK) {
|
|
|
|
propVal = resolveSystemProperty(placeholder);
|
|
|
|
}
|
|
|
|
return propVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
```java
|
|
|
|
@Nullable
|
|
|
|
protected String resolvePlaceholder(String placeholder, Properties props) {
|
|
|
|
return props.getProperty(placeholder);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
```java
|
|
|
|
@Nullable
|
|
|
|
protected String resolveSystemProperty(String key) {
|
|
|
|
try {
|
|
|
|
String value = System.getProperty(key);
|
|
|
|
if (value == null && this.searchSystemEnvironment) {
|
|
|
|
value = System.getenv(key);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
catch (Throwable ex) {
|
|
|
|
if (logger.isDebugEnabled()) {
|
|
|
|
logger.debug("Could not access system property '" + key + "': " + ex);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|