Spring SystemPropertyUtils

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

  • 内部属性

  1. /**
  2. *
  3. * Prefix for system property placeholders: "${".
  4. * 前缀占位符
  5. * */
  6. public static final String PLACEHOLDER_PREFIX = "${";
  7. /**
  8. * Suffix for system property placeholders: "}".
  9. * 后缀占位符
  10. * */
  11. public static final String PLACEHOLDER_SUFFIX = "}";
  12. /**
  13. * Value separator for system property placeholders: ":".
  14. * 值分割符号
  15. * */
  16. public static final String VALUE_SEPARATOR = ":";
  17. /**
  18. * 占位符解析类
  19. */
  20. private static final PropertyPlaceholderHelper strictHelper =
  21. new PropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, VALUE_SEPARATOR, false);
  22. /**
  23. * 占位符解析类
  24. */
  25. private static final PropertyPlaceholderHelper nonStrictHelper =
  26. new PropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, VALUE_SEPARATOR, true);

resolvePlaceholders

  • 解析属性

SystemPropertyUtils-resolvePlaceholders.png

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

通过 PlaceholderResolver 获取属性值

SystemPropertyUtils 内部有 PlaceholderResolver 实现

  • 最终通过下面的类来获取具体的属性值
  1. private static class SystemPropertyPlaceholderResolver implements PropertyPlaceholderHelper.PlaceholderResolver {
  2. private final String text;
  3. public SystemPropertyPlaceholderResolver(String text) {
  4. this.text = text;
  5. }
  6. @Override
  7. @Nullable
  8. public String resolvePlaceholder(String placeholderName) {
  9. try {
  10. String propVal = System.getProperty(placeholderName);
  11. if (propVal == null) {
  12. // Fall back to searching the system environment.
  13. // 获取系统属性
  14. propVal = System.getenv(placeholderName);
  15. }
  16. return propVal;
  17. }
  18. catch (Throwable ex) {
  19. System.err.println("Could not resolve placeholder '" + placeholderName + "' in [" +
  20. this.text + "] as system property: " + ex);
  21. return null;
  22. }
  23. }
  24. }