Java配置框架
我正在清除 Java 库中的所有硬编码值,并且想知道哪种框架最适合处理运行时配置(就零或接近零配置而言)? 我更喜欢基于 XML 的配置文件,但这不是必需的。
仅当您有框架的实际经验时才回复。 我不是在寻找例子,而是在寻找经验......
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我正在清除 Java 库中的所有硬编码值,并且想知道哪种框架最适合处理运行时配置(就零或接近零配置而言)? 我更喜欢基于 XML 的配置文件,但这不是必需的。
仅当您有框架的实际经验时才回复。 我不是在寻找例子,而是在寻找经验......
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(14)
Commons 配置
我们正在使用它。 单独的属性文件更容易处理,但如果您需要表示更复杂的数据公共配置可以做到这一点并读取您的属性文件。
如果您不做任何复杂的事情,我会坚持使用属性文件。
Commons Configuration
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.
如果您想做一些高级(并且类型安全)的事情,您可能需要看看这个:http://www.ibm.com/developerworks/java/library/j-configint/index.html
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
我倾向于使用
java.util.Properties
(或其他语言和框架中的类似类)大多数时候包装在特定于应用程序的配置类中,但我对此的替代方案或变体非常感兴趣。 特别是如果涉及图形配置对话框或配置数据的多个视图,事情可能会变得有点棘手。不幸的是,我对 Java 的特定库没有任何经验(除了我自己编写的库),但任何指示将不胜感激。
更新
好的。 这并不完全正确,三是 Spring Java 配置项目。
I tend to use
java.util.Properties
(or similar classes in other languages and frameworks) wrapped in an application-specific configuration class most 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.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.
Update
OK. That wasn't entirely true, three is the Spring Java Configuration Project.
我几周前写过关于这个问题并得出了结论XML 是使用最广泛的符号之一。
这是最好的吗? 我不这么认为,我真的很喜欢 JSON,但工具仍然达不到 XML,所以我想我们必须拭目以待。
I wrote about this a couple of weeks ago and came to the conclusion that XML is one of the most widely used notations.
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.
您可以尝试 YamlBeans。 通过这种方式,您可以编写想要保存配置数据的任何类,然后您可以自动在 YAML 中写入和读取它们。
YAML 是一种人类可读的数据格式。 它比 java.util.Properties 具有更强的表达能力。 您可以拥有列表、地图、锚点、键入的数据等。
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.
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.
请看一下这个网址:
http://issues.apache.org/jira/browse/CONFIGURATION-394
我们正在寻找的配置框架是 Apache Commons 配置之上的东西,并且必须支持并发问题、JMX 问题和大多数存储(例如 .properties 文件、.xml 文件或 PreferencesAPI)。
weblogic 团队在“管理控制台”上提供的内容很有趣,通过它您可以对配置进行事务(原子)更新,以便通知注册的侦听器。
Apache 的家伙坚持认为这个项目超出了 Commons Configuration 的范围,也许吧!
我附上了一个简单的配置框架,请看一下。
Please take a look at this URL:
http://issues.apache.org/jira/browse/CONFIGURATION-394
The Configuration framework which we're looking for it is something on top of Apache Commons Configuration and must support Concurrency Issues, JMX issues and most of stores(e.g .properties file, .xml files or PreferencesAPI).
What weblogic team provides on 'Administration Console' is intersting which through it you can have transactional(atomic) updates on configurations so that are registered listeners be notified.
The Apache guys insist that this project is out of scopes of Commons Configuration, maybe!
I've attached a simple configuration framework, take look please.
我刚刚发布了一些关于使用 Spring 的 ClassPathResource 作为IoC 的替代方案。 ClassPathResource 允许您将属性文件放置在类路径上的任何位置(例如,全部放在一个位置,或者作为它们配置的代码的对等体)。我的示例仅使用 java.util.Properties,因此您可以使用纯文本“name=value”样式或其 XML 格式。
I just posted a brief bit of code about using Spring's ClassPathResource as an alternative to IoC. ClassPathResource permits you to place property files anywhere on the classpath (e.g., all in one place, or as peers to the code they configure. My example just uses java.util.Properties, so you can use the plaintext "name=value" style or its XML format.
属性文件非常简单,如果您需要更多功能,您可以将一些配置文件格式化为 Java 类。 它们可以放置在不同的包/模块中,并且可以使用 BeanShell 等库在运行时进行预编译或加载。
注意:在最简单的情况下(预编译),您不需要任何额外的库。
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.
Note: In the simplest case (pre-compiled) you don't need any additional libraries.
关于使用 java.util.Properties 的建议 - 从 jdk 1.5 开始,首选项 API (java.util.prefs) 似乎是使用 Properties API 的首选替代方案。
原因:可扩展性提高、后端中立性等。
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.
Reasons: increased scalability, back-end neutrality, ect.
您可以查看新发布的tools4j-config,其使命宣言是为了让你在运行时轻松处理配置。
You could have a look at newly announced tools4j-config whose mission statement is to allow you to easily handle configuration at runtime.
智能参数利用工具(InPUT,页面)允许外部化几乎任何(硬编码)决策作为参数写入基于 XML 的配置文件。它于 2012 年初启动,作为对现有配置工具在通用性和关注点分离方面的缺陷的回应。
InPUT 可能比大多数用例所需的功能更强大,因为它允许对实验数据(输入 - 输出)进行独立于编程语言的表述,并具有诸如复杂描述符到类的定义等功能映射,或基于预定义值范围的随机配置生成和验证(用于测试和研究,例如蒙特卡罗模拟)。 您可以定义带有子参数的参数、参数值的相对限制(数字参数a > 参数b)等。
它仍处于测试阶段,但相当稳定,我将其用于我的研究、实验配置和记录以及教学目的。 一旦它可用于其他语言(管道中的 C++ 适配器),其他研究人员/从业者就可以重用在 C++ 中运行相同算法实现的描述符(使用代码映射概念)。 这样,可以验证实验结果/可以更轻松地迁移程序。 该文档仍在工作过程中,但页面上有几个示例。 InPUT 是开源软件。
对于那些感兴趣的人,请参阅概念研究论文。
the Intelligent Parameter Utilization Tool (InPUT, page) allows to externalize almost any (hard coded) decision as a parameter into an XML based configuration file. It has been initiated in early 2012 as a response to the perceived deficiencies in existing configuration tools with respect to generality, and separation of concerns.
InPUT is probably more powerful than most use cases require, as it allows for the programming language independent formulation of experimental data (input - output), with features such as the definition of complex descriptor to class mappings, or randomized configuration spawning and validation based on predefined value ranges (for test and research, e.g. Monte Carlo simulations). You can define parameters with sub parameters, relative restrictions on parameter values (numerical param a > param b) etc. .
Its still in beta, but rather stable, I use it for my research, for the configuration and documentation of experiments, and for teaching purposes. Once it is available for other languages (C++ adapter in the pipe), other researchers/practitioners can reuse the descriptors running their implementations of the same algorithms in C++ (using the code mapping concept). That way, experimental results can be validated/programs can be migrated more easily. The documentation is still in working process, but a couple of examples are available on the page. InPUT is open source software.
For those interested, the Conceptual Research Paper.
以下是各种选项:
您可能想要阅读Commons 配置与 JFig 和 JConfig 的比较以及使用 JFig 配置您的应用程序以获取来自不同用户的一些反馈。
就我个人而言,我使用过 jConfig,这是一次很好的体验。
Here are various options:
You might want to read Comparison of Commons Configuration With JFig and JConfig and Configuring your Applications using JFig for some feedback from various users.
Personally, I've used jConfig and it was a good experience.
如果您的硬编码值只是简单的键值对,您应该查看 java.util.Properties。 它比 xml 简单得多,更容易使用,而且实现起来也非常简单。
如果您正在使用 Java 并且您正在存储或从磁盘检索的数据被建模为键值对(听起来就像您的情况),那么我真的无法想象更好的解决方案。
我已经使用属性文件在较大的项目中对小包进行简单配置,并作为整个项目的更全局的配置,并且我从未遇到过问题。
当然,这有一个巨大的好处,那就是不需要使用任何第三方库。
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.
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.
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.
Apache Commons 配置效果很好。 它支持在后端以多种格式存储配置,包括属性、XML、JNDI 等。 它易于使用和扩展。 为了获得最大的灵活性,请使用 factory 来获取配置,只需使用 <之后是 href="http://commons.apache.org/configuration/apidocs/org/apache/commons/configuration/Configuration.html" rel="nofollow noreferrer">配置界面。
Commons Configuration 与直接属性文件的区别在于它支持自动转换为常见类型(int、float、String 数组),并且支持属性替换:
Apache Commons Configuration works 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 factory to get the configuration and just use the Configuration interface after that.
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: