Module bus.setting

Class Props

All Implemented Interfaces:
Serializable, Cloneable, Map<Object,Object>, org.miaixz.bus.core.lang.getter.TypeGetter<CharSequence>

public final class Props extends Properties implements org.miaixz.bus.core.lang.getter.TypeGetter<CharSequence>
Properties文件读取封装类
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Constructor Details

    • Props

      public Props()
      构造
    • Props

      public Props(String path)
      构造,使用相对于Class文件根目录的相对路径
      Parameters:
      path - 配置文件路径,相对于ClassPath,或者使用绝对路径
    • Props

      public Props(String path, Charset charset)
      构造,使用相对于Class文件根目录的相对路径
      Parameters:
      path - 相对或绝对路径
      charset - 自定义编码
    • Props

      public Props(File propertiesFile)
      构造
      Parameters:
      propertiesFile - 配置文件对象
    • Props

      public Props(File propertiesFile, Charset charset)
      构造
      Parameters:
      propertiesFile - 配置文件对象
      charset - 自定义编码
    • Props

      public Props(org.miaixz.bus.core.io.resource.Resource resource, Charset charset)
      构造,使用URL读取
      Parameters:
      resource - Resource
      charset - 自定义编码
    • Props

      public Props(Properties properties)
      构造,使用URL读取
      Parameters:
      properties - 属性文件路径
  • Method Details

    • of

      public static Props of()
      构建一个空的Props,用于手动加入参数
      Returns:
      Setting
    • of

      public static Props of(String resource)
      获得Classpath下的Properties文件
      Parameters:
      resource - 资源(相对Classpath的路径)
      Returns:
      Properties
    • of

      public static Props of(String resource, Charset charset)
      获得Classpath下的Properties文件
      Parameters:
      resource - 资源(相对Classpath的路径)
      charset - 自定义编码
      Returns:
      Properties
    • of

      public static Props of(Props properties)
      Props转为Props
      Parameters:
      properties - Props
      Returns:
      Properties
    • get

      public static Props get(String name)
      获取当前环境下的配置文件 name可以为不包括扩展名的文件名(默认.properties),也可以是文件名全称
      Parameters:
      name - 文件名,如果没有扩展名,默认为.properties
      Returns:
      当前环境下配置文件
    • parse

      public static void parse(Map<String,Object> result, String content)
      解析PROPS
      Parameters:
      result - 数据结果
      content - 数据内容
    • getFirstFound

      public static Props getFirstFound(String... names)
      获取给定路径找到的第一个配置文件 * name可以为不包括扩展名的文件名(默认.properties为结尾),也可以是文件名全称
      Parameters:
      names - 文件名,如果没有扩展名,默认为.properties
      Returns:
      当前环境下配置文件
    • getProperties

      public static Props getProperties()
      获取系统参数,例如用户在执行java命令时定义的 -Duse=bus
      Returns:
      系统参数Props
    • load

      public void load(URL url)
      初始化配置文件
      Parameters:
      url - URL
    • load

      public void load(org.miaixz.bus.core.io.resource.Resource resource)
      初始化配置文件
      Parameters:
      resource - Resource
    • load

      public void load()
      重新加载配置文件
    • autoLoad

      public void autoLoad(boolean autoReload)
      在配置文件变更时自动加载
      Parameters:
      autoReload - 是否自动加载
    • getObject

      public Object getObject(CharSequence key, Object defaultValue)
      Specified by:
      getObject in interface org.miaixz.bus.core.lang.getter.TypeGetter<CharSequence>
    • get

      public <P, T> T get(org.miaixz.bus.core.center.function.FunctionX<P,T> func)
      根据lambda的方法引用,获取
      Type Parameters:
      P - 参数类型
      T - 返回值类型
      Parameters:
      func - 方法引用
      Returns:
      获取表达式对应属性和返回的对象
    • getAndRemoveString

      public String getAndRemoveString(String... keys)
      获取并删除键值对,当指定键对应值非空时,返回并删除这个值,后边的键对应的值不再查找
      Parameters:
      keys - 键列表,常用于别名
      Returns:
      字符串值
    • getSubProps

      public Props getSubProps(String prefix)
      获取一个新的子属性,子属性键值对拥有公共前缀,以.分隔。
           a.b
           a.c
           b.a
       
      则调用getSubProps("a");得到
           a.b
           a.c
       
      Parameters:
      prefix - 前缀,可以不以.结尾
      Returns:
      子属性
    • toProperties

      public Props toProperties()
      转换为标准的Props对象
      Returns:
      Props对象
    • toBean

      public <T> T toBean(Class<T> beanClass)
      将配置文件转换为Bean,支持嵌套Bean 支持的表达式:
       persion
       persion.name
       persons[3]
       person.friends[5].name
       ['person']['friends'][5]['name']
       
      Type Parameters:
      T - Bean类型
      Parameters:
      beanClass - Bean类
      Returns:
      Bean对象
    • toBean

      public <T> T toBean(Class<T> beanClass, String prefix)
      将配置文件转换为Bean,支持嵌套Bean 支持的表达式:
       persion
       persion.name
       persons[3]
       person.friends[5].name
       ['person']['friends'][5]['name']
       
      Type Parameters:
      T - Bean类型
      Parameters:
      beanClass - Bean类
      prefix - 公共前缀,不指定前缀传null,当指定前缀后非此前缀的属性被忽略
      Returns:
      Bean对象
    • toBean

      public <T> T toBean(T bean)
      将配置文件转换为Bean,支持嵌套Bean 支持的表达式:
       persion
       persion.name
       persons[3]
       person.friends[5].name
       ['person']['friends'][5]['name']
       
      Type Parameters:
      T - Bean类型
      Parameters:
      bean - Bean对象
      Returns:
      Bean对象
    • toBean

      public <T> T toBean(T bean, String prefix)
      将配置文件转换为Bean,支持嵌套Bean 支持的表达式:
       persion
       persion.name
       persons[3]
       person.friends[5].name
       ['person']['friends'][5]['name']
       
      Type Parameters:
      T - Bean类型
      Parameters:
      bean - Bean对象
      prefix - 公共前缀,不指定前缀传null,当指定前缀后非此前缀的属性被忽略
      Returns:
      Bean对象
    • set

      public void set(String key, Object value)
      设置值,无给定键创建之。设置后未持久化
      Parameters:
      key - 属性键
      value - 属性值
    • setFields

      public Props setFields(org.miaixz.bus.core.center.function.SupplierX<?>... fields)
      通过lambda批量设置值 实际使用时,可以使用getXXX的方法引用来完成键值对的赋值:
       User user = GenericBuilder.of(User::new).with(User::setUsername, "bus").build();
       Setting.of().setFields(user::getNickname, user::getUsername);
       
      Parameters:
      fields - lambda,不能为空
      Returns:
      this
    • store

      public void store(String absolutePath) throws org.miaixz.bus.core.lang.exception.InternalException
      持久化当前设置,会覆盖掉之前的设置
      Parameters:
      absolutePath - 设置文件的绝对路径
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - IO异常,可能为文件未找到
    • store

      public void store(String path, Class<?> clazz)
      存储当前设置,会覆盖掉以前的设置
      Parameters:
      path - 相对路径
      clazz - 相对的类