Java配置框架
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java configuration framework
提问by Steen
I'm in the process of weeding out all hardcoded values in a Java library and was wondering what framework would be the best (in terms of zero- or close-to-zero configuration) to handle run-time configuration? I would prefer XML-based configuration files, but it's not essential.
我正在清除 Java 库中的所有硬编码值,并且想知道哪种框架是处理运行时配置的最佳框架(就零配置或接近零配置而言)?我更喜欢基于 XML 的配置文件,但这不是必需的。
Please do only reply if you have practical experience with a framework. I'm not looking for examples, but experience...
请仅在您对框架有实际经验时才回复。我不是在寻找例子,而是在寻找经验......
采纳答案by Mike Stone
If your hardcoded values are just simple key-value pairs, you should look at java.util.Properties. It's a lot simpler than xml, easier to use, and mind-numbingly trivial to implement.
如果您的硬编码值只是简单的键值对,您应该查看java.util.Properties。它比 xml 简单得多,更容易使用,而且实现起来非常简单。
If you are working with Java and the data you are storing or retrieving from disk is modeled as a key value pair (which it sounds like it is in your case), then I really can't imagine a better solution.
如果您正在使用 Java,并且您正在存储或从磁盘检索的数据被建模为键值对(这听起来像是您的情况),那么我真的无法想象更好的解决方案。
I have used properties files for simple configuration of small packages in a bigger project, and as a more global configuration for a whole project, and I have never had problems with it.
我曾使用属性文件在较大的项目中对小包进行简单配置,并将其用作整个项目的全局配置,而且我从未遇到过问题。
Of course this has the huge benefit of not requiring any 3rd party libraries to utilize.
当然,这具有不需要任何 3rd 方库来使用的巨大好处。
回答by Anders Sandvig
I tend to use java.util.Properties
(or similar classes in other languages and frameworks) wrapped in an application-specific configuration classmost of the time, but I am very interested in alternatives or variations on this. Especially since things can become a bit tricky if graphical configuration dialogs or multiple views on the configuration data is involved.
大多数时候,我倾向于使用java.util.Properties
(或其他语言和框架中的类似类)包装在特定于应用程序的配置类中,但我对此的替代方案或变体非常感兴趣。特别是因为如果涉及图形配置对话框或配置数据的多个视图,事情会变得有点棘手。
Unfortunately I don't have any experience with specific libraries for Java (except with the ones I have written myself), but any pointers would be appreciated.
不幸的是,我对 Java 的特定库没有任何经验(除了我自己编写的库),但任何指针都将不胜感激。
Update
更新
OK. That wasn't entirely true, three is the Spring Java Configuration Project.
好的。这并不完全正确,三是Spring Java 配置项目。
回答by dlinsin
I wroteabout this a couple of weeks ago and came to the conclusion that XML is one of the most widely used notations.
几周前我写过这篇文章,得出的结论是 XML 是使用最广泛的表示法之一。
Is it the best? I don't think so, I really like JSON, but the tooling is still not up to XML so I guess we have to wait and see.
它是最好的吗?我不这么认为,我真的很喜欢 JSON,但工具仍然不符合 XML,所以我想我们必须拭目以待。
回答by ScArcher2
We're using this. Properties files alone are much easier to handle, but if you need to represent more complex data commons configuration can do this and read your properties files as well.
我们正在使用这个。单独的属性文件更容易处理,但是如果您需要表示更复杂的数据公共配置,则可以执行此操作并读取您的属性文件。
If you aren't doing anything complicated I'd stick to properites files.
如果你不做任何复杂的事情,我会坚持使用属性文件。
回答by John Meagher
Apache Commons Configurationworks great. It supports having the configuration stored in a wide range of formats on the backend including properties, XML, JNDI, and more. It is easy to use and to extend. To get the most flexibility out of it use a factoryto get the configuration and just use the Configuration interfaceafter that.
Apache Commons 配置效果很好。它支持在后端以多种格式存储配置,包括属性、XML、JNDI 等。它易于使用和扩展。为了获得最大的灵活性,请使用工厂获取配置,然后使用配置界面。
Two feature of Commons Configuration that differentiate it over a straight Properties file is that it support automatic conversion to common types (int, float, String arrays) and it supports property substitution:
Commons Configuration 的两个区别于直接属性文件的特性是它支持自动转换为常见类型(int、float、String 数组),并且支持属性替换:
server.host=myHost
server.url=http://${server.host}/somePath
回答by lowellk
If you want to do something advanced (and typesafe), you might want to take a look at this: http://www.ibm.com/developerworks/java/library/j-configint/index.html
如果你想做一些高级的(和类型安全的),你可能想看看这个:http: //www.ibm.com/developerworks/java/library/j-configint/index.html
回答by Peter Lawrey
Properties files a very simple, if you need something more functional, you could format some of your configuration files as Java classes. These can be placed in a different package/module and can be pre-compiled or loaded at runtime with a library like BeanShell.
属性文件非常简单,如果您需要更多功能,您可以将一些配置文件格式化为 Java 类。这些可以放在不同的包/模块中,并且可以在运行时使用 BeanShell 之类的库进行预编译或加载。
Note: In the simplest case (pre-compiled) you don't need any additional libraries.
注意:在最简单的情况下(预编译),您不需要任何额外的库。
回答by Peter Lawrey
Regarding the suggestions to use java.util.Properties - starting in jdk 1.5, the Preferences API (java.util.prefs) appears to be the preferred alternative to using the Properties API.
关于使用 java.util.Properties 的建议 - 从 jdk 1.5 开始,Preferences API (java.util.prefs) 似乎是使用 Properties API 的首选替代方案。
Reasons: increased scalability, back-end neutrality, ect.
原因:增加可扩展性、后端中立性等。
回答by Pascal Thivent
Here are various options:
这里有多种选择:
- java.util.Properties
- java.util.prefs.Preferences (since Java 5)
- Commons Configuration
- jConfig
- JFig
- Carbon's Configuration Service
- java.util.Properties
- java.util.prefs.Preferences(从 Java 5 开始)
- 公共配置
- 配置文件
- 无花果
- Carbon的配置服务
You might want to read Comparison of Commons Configuration With JFig and JConfigand Configuring your Applications using JFigfor some feedback from various users.
你可能想读共享配置使用JFig和JConfig的比较和使用JFig配置您的应用程序从各种用户的一些反馈。
Personally, I've used jConfig and it was a good experience.
就个人而言,我使用过 jConfig,这是一次很好的体验。
回答by NateS
You can try YamlBeans. This way you write whatever classes you want to hold your config data, then you can automatically write and read them to and from YAML.
你可以试试YamlBeans。通过这种方式,您可以编写任何想要保存配置数据的类,然后您可以自动将它们写入和读取到 YAML。
YAML is a human readable data format. It has more expressive power than java.util.Properties. You can have lists, maps, anchors, typed data, etc.
YAML 是一种人类可读的数据格式。它比 java.util.Properties 具有更强的表现力。您可以拥有列表、地图、锚点、类型数据等。