avaje / avaje-config

Application configuration / properties loading for JVM applications
https://avaje.io/config
Apache License 2.0
58 stars 8 forks source link
application-configuration avaje dynamic-configuration java jvm kotlin properties properties-loader yaml

Discord Maven Central javadoc License Build native image build

Avaje Config

This library loads properties files that can be used to configure an application including "testing" and "local development" and dynamic configuration (changes to configuration properties at runtime).

<dependency>
  <groupId>io.avaje</groupId>
  <artifactId>avaje-config</artifactId>
  <version>${avaje.config.version}</version>
</dependency>

Typical use

Config use

Getting property values


// get a String property
String value = Config.get("myapp.foo");

// with a default value
String value = Config.get("myapp.foo", "withDefaultValue");

// also int, long and boolean with and without default values
int intVal = Config.getInt("bar");
long longVal = Config.getLong("bar");
boolean booleanVal = Config.getBool("bar");

Register callback on property change.


Config.onChange("myapp.foo", newValue -> {
  // do something ...
});

Config.onChangeInt("myapp.foo", newIntValue -> {
  // do something ...
});

Config.onChangeLong("myapp.foo", newLongValue -> {
  // do something ...
});

Config.onChangeBool("myapp.foo", newBooleanValue -> {
  // do something ...
});

Loading properties

Config loads properties from expected locations as well as via command line arguments. Below is the how it looks for configuration properties.

Setting the config.profiles or environment variable CONFIG_PROFILES will cause avaje config to load the property files in the form application-${profile}.properties (will also work for yml/yaml files).

For example, if you set the config.profiles to dev,docker it will attempt to load application-dev.properties and application-docker.properties.

We can define a load.properties property which has name of property file in resource folder, or path locations for other properties/yaml files to load.

load.properties is pretty versatile and can even be chained. For example, in your main application properties, you can have load.properties=application-${profile:local}.properties to load based on another property, and in the loaded properties you can add load.properties there to load more properties, and so on.

Example application.properties:

common.property=value
load.properties=application-${profile:local}.properties,path/to/prop/application-extra2.properties

If no test resources were loaded then it additionally loads from "local dev" and command line:

We can specify an app.name property and then put a properties/yaml file at: ${user.home}/.localdev/{appName}.yaml We do this to set/override properties when we want to run the application locally (aka main method)

Command line arguments starting with -P can specify properties/yaml files to load

When properties are loaded they are merged/overlayed.

config.load.systemProperties

If we set config.load.systemProperties to true then all the properties that have been loaded are then set into system properties.