Skip to content

Latest commit

 

History

History
83 lines (64 loc) · 2.18 KB

Spring-SystemPropertyUtils.md

File metadata and controls

83 lines (64 loc) · 2.18 KB

Spring SystemPropertyUtils

  • spring 中获取系统属性的工具类

  • 内部属性

/** * * Prefix for system property placeholders: "${". * 前缀占位符 * */publicstaticfinalStringPLACEHOLDER_PREFIX = "${"; /** * Suffix for system property placeholders: "}". * 后缀占位符 * */publicstaticfinalStringPLACEHOLDER_SUFFIX = "}"; /** * Value separator for system property placeholders: ":". * 值分割符号 * */publicstaticfinalStringVALUE_SEPARATOR = ":"; /** * 占位符解析类 */privatestaticfinalPropertyPlaceholderHelperstrictHelper = newPropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, VALUE_SEPARATOR, false); /** * 占位符解析类 */privatestaticfinalPropertyPlaceholderHelpernonStrictHelper = newPropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, VALUE_SEPARATOR, true);

resolvePlaceholders

  • 解析属性

SystemPropertyUtils-resolvePlaceholders.png

时序图因为有递归所以看着有点长, 其核心方法最后会指向 PlaceholderResolver

通过 PlaceholderResolver 获取属性值

SystemPropertyUtils 内部有 PlaceholderResolver 实现

  • 最终通过下面的类来获取具体的属性值
privatestaticclassSystemPropertyPlaceholderResolverimplementsPropertyPlaceholderHelper.PlaceholderResolver { privatefinalStringtext; publicSystemPropertyPlaceholderResolver(Stringtext) { this.text = text; } @Override@NullablepublicStringresolvePlaceholder(StringplaceholderName) { try { StringpropVal = System.getProperty(placeholderName); if (propVal == null) { // Fall back to searching the system environment.// 获取系统属性propVal = System.getenv(placeholderName); } returnpropVal; } catch (Throwableex) { System.err.println("Could not resolve placeholder '" + placeholderName + "' in [" + this.text + "] as system property: " + ex); returnnull; } } }
close