Commons Configuration AbstractConfiguration 的 get() 和 getArray() 有什么问题?
What's wrong with Commons Configuration AbstractConfiguration's get() and getArray()?
使用 commons-configuration2:2.4
.
my.properties
:
arrayOfSingle=0xA
arrayOfMultiples=0xD,0xA
CONFIG.getArray(int[].class, "arrayOfSingle"));
org.apache.commons.configuration2.ex.ConversionException:
The value '0xA' (class java.lang.String) can't be converted to a [I object
CONFIG.getArray(int[].class, "arrayOfMultiples"));
org.apache.commons.configuration2.ex.ConversionException:
The value '0xD,0xA' (class java.lang.String) can't be converted to a [I object
CONFIG.getArray(Integer[].class, "arrayOfSingle"));
org.apache.commons.configuration2.ex.ConversionException:
The value '0xA' (class java.lang.String) can't be converted to a [Ljava.lang.Integer; object
CONFIG.getArray(Integer[].class, "arrayOfMultiples"));
org.apache.commons.configuration2.ex.ConversionException:
The value '0xD,0xA' (class java.lang.String) can't be converted to a [Ljava.lang.Integer; object
CONFIG.get(int[].class, "arrayOfSingle") // prints "[I@7dba7035", i.e.works
CONFIG.get(int[].class, "arrayOfMultiples")
org.apache.commons.configuration2.ex.ConversionException:
Could not convert 0xD,0xA to java.lang.Integer! Invalid hex number.
CONFIG.get(Integer[].class, "arrayOfSingle") // prints "[Ljava.lang.Integer;@44d379bb", i.e. works
CONFIG.get(Integer[].class, "arrayOfMultiples")
org.apache.commons.configuration2.ex.ConversionException:
Could not convert 0xD,0xA to java.lang.Integer! Invalid hex number.
getArray(Class<?> cls, String key, Object defaultValue)
已弃用(建议使用 get(Class<T> cls, String key, T defaultValue)
)但 getArray(Class<?> cls, String key)
未弃用并且 get(Class<T> cls, String key)
也不起作用。
放弃 getArray()
并使用 Lists and arrays 中描述的多行变体。
my.properties
:
arrayOfMultiples=0xD
arrayOfMultiples=0xA
CONFIG.get(int[].class, "arrayOfMultiples")
CONFIG.get(Integer[].class, "arrayOfMultiples")
我希望这对那些想要如下所示以逗号分隔属性并获得值列表或数组的人有用。
即使 也被接受,但如果您不喜欢同一键的多个条目,则以下解决方案有效。
添加依赖项:
compile("org.apache.commons:commons-configuration2:2.7")
application.properties 文件:
arrayOfMultiples=0xD,0xA
客户:
PropertiesConfiguration propertiesConfiguration = new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class)
.configure(new Parameters()
.properties()
.setFileName("application.properties-file-path")
.setListDelimiterHandler(new DefaultListDelimiterHandler(',')))
.getConfiguration();
输出:
propertiesConfiguration.get(Integer[].class, "arrayOfMultiples");
// or
propertiesConfiguration.get(int[].class, "arrayOfMultiples"))
[13, 10]
注意:由于值是十六进制的,转换器会将其转换为十进制。但适用于任何 int 值。
当您需要列表时,也适用于以下类型的读取
propertiesConfiguration.getList(int.class, "arrayOfMultiples");
// or
propertiesConfiguration.getList(Integer.class, "arrayOfMultiples");
使用 commons-configuration2:2.4
.
my.properties
:
arrayOfSingle=0xA
arrayOfMultiples=0xD,0xA
CONFIG.getArray(int[].class, "arrayOfSingle"));
org.apache.commons.configuration2.ex.ConversionException:
The value '0xA' (class java.lang.String) can't be converted to a [I object
CONFIG.getArray(int[].class, "arrayOfMultiples"));
org.apache.commons.configuration2.ex.ConversionException:
The value '0xD,0xA' (class java.lang.String) can't be converted to a [I object
CONFIG.getArray(Integer[].class, "arrayOfSingle"));
org.apache.commons.configuration2.ex.ConversionException:
The value '0xA' (class java.lang.String) can't be converted to a [Ljava.lang.Integer; object
CONFIG.getArray(Integer[].class, "arrayOfMultiples"));
org.apache.commons.configuration2.ex.ConversionException:
The value '0xD,0xA' (class java.lang.String) can't be converted to a [Ljava.lang.Integer; object
CONFIG.get(int[].class, "arrayOfSingle") // prints "[I@7dba7035", i.e.works
CONFIG.get(int[].class, "arrayOfMultiples")
org.apache.commons.configuration2.ex.ConversionException:
Could not convert 0xD,0xA to java.lang.Integer! Invalid hex number.
CONFIG.get(Integer[].class, "arrayOfSingle") // prints "[Ljava.lang.Integer;@44d379bb", i.e. works
CONFIG.get(Integer[].class, "arrayOfMultiples")
org.apache.commons.configuration2.ex.ConversionException:
Could not convert 0xD,0xA to java.lang.Integer! Invalid hex number.
getArray(Class<?> cls, String key, Object defaultValue)
已弃用(建议使用 get(Class<T> cls, String key, T defaultValue)
)但 getArray(Class<?> cls, String key)
未弃用并且 get(Class<T> cls, String key)
也不起作用。
放弃 getArray()
并使用 Lists and arrays 中描述的多行变体。
my.properties
:
arrayOfMultiples=0xD
arrayOfMultiples=0xA
CONFIG.get(int[].class, "arrayOfMultiples")
CONFIG.get(Integer[].class, "arrayOfMultiples")
我希望这对那些想要如下所示以逗号分隔属性并获得值列表或数组的人有用。
即使
添加依赖项:
compile("org.apache.commons:commons-configuration2:2.7")
application.properties 文件:
arrayOfMultiples=0xD,0xA
客户:
PropertiesConfiguration propertiesConfiguration = new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class)
.configure(new Parameters()
.properties()
.setFileName("application.properties-file-path")
.setListDelimiterHandler(new DefaultListDelimiterHandler(',')))
.getConfiguration();
输出:
propertiesConfiguration.get(Integer[].class, "arrayOfMultiples");
// or
propertiesConfiguration.get(int[].class, "arrayOfMultiples"))
[13, 10]
注意:由于值是十六进制的,转换器会将其转换为十进制。但适用于任何 int 值。
当您需要列表时,也适用于以下类型的读取
propertiesConfiguration.getList(int.class, "arrayOfMultiples");
// or
propertiesConfiguration.getList(Integer.class, "arrayOfMultiples");