java:Preferences API与Apache Commons配置

Jas*_*n S 6 java configuration apache-commons

我需要允许用户存储/加载任意数量的对象列表(假设它们是Serializable).从概念上讲,我想要一个像数据模型

class FooBean { /* bean stuff here */ }

class FooList {
    final private Set<FooBean> items = new HashSet<FooBean>();

    public boolean add(FooBean item) { return items.add(item); }
    public boolean remove(FooBean item) { return items.remove(item); }
    public Collection<FooBean> getItems() { 
       return Collections.unmodifiableSet(items); 
    }
}

class FooStore {
    public FooStore() {
       /* something... uses Preferences or Commons Configuration */ 
    }
    public FooList load(String key) {
       /* something... retrieves a FooList associated with the key */ 
    }
    public void store(String key, FooList items) { 
       /* something... saves a FooList under the given key */
    }
}
Run Code Online (Sandbox Code Playgroud)

我应该使用Preferences API还是Commons Config?每个人的优势是什么?

Rid*_*del 6

好吧,commons-configuration和许多apache项目一样,是一个抽象层,允许一个人无限地使用首选项,一个ldap存储,属性文件等等.因此,您的问题可以按原样重写:您是否需要更改用于存储首选项的格式?如果不是,那么java偏好是可行的方法.在其他地方,考虑公共配置的可移植性.


Kan*_*ath 1

我通常会使用 Preferences API,它是 JDK 的一部分,除非存在可以通过 commons-config 解决的问题。

就我个人而言,当我使用 spring 时,它有一个属性配置器,它可以为我完成大部分工作。