我试图java.util.Properties在我的类中有一个默认对象,它接受默认属性,让开发人员通过指定另一个java.util.Properties对象来覆盖其中一些,但我找不到一个很好的方法.
预期用途如下:
Properties defaultProperties = new Properties();
defaultProperties.put("key1", "value1");
defaultProperties.put("key2", "value2");
Properties otherProperties = new Properties();
otherProperties.put("key2", "value3");
Properties finalProperties = new Properties(defaultProperties);
//
// I'd expect to have something like:
//
// finalProperties.merge(otherProperties);
//
Run Code Online (Sandbox Code Playgroud) 我的目标是Properties在我的类中有一个私有静态对象,在创建Properties我的应用程序所需的其他对象时充当默认对象.当前的实现如下所示:
public class MyClass {
private static Properties DEFAULT_PROPERTIES = new Properties();
static {
try {
DEFAULT_PROPERTIES.load(
MyClass.class.getResourceAsStream("myclass.properties"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
看着它,它有效,但感觉不对.
你会怎么做?