java:首选项 API 与 Apache Commons 配置 [英] java: Preferences API vs. Apache Commons Configuration

查看:30
本文介绍了java:首选项 API 与 Apache Commons 配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to allow the user to store/load an arbitrary number of lists of objects (assume they are Serializable). Conceptually I want a data model like

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 */
    }
}

我是否应该使用 首选项 API公共配置?各自的优势是什么?

Should I use the Preferences API or Commons Config? What's the advantages of each?

推荐答案

好吧,commons-configuration 就像许多 apache 项目一样,是一个抽象层,允许人们无缝地使用首选项、ldap 存储、属性文件等.因此,您的问题可以改写为:您是否需要更改用于存储首选项的格式?如果没有,java首选项是要走的路.在其他地方,请考虑公共配置的可移植性.

Well, commons-configuration is, like many of apache project, an abstraction layer allowing one to seemlessly use preferences, an ldap store, properties files, and so on. So, your question could be rewritten as is : Will you need to change the format you use to store your preferences ? If no, java preferences are the way to go. Elsewhere, consider the portability of commons configuration.

这篇关于java:首选项 API 与 Apache Commons 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆