Kotlin Application Configuration: Overview of Libraries and Tools
Configuration is one of the key tasks in modern application development. Properly organized settings management allows you to easily adapt an application to different environments (development, testing, production), simplifies deployment, and enhances security (for example, by eliminating the storage of secrets in code). In the Kotlin ecosystem, there are several popular approaches and libraries for solving this problem. In this article, we will review the main tools, their advantages, and usage examples.
1. Typesafe Config (HOCON)
HOCON (Human-Optimized Config Object Notation) is a configuration file format and library developed for the JVM platform. It is widely used in Kotlin projects, especially in conjunction with frameworks like Akka, Play Framework, or Lagom. Main advantages: concise syntax, support for inheritance, references to other values, and file inclusion.
Installation
// build.gradle.ktsimplementation("com.typesafe:config:1.4.3")Example configuration file (application.conf)
app { name = "MyKotlinApp" version = "1.0.0" server { host = "localhost" port = 8080 } database { url = "jdbc:postgresql://${app.server.host}:5432/mydb" user = "admin" password = ${?DB_PASSWORD} // substitution from environment variable }}Reading configuration in Kotlin
import com.typesafe.config.ConfigFactory
fun main() { val config = ConfigFactory.load() val appName = config.getString("app.name") val port = config.getInt("app.server.port") val dbUrl = config.getString("app.database.url") println("App: $appName, Port: $port, DB URL: $dbUrl")}The library automatically loads application.conf from the classpath and supports overriding settings via environment variables or JVM parameters.
2. Koin — Configuration via Dependency Injection
Koin is a lightweight DI framework for Kotlin that can also be used for configuration management. It allows you to declare settings as singletons or factories and inject them into application components.
Example with Koin
// Defining a module with configurationval appModule = module { single { AppConfig( name = "KoinApp", version = "2.0", port = 9090 ) }}
// Starting KoinstartKoin { modules(appModule)}
// Usageclass MyService(private val config: AppConfig) { fun start() = println("Starting ${config.name} on port ${config.port}")}Koin is convenient for small and medium-sized projects where you don't want to include heavy frameworks. The configuration remains in code but can be extracted into separate files or loaded from environment variables.
3. Spring Boot and @ConfigurationProperties
For projects using Spring Boot, configuration is integrated at the framework level. Configuration classes are annotated with @ConfigurationProperties, and values are loaded from application.yml or application.properties.
Example
// application.ymlapp: name: SpringKotlinApp server: port: 8080 database: url: jdbc:postgresql://localhost:5432/mydb
// Kotlin class@ConfigurationProperties(prefix = "app")data class AppProperties( val name: String, val server: ServerConfig, val database: DatabaseConfig) { data class ServerConfig(val port: Int) data class DatabaseConfig(val url: String)}
// Usage in a service@Serviceclass MyService(private val props: AppProperties) { fun info() = "App: ${props.name}, Port: ${props.server.port}"}Spring Boot supports profiles