Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Application configuration / properties loading for JVM applications

License

NotificationsYou must be signed in to change notification settings

avaje/avaje-config

Repository files navigation

DiscordMaven CentraljavadocLicenseBuildnative image build

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

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

Typical use

  • Put application.yaml into src/main/resources for properties that have reasonable defaults
  • Put application-test.yaml into src/test/resources for properties used when running tests
  • Specify external properties via command line arguments. These effectively override application.yaml properties.

Config use

Getting property values

// get a String propertyStringvalue =Config.get("myapp.foo");// with a default valueStringvalue =Config.get("myapp.foo","withDefaultValue");// also int, long and boolean with and without default valuesintintVal =Config.getInt("bar");longlongVal =Config.getLong("bar");booleanbooleanVal =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.

  • loads from main resources (if they exist)

    • application.yaml
    • application.properties
  • loads files from the current working directory (if they exist)

    • application.yaml
    • application.properties
  • loads via system propertyprops.file or environment variablePROPS_FILE (if defined)

  • loads via system propertyavaje.profiles or environment variableAVAJE_PROFILES (if defined).

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

For example, if you set theconfig.profiles todev,docker it will attempt to loadapplication-dev.properties andapplication-docker.properties.

  • loads viaload.properties property.

We can define aload.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 haveload.properties=application-${profile:local}.properties to load based on another property, and in the loaded properties you can addload.properties there to load more properties, and so on.

Example application.properties:

common.property=valueload.properties=application-${profile:local}.properties,path/to/prop/application-extra2.properties
  • loads test resources (if they exist, nb: Test resources are only visible when running tests)
    • application-test.properties
    • application-test.yaml

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

  • loads from "local dev".

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

  • loads from command line arguments

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 setconfig.load.systemProperties to true then all the properties that have been loaded are then set into system properties.


[8]ページ先頭

©2009-2025 Movatter.jp