parent
bdfafcad7e
commit
4d21e6d140
@ -0,0 +1,26 @@
|
||||
# Spring PlaceholderResolver
|
||||
- 类全路径: `org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver`
|
||||
|
||||
- 类作用将占位符中的内容替换成属性值.
|
||||
- 假设现有属性表: user.dir = c:\home
|
||||
传入参数 user.dir 会获得 c:\home
|
||||
|
||||
```java
|
||||
|
||||
@FunctionalInterface
|
||||
public interface PlaceholderResolver {
|
||||
|
||||
/**
|
||||
* Resolve the supplied placeholder name to the replacement value.
|
||||
* @param placeholderName the name of the placeholder to resolve
|
||||
* @return the replacement value, or {@code null} if no replacement is to be made
|
||||
*/
|
||||
@Nullable
|
||||
String resolvePlaceholder(String placeholderName);
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
- 类图如下
|
||||
|
||||
![PropertyPlaceholderConfigurerResolver](/images/spring/PropertyPlaceholderConfigurerResolver.png)
|
@ -0,0 +1,75 @@
|
||||
# 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;
|
||||
}
|
||||
}
|
||||
|
||||
```
|
@ -0,0 +1,43 @@
|
||||
# Spring ServletContextPlaceholderResolver
|
||||
|
||||
|
||||
- 类全路径: `org.springframework.web.util.ServletContextPropertyUtils.ServletContextPlaceholderResolver`
|
||||
|
||||
```java
|
||||
private static class ServletContextPlaceholderResolver
|
||||
implements PropertyPlaceholderHelper.PlaceholderResolver {
|
||||
|
||||
private final String text;
|
||||
|
||||
private final ServletContext servletContext;
|
||||
|
||||
public ServletContextPlaceholderResolver(String text, ServletContext servletContext) {
|
||||
this.text = text;
|
||||
this.servletContext = servletContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String resolvePlaceholder(String placeholderName) {
|
||||
try {
|
||||
// servlet 上下文获取
|
||||
String propVal = this.servletContext.getInitParameter(placeholderName);
|
||||
if (propVal == null) {
|
||||
// Fall back to system properties.
|
||||
propVal = System.getProperty(placeholderName);
|
||||
if (propVal == null) {
|
||||
// Fall back to searching the system environment.
|
||||
propVal = System.getenv(placeholderName);
|
||||
}
|
||||
}
|
||||
return propVal;
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
System.err.println("Could not resolve placeholder '" + placeholderName + "' in [" +
|
||||
this.text + "] as ServletContext init-parameter or system property: " + ex);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
```
|
@ -0,0 +1,35 @@
|
||||
# Spring SystemPropertyPlaceholderResolver
|
||||
|
||||
- 类全路径: `org.springframework.util.SystemPropertyUtils.SystemPropertyPlaceholderResolver`
|
||||
|
||||
|
||||
```java
|
||||
private static class SystemPropertyPlaceholderResolver implements PropertyPlaceholderHelper.PlaceholderResolver {
|
||||
|
||||
private final String text;
|
||||
|
||||
public SystemPropertyPlaceholderResolver(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String resolvePlaceholder(String placeholderName) {
|
||||
try {
|
||||
String propVal = System.getProperty(placeholderName);
|
||||
if (propVal == null) {
|
||||
// Fall back to searching the system environment.
|
||||
// 获取系统属性
|
||||
propVal = System.getenv(placeholderName);
|
||||
}
|
||||
return propVal;
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
System.err.println("Could not resolve placeholder '" + placeholderName + "' in [" +
|
||||
this.text + "] as system property: " + ex);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
```
|
After Width: | Height: | Size: 12 KiB |
After Width: | Height: | Size: 12 KiB |
Loading…
Reference in new issue