处理复杂配置的可能的轻量级 Java 配置库有哪些?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5495937/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:23:52  来源:igfitidea点击:

What are possible lightweight Java configuration libraries for handling complex configuration?

javaspringconfiguration-files

提问by Nakedible

I have a need for processing a relatively complex set of configuration parameters for a java application. The requirements are roughly:

我需要为 Java 应用程序处理一组相对复杂的配置参数。要求大致如下:

  • Nested configuration values with lists, maps, etc. - not just plain key/value pairs
  • Multiple configuration files where later configuration files can intelligently override settings from earlier configuration files.
  • Multiple references to the same configured item from different places
  • Inheritable configured objects, so anew object can copy configuration from a previous object can just change specific things
  • Very little code needed for a new configuration options - the optimal would be to just add an @Configurable annotation to a field, or something similar
  • 带有列表、映射等的嵌套配置值——不仅仅是普通的键/值对
  • 多个配置文件,其中以后的配置文件可以智能地覆盖早期配置文件中的设置。
  • 从不同地方多次引用同一个配置项
  • 可继承的配置对象,因此新对象可以从以前的对象复制配置,只需更改特定的内容
  • 新配置选项所需的代码很少 - 最佳方式是将 @Configurable 注释添加到字段或类似的东西

Now, I know that Spring fulfills all of these in a certain way. However, it has some disadvantages, though none fatal:

现在,我知道 Spring 以某种方式实现了所有这些。然而,它有一些缺点,虽然不是致命的:

  • Spring is not really optimized as a configuration language, as it is somewhat more concerned about dependency injection
  • The XML configuration files are more meant to be shipped inside a JAR file and not modified by the end user, with all the configurable properties referenced via a separate properties file or similar - where as I need the configuration file to be complete and easily end-user modifiable
  • Spring configuration is somewhat tedious when there are online configuration refreshes without terminating ongoing connections and when there needs to be separate configuration checking primitives that need to validate a configuration fully, but not actually do anything
  • Spring 并没有真正优化为一种配置语言,因为它更关心依赖注入
  • XML 配置文件更倾向于在 JAR 文件中传送,而不是由最终用户修改,所有可配置属性都通过单独的属性文件或类似文件引用 - 因为我需要配置文件完整且易于结束 -用户可修改
  • 当在线配置刷新而不终止正在进行的连接以及需要单独的配置检查原语需要完全验证配置但实际上不做任何事情时,Spring 配置有点乏味

So, I am asking, can you think of any alternatives that would fulfill my requirements?

所以,我问,你能想到任何可以满足我要求的替代方案吗?

I sort of like YamlBeans, but it is lacking a few features.

我有点喜欢 YamlBeans,但它缺少一些功能。

采纳答案by Thomas

Have you already had a look at Apache Commons Configuration?

您是否已经看过Apache Commons 配置

回答by Nathan Perrier

This one looks interesting, though I have to admit I haven't used it:

这个看起来很有趣,但我不得不承认我没有使用过它:

http://owner.aeonbits.org

http://owner.aeonbits.org

回答by smat

We use Constrettoat our project. Very lightweight and easy to use.

我们在我们的项目中使用Constretto。非常轻巧且易于使用。

回答by Norbert

You could give cfg4ja try. It's mostly oriented towards distributed apps but supports most of the requested features.

你可以试试cfg4j。它主要面向分布式应用程序,但支持大多数请求的功能。

回答by AngerClown

For most of the requirements, I have been able to accomplish this with standard Properties. I wrapped a Properties object in a configuration class that read properties file from:

对于大多数要求,我已经能够使用标准属性来实现这一点。我将一个 Properties 对象包装在一个配置类中,该类从以下位置读取属性文件:

  1. a common jar
  2. the "application" jar / classpath
  3. the filesystem (specified as a -D property to the JVM)
  1. 一个普通的罐子
  2. “应用程序”jar/类路径
  3. 文件系统(指定为 JVM 的 -D 属性)

The Configuration class code is such that properties in the application overwrite properties in common and the filesystem properties overwrite application level properties.

配置类代码使得应用程序中的属性覆盖公共属性,文件系统属性覆盖应用程序级别的属性。

The Configuration is loaded as a Spring bean and exposes its internal Properties object to a PropertyPlaceholderConfigurer which allow further $varsubstitution in Spring xml files. Since the config is a Spring bean, it can be injected where ever I need it.

配置作为 Spring bean 加载,并将其内部 Properties 对象公开给 PropertyPlaceholderConfigurer,允许$var在 Spring xml 文件中进一步替换。由于配置是一个 Spring bean,它可以在我需要的任何地方注入。

Something like this should be extensible to handling JSON or YAML file instead of properties files to allow maps, list, etc.

像这样的东西应该可以扩展到处理 JSON 或 YAML 文件而不是属性文件以允许映射、列表等。