20

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 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

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.

The Alchemist
  • 3,397
  • 21
  • 22
Nakedible
  • 4,067
  • 7
  • 34
  • 40

5 Answers5

18

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

http://owner.aeonbits.org

Nathan Perrier
  • 534
  • 1
  • 5
  • 14
  • 1
    It has been 6 years since the comment. But I still want to write: really, OWNER is a very interesting choice: Still supported -> last version: June 2020; Perfect and very nice looking documentation; Lightweight -> single JAR <100 Kb; Long list of features out of the box; Easy to expand -> you can right your own Loaders(and get your properties from any source), Converters (and convert your property to any type in any way) etc. – Gmugra Dec 10 '20 at 07:24
12

Have you already had a look at Apache Commons Configuration?

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • I have, but though it didn't fulfill the requirements. Now, reading it a bit further, I found out it does support many of the features - but I'm still not sure if it fulfills the requirements, so I need to reread the documentation. – Nakedible Mar 31 '11 at 07:46
  • After reading through the documentation once more: No multiple references to same configured object and no inheritable configuration (without doing it yourself on top of Apache Commons Configuration). – Nakedible Apr 04 '11 at 13:32
  • 4
    Also, Apache Commons Configuration has a lot of runtime dependencies. It isn't exactly lightweight. – wmarbut Jun 28 '13 at 14:23
7

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

smat
  • 489
  • 3
  • 5
  • Looks good - except doesn't seem to fulfill the listed requirements, namely object references and "inhertance". – Nakedible Mar 31 '11 at 07:50
5

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

PanHrabia
  • 174
  • 1
  • 4
2

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.

AngerClown
  • 6,149
  • 1
  • 25
  • 28
  • Also, here's some sample code for integrating JSON properties into Spring config files: http://christopherhunt-software.blogspot.com/2010/04/spring-propertyeditor-for-json-map-and.html – AngerClown Mar 31 '11 at 13:19
  • This is a pretty basic setup. However, my requirements in the question specifically list several things not achieved by this. The configuration needs of this project are actually complex enough to need it - the current configuration is over 5000 lines long. – Nakedible Apr 04 '11 at 13:31