All Projects → IvanTrendafilov → Confucius

IvanTrendafilov / Confucius

Licence: apache-2.0
A lightweight Java configuration library

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Confucius

javaproperties
Python library for reading & writing Java .properties files
Stars: ✭ 20 (-60.78%)
Mutual labels:  configuration, properties
Konf
A type-safe cascading configuration library for Kotlin/Java/Android, supporting most configuration formats
Stars: ✭ 225 (+341.18%)
Mutual labels:  properties, configuration
Konfig
Simple config properties API for Kotlin
Stars: ✭ 249 (+388.24%)
Mutual labels:  properties, configuration
PropertiesFile4Delphi
Library for managing configuration files with key-value syntax
Stars: ✭ 17 (-66.67%)
Mutual labels:  configuration, properties
go-contrib
Helper for Log configuration, Mixin for properties with fangs
Stars: ✭ 20 (-60.78%)
Mutual labels:  configuration, properties
easy-props
The simple, stupid properties library for Java
Stars: ✭ 76 (+49.02%)
Mutual labels:  configuration, properties
profig
Powerful configuration management for Scala (JSON, properties, command-line arguments, and environment variables)
Stars: ✭ 25 (-50.98%)
Mutual labels:  configuration, properties
goconfig
.gitconfig syntax parser
Stars: ✭ 15 (-70.59%)
Mutual labels:  configuration, properties
Ec 471g
黑苹果配置 hackintosh dsdt clover v3489 e1 471g ec 471g v3 471g acer mac osx efi ssdt aml config plist hd4000 gt630m inter
Stars: ✭ 30 (-41.18%)
Mutual labels:  configuration
Rescripts
💥 Use the latest react-scripts with custom configurations for Babel, ESLint, TSLint, Webpack,... ∞
Stars: ✭ 992 (+1845.1%)
Mutual labels:  configuration
Emacs.dz
Awesome emacs config files
Stars: ✭ 886 (+1637.25%)
Mutual labels:  configuration
Cookbook
🎶 Cookbook for Nette Framework (@nette) & Contributte (@contributte). Read it while its HOT!
Stars: ✭ 30 (-41.18%)
Mutual labels:  configuration
Rose
🌹 Rose is a toolkit for writing, editing and running application configurations.
Stars: ✭ 41 (-19.61%)
Mutual labels:  configuration
Cas Configserver Overlay
Generic CAS Spring Cloud Configuration Server WAR overlay
Stars: ✭ 28 (-45.1%)
Mutual labels:  configuration
Node No Config
Config and resource loader
Stars: ✭ 45 (-11.76%)
Mutual labels:  configuration
Yamlsettings
Yaml Settings Configuration Module
Stars: ✭ 12 (-76.47%)
Mutual labels:  configuration
Kubevious
Kubevious - application centric Kubernetes UI and continuous assurance provider
Stars: ✭ 869 (+1603.92%)
Mutual labels:  configuration
Resticprofile
Configuration profiles for restic backup
Stars: ✭ 48 (-5.88%)
Mutual labels:  configuration
Laravel Settings
Simple Settings package for a laravel application
Stars: ✭ 45 (-11.76%)
Mutual labels:  configuration
Configuration
Library for setting values to structs' fields from env, flags, files or default tag
Stars: ✭ 37 (-27.45%)
Mutual labels:  configuration

Confucius - lightweight configuration

A lightweight Java configuration library and a drop-in replacement for java.util.Properties. Confucius is the easiest way to add configuration to your application. It is licensed under the Apache License, Version 2.0.

Why use Confucius?

  • a fluent API to access key-value pairs, as well as lists
  • meticulously tested - with 84 test cases and 97% branch coverage
  • support for a powerful context-based Properties file format
  • support for the standard Java Properties file format
  • automatically load configuration from a specified Properties file or InputStream
  • use a single Properties file to wire multiple app instances via contexts
  • tiny, yet powerful - only 16K on disk
  • no XML required... unless you use Maven

Getting started

Four steps are required to get started with Confucius.

First, you need the Confucius jar file on your classpath. Confucius is available from Maven Central, so add to your pom.xml (or manually download, if need be):

    <dependency>
        <groupId>org.trendafilov.confucius</groupId>
        <artifactId>confucius</artifactId>
        <version>1.2.2</version>
    </dependency>

Second, define your configuration file. In this example, we will showcase the recommended context-based Properties format with three contexts, including the mandatory Default context. By convention, a Confucius Properties file has a .cfg extension.

Alternatively, you can supply any existing standard Java Properties file, but you will not be able to take advantage of context-driven configuration.

shop.cfg:

[Default]
slogan  = The best bagels store in town.
bagels  = plain, cinnamon raisin, chocolate

[NY]
slogan  = NY bagel place - Try them to love them.
bagels  = plain, plain /w cream cheese, whole wheat 

[SF]
slogan  = The Round Food Experience(tm).

Note: the SF context is missing the bagels key. This is intentional and will cause the value of the bagels property to be inherited from the Default context.

Third, reference the configuration keys in your code.

BagelShop.java:

package org.trendafilov.confucius.examples.bagels;

import org.trendafilov.confucius.*;

public class BagelShop {
    
    public static void main(String[] args) {
        Configurable config = Configuration.getInstance();
        System.out.println("Hello and welcome to our store. " + config.getStringValue("slogan"));
        System.out.println("Today's selection: " + config.getStringList("bagels"));
    }
}

Finally, to launch the resulting application, specify the conf.properties and conf.context system properties using the -D switch, as follows:

Referencing the NY context:

$ java -Dconf.properties=`pwd`/shop.cfg -Dconf.context=NY -cp *.jar org.trendafilov.confucius.examples.bagels.BagelShop

Hello and welcome to our store. NY bagel place - Try them to love them.
Today's selection: [plain, plain /w cream cheese, whole wheat]

Referencing the SF context:

$ java -Dconf.properties=`pwd`/shop.cfg -Dconf.context=SF -cp *.jar org.trendafilov.confucius.examples.bagels.BagelShop

Hello and welcome to our store. The Round Food Experience(tm).
Today's selection: [plain, cinnamon raisin, chocolate]

Without specifying a context:

$ java -Dconf.properties=`pwd`/shop.cfg -cp *.jar org.trendafilov.confucius.examples.bagels.BagelShop

Hello and welcome to our store. The best bagels store in town.
Today's selection: [plain, cinnamon raisin, chocolate]

Examples are available at Confucius-examples.

FAQ

Q: How do I get the documentation?
A: Ideally, you will be referencing the documentation as assisted by your IDE. If you need to look at an HTML version of the javadoc, please checkout the project and execute mvn javadoc:javadoc.

Q: I want to see configuration information in my log files. How?
A: Confucius exposes logging via SLF4J. To enable it, please place the desired binding on your classpath, as instructed here.

Q: I want to inject a Configurable instance via a dependency injection framework. How do I do this correctly?
A: DI containers should control the scope of the Configurable singleton. Therefore, please wire the InjectableConfiguration object instead of Configuration with your DI framework of choice.

Q: Please summarise how context-based Properties files work?
A: At a minimum, you must specify a Default context section. Its contents are always processed and are processed first. Each context section contains a list of key-value pairs. Additional contexts may be defined - if so, you should specify which context should be processed via the conf.context property. Please note - the values of keys which already exist in Default will be overriden. Furthermore, it is possible to define variable substitutions, including substitutions which span the Default and the user-specified context.

Q: Please explain variable substitution?
A: Sure, here is an example:

...
name            = John Smith
home.address    = 22 Main street
mailing.address = ${home.address}
...

the value mailing.address is set to be whatever the value of home.address is.
In general, you could also assign variables to other variables and there is no limit on the depth of variable references. Circular definitions are unresolvable and will be treated as literals.

Q: I need to use a standard existing Java properties file, how do I set this up?
A: There is no special setup required. Just set the conf.properties property to the file path, as usual.

Q: Why not just use java.util.Properties?
A: Confucius aims to fix a lot of the issues present in that class (extends Hashtable, no clean way to interpolate with a HashMap, doesn't use generics, manual type conversions required, and so on) and adds several powerful features, whilst still maintaining the simplicity of the Properties concept and compatibility with existing configuration files.

Q: Why NoXML? XML is much more expressive and powerful.
A: Indeed, it is! XML is a great tool for the right tasks. Nevertheless, for the vast majority of applications, XML configuration adds more complexity than value. This results in configuration files which are difficult to understand, cumbersome to update, and hard to maintain. For example, if we were to translate the Confucius properties file from the example above to XML, we might end up with something like this:

config.xml:

<configuration>
    <Default>
        <property name="slogan">
            <value>The best bagels store in town.</value>
        </property>
        <util:list name="bagels">
            <value>plain</value>
            <value>cinnamon raisin</value>
            <value>chocolate</value>
        </util:list>
    </Default>
    <NY>
        <property name="slogan">
            <value>NY bagel place - Try them to love them.</value>
        </property>
        <util:list name="bagels">
            <value>plain</value>
            <value>plain /w cream cheese</value>
            <value>whole wheat</value>
        </util:list>
    </NY>
    <SF>
        <property name="slogan">
            <value>The Round Food Experience(tm).</value>
        </property>
        <util:list name="bagels"> <!-- no inheritance -->
            <value>plain</value>
            <value>cinnamon raisin</value>
            <value>chocolate</value>
        </util:list>
    </SF>
</configuration>

Even in this trivial example, the corresponding XML file has grown notably large. Confucius aims to capture the 90% case and save you time.

Support, questions, contributions

Please open a ticket and we will discuss.

Contributors

  • Bartłomiej Dudała @bartlomiej-dudala. Work on support for InputStreams in InjectableConfiguration.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].