Class SettingUtil

java.lang.Object
com.sure.tool.config.SettingUtil

public class SettingUtil extends Object
分层配置读取工具类,参考 Hutool Setting 设计。

配置来源优先级(高 → 低):

  1. 系统属性(-Dkey=value)
  2. 环境变量(key 中的点转为下划线并大写,如 server.port → SERVER_PORT)
  3. 配置文件(UTF-8、key=value、# 注释、空行忽略)
  4. 调用方提供的默认值

状态为 Copy-on-Write 快照,load(String) 系列方法整体替换配置, 并发读写安全。

Since:
1.2.0
Author:
suretool
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    清空当前配置快照。
    static boolean
    是否包含指定键(分层查找命中任意一层均视为包含)。
    static Object
    get(String key)
    获取原始配置值(分层查找)。
    static Boolean
    获取布尔配置(分层查找)。
    static boolean
    getBoolean(String key, boolean defaultValue)
    获取布尔配置(分层查找)。
    static Double
    获取双精度配置(分层查找)。
    static double
    getDouble(String key, double defaultValue)
    获取双精度配置(分层查找)。
    static Integer
    获取整型配置(分层查找)。
    static int
    getInt(String key, int defaultValue)
    获取整型配置(分层查找)。
    static Long
    获取长整型配置(分层查找)。
    static long
    getLong(String key, long defaultValue)
    获取长整型配置(分层查找)。
    static String
    获取字符串配置(分层查找,无默认值)。
    static String
    getString(String key, String defaultValue)
    获取字符串配置(分层查找)。
    static Set<String>
    返回当前配置快照的全部键。
    static void
    load(File file)
    从文件加载配置(UTF-8),整体替换当前快照。
    static void
    从输入流加载配置(UTF-8),整体替换当前快照。
    static void
    load(String path)
    从文件加载配置(UTF-8),整体替换当前快照。
    static void
    load(URL url)
    从 URL 加载配置(UTF-8),整体替换当前快照。

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • load

      public static void load(String path)
      从文件加载配置(UTF-8),整体替换当前快照。
      Parameters:
      path - 文件路径
    • load

      public static void load(File file)
      从文件加载配置(UTF-8),整体替换当前快照。
      Parameters:
      file - 文件
    • load

      public static void load(URL url)
      从 URL 加载配置(UTF-8),整体替换当前快照。
      Parameters:
      url - 配置 URL
    • load

      public static void load(InputStream in)
      从输入流加载配置(UTF-8),整体替换当前快照。
      Parameters:
      in - 输入流
    • getString

      public static String getString(String key)
      获取字符串配置(分层查找,无默认值)。
      Parameters:
      key - 配置键
      Returns:
      配置值,未找到返回 null
    • getString

      public static String getString(String key, String defaultValue)
      获取字符串配置(分层查找)。
      Parameters:
      key - 配置键
      defaultValue - 默认值
      Returns:
      配置值,未找到返回默认值
    • getInt

      public static Integer getInt(String key)
      获取整型配置(分层查找)。
      Parameters:
      key - 配置键
      Returns:
      配置值,未找到返回 null
    • getInt

      public static int getInt(String key, int defaultValue)
      获取整型配置(分层查找)。
      Parameters:
      key - 配置键
      defaultValue - 默认值
      Returns:
      配置值,未找到返回默认值
    • getLong

      public static Long getLong(String key)
      获取长整型配置(分层查找)。
      Parameters:
      key - 配置键
      Returns:
      配置值,未找到返回 null
    • getLong

      public static long getLong(String key, long defaultValue)
      获取长整型配置(分层查找)。
      Parameters:
      key - 配置键
      defaultValue - 默认值
      Returns:
      配置值,未找到返回默认值
    • getDouble

      public static Double getDouble(String key)
      获取双精度配置(分层查找)。
      Parameters:
      key - 配置键
      Returns:
      配置值,未找到返回 null
    • getDouble

      public static double getDouble(String key, double defaultValue)
      获取双精度配置(分层查找)。
      Parameters:
      key - 配置键
      defaultValue - 默认值
      Returns:
      配置值,未找到返回默认值
    • getBoolean

      public static Boolean getBoolean(String key)
      获取布尔配置(分层查找)。
      Parameters:
      key - 配置键
      Returns:
      配置值,未找到返回 null
    • getBoolean

      public static boolean getBoolean(String key, boolean defaultValue)
      获取布尔配置(分层查找)。
      Parameters:
      key - 配置键
      defaultValue - 默认值
      Returns:
      配置值,未找到返回默认值
    • get

      public static Object get(String key)
      获取原始配置值(分层查找)。
      Parameters:
      key - 配置键
      Returns:
      配置值,未找到返回 null
    • contains

      public static boolean contains(String key)
      是否包含指定键(分层查找命中任意一层均视为包含)。
      Parameters:
      key - 配置键
      Returns:
      命中返回 true
    • keys

      public static Set<String> keys()
      返回当前配置快照的全部键。
      Returns:
      配置键集合
    • clear

      public static void clear()
      清空当前配置快照。