Skip to content

Application properties

Marcin Zielonka edited this page Jan 23, 2025 · 3 revisions

javalin-xt provides a simple mechanism of loading application configuration from YAML file located in resources. It is very similar to Spring Boot application properties.

Enabling application properties

Application properties can be enabled via JavalinConfig.enableApplicationProperties() method.

val app = Javalin.create { config ->
    config.enableApplicationProperties()
}

Accessing application properties

Application properties are by default read from application.yml file located in resources directory. There is no support for .properties format yet - only YAML.

You can access application properties via app.properties extension value. Then, you can access given property using the following methods:

  • get(key: String): Property
  • getOrNull(key: String): Property

The first one retrieves property under given key or throws PropertyNotFoundException if property does not exist. The property can also be acessed in the same way as accessing map values by key:

val property1 = app.properties.get("property1")
val property2 = app.properties["property2"]

The second one provides ability to retrieve the property if exists or null otherwise. Therefore, you can provide default values using Kotlin ?: operator:

val property1 = app.properties.getOrNull("property1")?.asString ?: "defaultValue1"

Supported structures

javalin-xt currently supports both nested property structures and arrays. Example:

property1: value1
property2:
  property3: value3
  property4:
    property5: value5
    property6: value6
property7:
  - property7a:
      property8a: value8a
      property9a: value9a
  - property7b:
      property8b: value8b
      property9b: value9b

Supported types

Currently, javalin-xt application properties supports the following types:

  • String
  • numeric (Long, Int, Float, Double)
  • Boolean
  • List<String>
  • numeric list (List<Long>, List<Int>, List<Double>, List<Float>)
  • List<Boolean>

To retrieve given property as a value with given type, you can use one of the following methods on retrieved Property property:

  • asString
  • asInt, asLong, asDouble, asFloat
  • asBoolean
  • asStringList
  • asIntList, asLongList, asFloatList, asDoubleList
  • asBooleanList

Example:

val property1: Long = app.properties["property1"].asLong
val property2: List<String> = app.properties.getOrNull("property2")?.asStringList ?: emptyList<String>

The following tables presents matrix of possible conversions between properties provided in YAML file and accessed in runtime:

YAML \ runtime asString asInt asLong asDouble asFloat asBoolean asStringList asIntList asLongList asFloatList asDoubleList asBooleanList
string
numeric
boolean
list<string>
list<numeric>
list<boolean>

Profiles

javalin-xt application properties feature provides support for specifying profile to read properties from different YAML file related to given profile.

If profile is specified, the application properties are retrieved from application-<profile>.yml first and if not found, there is a fallback to application.yml.

Specifying profile

To specify a profile, you need to provide it within application properties config:

val app = Javalin.create { config ->
    config.enableApplicationProperties { propertiesConfig ->
        propertiesConfig.profile = "dev"
    }
}

Environment variables

javalin-xt application properties also support specifying values in YAML file from environment variables.

If you want to declare property value to be retrieved from environment variable, use ${ENV_VAR} syntax:

property1:
  property2: ${PROPERTY_2_VALUE}

You can also disable resolving environment variable feature by providing additional flag with application properties config:

val app = Javalin.create { config ->
    config.enableApplicationProperties { propertiesConfig ->
        propertiesConfig.resolveEnvironmentVariables = false
    }
}
Clone this wiki locally