- 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>
A wrapper class for reading and handling
.properties files. It extends java.util.Properties with
additional convenience methods for typed data retrieval, automatic reloading, and bean mapping.- Since:
- Java 17+
- Author:
- Kimi Liu
- See Also:
-
Field Summary
Fields inherited from class java.util.Properties
defaults -
Constructor Summary
ConstructorsConstructorDescriptionProps()Constructs a new, emptyPropsobject.Constructs aPropsobject from aFile.Constructs aPropsobject from aFilewith a specified charset.Constructs aPropsobject by loading a properties file from the classpath.Constructs aPropsobject by loading a properties file with a specified charset.Props(Properties properties) Constructs aPropsobject from an existingPropertiesobject.Constructs aPropsobject from aResourcewith a specified charset. -
Method Summary
Modifier and TypeMethodDescriptionvoidautoLoad(boolean autoReload) Enables or disables automatic reloading of the properties file when it changes on the filesystem.static PropsGets a cachedPropsinstance for a given resource name.<P,T> T get(org.miaixz.bus.core.center.function.FunctionX<P, T> func) Gets a value using a lambda method reference to resolve the property name and return type.getAndRemoveString(String... keys) Gets and removes a property value.static PropsgetFirstFound(String... names) Gets the firstPropsinstance that can be successfully loaded from a list of resource names.getObject(CharSequence key, Object defaultValue) static PropsGets aPropsinstance containing the current system properties.getSubProps(String prefix) Extracts a subset of properties that share a common prefix.voidload()Reloads the properties from the original resource.voidInitializes thisPropsobject by loading from the givenURL.voidload(org.miaixz.bus.core.io.resource.Resource resource) Initializes thisPropsobject by loading from the givenResource.static Propsof()Creates a new, emptyPropsobject.static PropsCreates aPropsobject by loading a file from the classpath.static PropsCreates aPropsobject by loading a file from the classpath with a specified charset.static PropsCreates a copy of an existingPropsobject.static voidParses a string content in.propertiesformat and populates a map.voidSets a property value.setFields(org.miaixz.bus.core.center.function.SupplierX<?>... fields) Sets multiple properties using an array of lambda method reference suppliers.voidStores the current properties to a file at the specified absolute path, overwriting its previous content.voidStores the current properties to a file path relative to a given class.<T> TMaps the properties to a new Java Bean object.<T> TMaps a subset of properties (filtered by a prefix) to a new Java Bean object.<T> TtoBean(T bean) Maps the properties to an existing Java Bean object.<T> TMaps a subset of properties (filtered by a prefix) to an existing Java Bean object.Creates a newPropsinstance containing all the properties from this one.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, getBoolean, getBoolean, 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()Constructs a new, emptyPropsobject. -
Props
Constructs aPropsobject by loading a properties file from the classpath.- Parameters:
path- The path to the properties file, relative to the classpath root, or an absolute path.
-
Props
Constructs aPropsobject by loading a properties file with a specified charset.- Parameters:
path- The path to the properties file (relative or absolute).charset- The character set to use for reading the file.
-
Props
Constructs aPropsobject from aFile.- Parameters:
propertiesFile- The properties file.
-
Props
Constructs aPropsobject from aFilewith a specified charset.- Parameters:
propertiesFile- The properties file.charset- The character set to use.
-
Props
Constructs aPropsobject from aResourcewith a specified charset.- Parameters:
resource- The resource representing the properties file.charset- The character set to use.
-
Props
Constructs aPropsobject from an existingPropertiesobject.- Parameters:
properties- The properties to copy.
-
-
Method Details
-
of
Creates a new, emptyPropsobject.- Returns:
- A new
Propsinstance.
-
of
Creates aPropsobject by loading a file from the classpath.- Parameters:
resource- The path to the resource, relative to the classpath root.- Returns:
- A new
Propsinstance.
-
of
Creates aPropsobject by loading a file from the classpath with a specified charset.- Parameters:
resource- The path to the resource.charset- The character set to use.- Returns:
- A new
Propsinstance.
-
of
Creates a copy of an existingPropsobject.- Parameters:
properties- ThePropsobject to copy.- Returns:
- A new
Propsinstance.
-
get
Gets a cachedPropsinstance for a given resource name. If the name has no extension,.propertiesis assumed.- Parameters:
name- The name of the properties file.- Returns:
- A cached or new
Propsinstance.
-
parse
Parses a string content in.propertiesformat and populates a map.- Parameters:
result- The map to populate with the parsed key-value pairs.content- The string content to parse.
-
getFirstFound
Gets the firstPropsinstance that can be successfully loaded from a list of resource names.- Parameters:
names- The resource names to try.- Returns:
- The first found
Propsinstance, or null if none are found.
-
getProperties
Gets aPropsinstance containing the current system properties.- Returns:
- A new
Propsinstance with system properties.
-
load
Initializes thisPropsobject by loading from the givenURL.- Parameters:
url- The URL of the properties file.
-
load
public void load(org.miaixz.bus.core.io.resource.Resource resource) Initializes thisPropsobject by loading from the givenResource.- Parameters:
resource- The resource to load.
-
load
public void load()Reloads the properties from the original resource. -
autoLoad
public void autoLoad(boolean autoReload) Enables or disables automatic reloading of the properties file when it changes on the filesystem.- Parameters:
autoReload-trueto enable auto-reloading,falseto disable it.
-
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) Gets a value using a lambda method reference to resolve the property name and return type.- Type Parameters:
P- The type of the class containing the method.T- The return type of the method.- Parameters:
func- The method reference (e.g.,Config::getUsername).- Returns:
- The property value converted to the specified type.
-
getAndRemoveString
Gets and removes a property value. It tries each key in the provided list until a non-null value is found, which is then returned and removed.- Parameters:
keys- A list of keys to try, often used for aliases.- Returns:
- The string value, or null if no key is found.
-
getSubProps
Extracts a subset of properties that share a common prefix. The prefix is removed from the keys in the resultingPropsobject.Example:
a.b = 1 a.c = 2 b.a = 3
CallinggetSubProps("a")would return aPropsobject containing:b = 1 c = 2
- Parameters:
prefix- The prefix to match. A dot is automatically appended if not present.- Returns:
- A new
Propsobject containing the subset of properties.
-
toProperties
Creates a newPropsinstance containing all the properties from this one.- Returns:
- A new
Propsinstance.
-
toBean
Maps the properties to a new Java Bean object. Supports nested properties.- Type Parameters:
T- The type of the bean.- Parameters:
beanClass- The class of the Java Bean to create and populate.- Returns:
- The newly created and populated bean object.
-
toBean
Maps a subset of properties (filtered by a prefix) to a new Java Bean object.- Type Parameters:
T- The type of the bean.- Parameters:
beanClass- The class of the Java Bean to create and populate.prefix- The prefix to filter properties by. Only properties starting with this prefix will be mapped.- Returns:
- The newly created and populated bean object.
-
toBean
public <T> T toBean(T bean) Maps the properties to an existing Java Bean object.- Type Parameters:
T- The type of the bean.- Parameters:
bean- The Java Bean object to populate.- Returns:
- The populated bean object.
-
toBean
Maps a subset of properties (filtered by a prefix) to an existing Java Bean object.- Type Parameters:
T- The type of the bean.- Parameters:
bean- The Java Bean object to populate.prefix- The prefix to filter properties by.- Returns:
- The populated bean object.
-
set
Sets a property value. If the key does not exist, it is created. This change is not persisted to the file untilstore(String)is called.- Parameters:
key- The property key.value- The property value.
-
setFields
Sets multiple properties using an array of lambda method reference suppliers. This is useful for setting properties from a bean's getters.Example:
User user = new User("test", "Test User"); Props.of().setFields(user::getUsername, user::getNickname);- Parameters:
fields- An array of suppliers, where each supplier returns a property value.- Returns:
- This
Propsinstance for chaining.
-
store
Stores the current properties to a file at the specified absolute path, overwriting its previous content.- Parameters:
absolutePath- The absolute path to the destination file.- Throws:
org.miaixz.bus.core.lang.exception.InternalException- if an I/O error occurs.
-
store
Stores the current properties to a file path relative to a given class.- Parameters:
path- The path relative to the class.clazz- The class to which the path is relative.
-