<p>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. </p>

<p>Please do only reply if you have practical experience with a framework. I'm not looking for examples, but experience...</p>

[{'answer_id': 25768, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 6, 'selected': True, 'text': '<p>If your hardcoded values are just simple key-value pairs, you should look at <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html" rel="noreferrer">java.util.Properties</a>.  It\'s a lot simpler than xml, easier to use, and mind-numbingly trivial to implement.</p>\n\n<p>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.</p>\n\n<p>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.</p>\n\n<p>Of course this has the huge benefit of not requiring any 3rd party libraries to utilize.</p>\n'}, {'answer_id': 25819, 'author': 'Anders Sandvig', 'author_id': 1709, 'author_profile': 'https://Stackoverflow.com/users/1709', 'pm_score': 2, 'selected': False, 'text': '<p>I tend to use <code>java.util.Properties</code> (or similar classes in other languages and frameworks) <a href="http://blog.looplabel.net/2008/06/26/design-patterns-for-implementing-application-preferences-anyone/" rel="nofollow noreferrer">wrapped in an application-specific configuration class</a> 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.</p>\n\n<p>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.</p>\n\n<p><strong>Update</strong></p>\n\n<p>OK. That wasn\'t entirely true, three is the <a href="http://www.springframework.org/javaconfig" rel="nofollow noreferrer">Spring Java Configuration Project</a>.</p>\n'}, {'answer_id': 25844, 'author': 'dlinsin', 'author_id': 198, 'author_profile': 'https://Stackoverflow.com/users/198', 'pm_score': 2, 'selected': False, 'text': '<p>I <a href="http://dlinsin.blogspot.com/2008/07/configuration-notations.html" rel="nofollow noreferrer">wrote</a> about this a couple of weeks ago and came to the conclusion that XML is one of the most widely used notations. </p>\n\n<p>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.</p>\n'}, {'answer_id': 26077, 'author': 'ScArcher2', 'author_id': 1310, 'author_profile': 'https://Stackoverflow.com/users/1310', 'pm_score': 3, 'selected': False, 'text': '<p><a href="http://commons.apache.org/configuration/" rel="noreferrer">Commons Configuration</a></p>\n\n<p>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.</p>\n\n<p>If you aren\'t doing anything complicated I\'d stick to properites files.</p>\n'}, {'answer_id': 34397, 'author': 'John Meagher', 'author_id': 3535, 'author_profile': 'https://Stackoverflow.com/users/3535', 'pm_score': 5, 'selected': False, 'text': '<p><a href="http://commons.apache.org/configuration/" rel="nofollow noreferrer">Apache Commons Configuration</a> 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 <a href="http://en.wikipedia.org/wiki/Factory_method_pattern" rel="nofollow noreferrer">factory</a> to get the configuration and just use the <a href="http://commons.apache.org/configuration/apidocs/org/apache/commons/configuration/Configuration.html" rel="nofollow noreferrer">Configuration interface</a> after that.</p>\n\n<p>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:</p>\n\n<pre class="lang-none prettyprint-override"><code>server.host=myHost\nserver.url=http://${server.host}/somePath\n</code></pre>\n'}, {'answer_id': 242419, 'author': 'lowellk', 'author_id': 22063, 'author_profile': 'https://Stackoverflow.com/users/22063', 'pm_score': 3, 'selected': False, 'text': '<p>If you want to do something advanced (and typesafe), you might want to take a look at this: <a href="http://www.ibm.com/developerworks/java/library/j-configint/index.html" rel="noreferrer">http://www.ibm.com/developerworks/java/library/j-configint/index.html</a></p>\n'}, {'answer_id': 854163, 'author': 'Peter Lawrey', 'author_id': 57695, 'author_profile': 'https://Stackoverflow.com/users/57695', 'pm_score': 0, 'selected': False, 'text': "<p>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.</p>\n\n<p>Note: In the simplest case (pre-compiled) you don't need any additional libraries.</p>\n"}, {'answer_id': 1004245, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>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.  </p>\n\n<p>Reasons: increased scalability, back-end neutrality, ect.</p>\n'}, {'answer_id': 1622751, 'author': 'Pascal Thivent', 'author_id': 70604, 'author_profile': 'https://Stackoverflow.com/users/70604', 'pm_score': 4, 'selected': False, 'text': '<p>Here are various options:</p>\n\n<ul>\n<li>java.util.Properties</li>\n<li>java.util.prefs.Preferences (since Java 5)</li>\n<li><a href="http://commons.apache.org/configuration/" rel="noreferrer">Commons Configuration</a></li>\n<li><a href="http://www.jConfig.org/" rel="noreferrer">jConfig</a></li>\n<li><a href="http://jfig.sourceforge.net/" rel="noreferrer">JFig</a></li>\n<li><a href="http://carbon.sourceforge.net/modules/core/docs/config/Usage.html" rel="noreferrer">Carbon\'s Configuration Service</a></li>\n</ul>\n\n<p>You might want to read <a href="http://www.mail-archive.com/commons-dev@jakarta.apache.org/msg37597.html" rel="noreferrer">Comparison of Commons Configuration With JFig and JConfig</a> and <a href="http://www.theserverside.com/news/thread.tss?thread_id=23763" rel="noreferrer">Configuring your Applications using JFig</a> for some feedback from various users.</p>\n\n<p>Personally, I\'ve used jConfig and it was a good experience. </p>\n'}, {'answer_id': 1949877, 'author': 'NateS', 'author_id': 187883, 'author_profile': 'https://Stackoverflow.com/users/187883', 'pm_score': 1, 'selected': False, 'text': '<p>You can try <a href="http://yamlbeans.sourceforge.net/" rel="nofollow noreferrer">YamlBeans</a>. 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.</p>\n\n<p>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.</p>\n'}, {'answer_id': 2447375, 'author': 'javabegin', 'author_id': 293990, 'author_profile': 'https://Stackoverflow.com/users/293990', 'pm_score': 1, 'selected': False, 'text': '<p>Please take a look at this URL:\n     <a href="http://issues.apache.org/jira/browse/CONFIGURATION-394" rel="nofollow noreferrer">http://issues.apache.org/jira/browse/CONFIGURATION-394</a></p>\n\n<p>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).</p>\n\n<p>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.</p>\n\n<p>The Apache guys insist that this project is out of scopes of Commons Configuration, maybe!</p>\n\n<p>I\'ve attached a simple configuration framework, take look please.</p>\n'}, {'answer_id': 2629387, 'author': 'Ichiro Furusato', 'author_id': 230955, 'author_profile': 'https://Stackoverflow.com/users/230955', 'pm_score': 1, 'selected': False, 'text': '<p>I just posted a brief bit of <a href="https://stackoverflow.com/questions/2621319/properties-framework-in-java-apps/2629148#2629148">code</a> 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.</p>\n'}, {'answer_id': 9103711, 'author': 'Kristian', 'author_id': 11429, 'author_profile': 'https://Stackoverflow.com/users/11429', 'pm_score': 0, 'selected': False, 'text': '<p>You could have a look at newly announced <a href="http://stoffe.deephacks.org/2012/02/01/tools4j-config/" rel="nofollow">tools4j-config</a> whose mission statement is to allow you to easily handle configuration at runtime.</p>\n'}, {'answer_id': 12216958, 'author': 'Felix Dobslaw', 'author_id': 1638882, 'author_profile': 'https://Stackoverflow.com/users/1638882', 'pm_score': 3, 'selected': False, 'text': '<p>the Intelligent Parameter Utilization Tool <strong>(InPUT, <a href="https://github.com/feldob/InPUT">page</a>)</strong> allows to <strong>externalize almost any (hard coded) decision as a parameter into an XML based configuration file.</strong> 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.</p>\n\n<p>InPUT is probably more powerful than most use cases require, as it allows for the <strong>programming language independent formulation</strong> of experimental data (input - output), with features such as the definition of <strong>complex descriptor to class mappings</strong>, or randomized configuration spawning and validation based on predefined value ranges (for test and research, e.g. Monte Carlo simulations). You can <strong>define parameters with sub parameters, relative restrictions on parameter values (numerical param a > param b)</strong> etc. .</p>\n\n<p>Its still in beta, but rather stable, I use it for my research, for the <strong>configuration and documentation of experiments, and for teaching purposes</strong>. 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, <strong>experimental results can be validated/programs can be migrated more easily</strong>. The documentation is still in working process, but a couple of <strong>examples are available</strong> on the page. InPUT is <strong>open source</strong> software.</p>\n\n<p>For those interested, the <a href="http://dl.acm.org/citation.cfm?id=2330784.2330807">Conceptual Research Paper</a>.</p>\n'}]
['https://Stackoverflow.com/questions/25765', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1448983/']