java:首选项 API 与 Apache Commons 配置

发布于 2024-08-23 09:27:51 字数 1061 浏览 5 评论 0原文

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

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

我应该使用 Preferences API 还是 Commons Config?各自的优点是什么?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

意中人 2024-08-30 09:27:52

Commons Configuration 不适合存储复杂的对象结构。你最好使用序列化框架。

Commons Configuration is not suited for storing complex objects structures. You'd better use a serialization framework.

彡翼 2024-08-30 09:27:51

好吧,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.

东走西顾 2024-08-30 09:27:51

鉴于存储与键关联的一组值的示例,在使用每个库时,您似乎有以下选项

  • 首选项 - 存储为与键关联的字节数组
  • Commons 配置 - 存储为与键关联的字符串列表

所以选择可以归结为将 FooBean 转换为字节数组还是字符串是否更容易。

Commons Configuration 的另一个优点是不同的后端。我用它来将属性存储在数据库中。如果您想将对象存储在用户本地计算机之外的其他位置,那么这将是更好的选择。

Given your example of storing a set of values associated with a key you would seem to have the following options when using each library

  • Preferences - store as a byte array associated with the key
  • Commons Configuration - store as a list of strings associated with a key

So the choice could come down to whether it is easier to convert a FooBean into a byte array or a String.

The other advantage of Commons Configuration is the different backends. I have used it to store properties in a database. If you want to store the objects somewhere other than the users local machine it would be the better choice.

深白境迁sunset 2024-08-30 09:27:51

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

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

I would normally go with Preferences API which is part of JDK unless there are issues which are otherwise solved by commons-config.

Personally when I use spring it has a Property Configurer which does most of these for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文