开发者

What are possible lightweight Java configuration libraries for handling complex configuration? [closed]

开发者 https://www.devze.com 2023-02-21 03:40 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 6 years ago.

Improve this question

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

  • Nested configuration values with lists, maps, etc. - not just plain key/value pairs
  • Multiple configuration files where later configuration files can intellig开发者_开发百科ently 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

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

  • 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

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.


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

http://owner.aeonbits.org


Have you already had a look at Apache Commons Configuration?


We use Constretto at our project. Very lightweight and easy to use.


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


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:

  1. a common jar
  2. the "application" jar / classpath
  3. the filesystem (specified as a -D property to the JVM)

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 $var substitution in Spring xml files. Since the config is a Spring bean, it can be injected where ever I need it.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消