diff --git a/docs/Spring/clazz/PlaceholderResolver/Spring-PlaceholderResolver.md b/docs/Spring/clazz/PlaceholderResolver/Spring-PlaceholderResolver.md new file mode 100644 index 0000000..e511150 --- /dev/null +++ b/docs/Spring/clazz/PlaceholderResolver/Spring-PlaceholderResolver.md @@ -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) diff --git a/docs/Spring/clazz/PlaceholderResolver/Spring-PropertyPlaceholderConfigurerResolver.md b/docs/Spring/clazz/PlaceholderResolver/Spring-PropertyPlaceholderConfigurerResolver.md new file mode 100644 index 0000000..cb50a27 --- /dev/null +++ b/docs/Spring/clazz/PlaceholderResolver/Spring-PropertyPlaceholderConfigurerResolver.md @@ -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; + } + } + +``` \ No newline at end of file diff --git a/docs/Spring/clazz/PlaceholderResolver/Spring-ServletContextPlaceholderResolver.md b/docs/Spring/clazz/PlaceholderResolver/Spring-ServletContextPlaceholderResolver.md new file mode 100644 index 0000000..f3ba8d3 --- /dev/null +++ b/docs/Spring/clazz/PlaceholderResolver/Spring-ServletContextPlaceholderResolver.md @@ -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; + } + } + } + +``` \ No newline at end of file diff --git a/docs/Spring/clazz/PlaceholderResolver/Spring-SystemPropertyPlaceholderResolver.md b/docs/Spring/clazz/PlaceholderResolver/Spring-SystemPropertyPlaceholderResolver.md new file mode 100644 index 0000000..d10ca38 --- /dev/null +++ b/docs/Spring/clazz/PlaceholderResolver/Spring-SystemPropertyPlaceholderResolver.md @@ -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; + } + } + } + +``` \ No newline at end of file diff --git a/docs/Spring/clazz/PlaceholderResolver/images/PropertyPlaceholderConfigurerResolver.png b/docs/Spring/clazz/PlaceholderResolver/images/PropertyPlaceholderConfigurerResolver.png new file mode 100644 index 0000000..16894fe Binary files /dev/null and b/docs/Spring/clazz/PlaceholderResolver/images/PropertyPlaceholderConfigurerResolver.png differ diff --git a/images/spring/PropertyPlaceholderConfigurerResolver.png b/images/spring/PropertyPlaceholderConfigurerResolver.png new file mode 100644 index 0000000..16894fe Binary files /dev/null and b/images/spring/PropertyPlaceholderConfigurerResolver.png differ