- 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:
-
Field Summary
Fields inherited from class java.util.Properties
defaults -
Constructor Summary
ConstructorsConstructorDescriptionProps()构造构造构造构造,使用相对于Class文件根目录的相对路径构造,使用相对于Class文件根目录的相对路径Props(Properties properties) 构造,使用URL读取构造,使用URL读取 -
Method Summary
Modifier and TypeMethodDescriptionvoidautoLoad(boolean autoReload) 在配置文件变更时自动加载static Props获取当前环境下的配置文件 name可以为不包括扩展名的文件名(默认.properties),也可以是文件名全称<P,T> T get(org.miaixz.bus.core.center.function.FunctionX<P, T> func) 根据lambda的方法引用,获取getAndRemoveString(String... keys) 获取并删除键值对,当指定键对应值非空时,返回并删除这个值,后边的键对应的值不再查找static PropsgetFirstFound(String... names) 获取给定路径找到的第一个配置文件 * name可以为不包括扩展名的文件名(默认.properties为结尾),也可以是文件名全称getObject(CharSequence key, Object defaultValue) static Props获取系统参数,例如用户在执行java命令时定义的 -Duse=busgetSubProps(String prefix) 获取一个新的子属性,子属性键值对拥有公共前缀,以.分隔。voidload()重新加载配置文件void初始化配置文件voidload(org.miaixz.bus.core.io.resource.Resource resource) 初始化配置文件static Propsof()构建一个空的Props,用于手动加入参数static Props获得Classpath下的Properties文件static Props获得Classpath下的Properties文件static PropsProps转为Propsstatic void解析PROPSvoid设置值,无给定键创建之。setFields(org.miaixz.bus.core.center.function.SupplierX<?>... fields) 通过lambda批量设置值 实际使用时,可以使用getXXX的方法引用来完成键值对的赋值:void持久化当前设置,会覆盖掉之前的设置void存储当前设置,会覆盖掉以前的设置<T> T将配置文件转换为Bean,支持嵌套Bean 支持的表达式:<T> T将配置文件转换为Bean,支持嵌套Bean 支持的表达式:<T> TtoBean(T bean) 将配置文件转换为Bean,支持嵌套Bean 支持的表达式:<T> T将配置文件转换为Bean,支持嵌套Bean 支持的表达式:转换为标准的Props对象Methods inherited from class java.util.Properties
clear, clone, compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, equals, forEach, get, getOrDefault, getProperty, getProperty, hashCode, isEmpty, keys, keySet, list, list, load, load, loadFromXML, merge, propertyNames, put, putAll, putIfAbsent, rehash, remove, remove, replace, replace, replaceAll, save, setProperty, size, store, store, storeToXML, storeToXML, storeToXML, stringPropertyNames, toString, valuesMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.miaixz.bus.core.lang.getter.TypeGetter
get, get, get, getBigDecimal, getBigDecimal, getBigInteger, getBigInteger, getBool, getBool, getByte, getByte, getBytes, getBytes, getChar, getChar, getDate, getDate, getDouble, getDouble, getEnum, getEnum, getFloat, getFloat, getInt, getInt, getLocalDate, getLocalDate, getLocalDateTime, getLocalDateTime, getLocalTime, getLocalTime, getLong, getLong, getNumber, getNumber, getObject, getShort, getShort, getSqlTime, getSqlTime, getSqlTimestamp, getSqlTimestamp, getString, getString
-
Constructor Details
-
Props
public Props()构造 -
Props
构造,使用相对于Class文件根目录的相对路径- Parameters:
path- 配置文件路径,相对于ClassPath,或者使用绝对路径
-
Props
构造,使用相对于Class文件根目录的相对路径- Parameters:
path- 相对或绝对路径charset- 自定义编码
-
Props
构造- Parameters:
propertiesFile- 配置文件对象
-
Props
构造- Parameters:
propertiesFile- 配置文件对象charset- 自定义编码
-
Props
构造,使用URL读取- Parameters:
resource-Resourcecharset- 自定义编码
-
Props
构造,使用URL读取- Parameters:
properties- 属性文件路径
-
-
Method Details
-
of
构建一个空的Props,用于手动加入参数- Returns:
- Setting
-
of
获得Classpath下的Properties文件- Parameters:
resource- 资源(相对Classpath的路径)- Returns:
- Properties
-
of
获得Classpath下的Properties文件- Parameters:
resource- 资源(相对Classpath的路径)charset- 自定义编码- Returns:
- Properties
-
of
Props转为Props- Parameters:
properties-Props- Returns:
- Properties
-
get
获取当前环境下的配置文件 name可以为不包括扩展名的文件名(默认.properties),也可以是文件名全称- Parameters:
name- 文件名,如果没有扩展名,默认为.properties- Returns:
- 当前环境下配置文件
-
parse
解析PROPS- Parameters:
result- 数据结果content- 数据内容
-
getFirstFound
获取给定路径找到的第一个配置文件 * name可以为不包括扩展名的文件名(默认.properties为结尾),也可以是文件名全称- Parameters:
names- 文件名,如果没有扩展名,默认为.properties- Returns:
- 当前环境下配置文件
-
getProperties
获取系统参数,例如用户在执行java命令时定义的 -Duse=bus- Returns:
- 系统参数Props
-
load
初始化配置文件- 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
- Specified by:
getObjectin interfaceorg.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
获取并删除键值对,当指定键对应值非空时,返回并删除这个值,后边的键对应的值不再查找- Parameters:
keys- 键列表,常用于别名- Returns:
- 字符串值
-
getSubProps
获取一个新的子属性,子属性键值对拥有公共前缀,以.分隔。a.b a.c b.a则调用getSubProps("a");得到a.b a.c- Parameters:
prefix- 前缀,可以不以.结尾- Returns:
- 子属性
-
toProperties
转换为标准的Props对象- Returns:
Props对象
-
toBean
将配置文件转换为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
将配置文件转换为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
将配置文件转换为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
设置值,无给定键创建之。设置后未持久化- Parameters:
key- 属性键value- 属性值
-
setFields
通过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
持久化当前设置,会覆盖掉之前的设置- Parameters:
absolutePath- 设置文件的绝对路径- Throws:
org.miaixz.bus.core.lang.exception.InternalException- IO异常,可能为文件未找到
-
store
存储当前设置,会覆盖掉以前的设置- Parameters:
path- 相对路径clazz- 相对的类
-