Jonas
09/24/2025, 9:09 PM@Property
annotation when using basic dependency registration? I cannot seem to get it to work, so I was wondering if this only works in configuration-based dependency registration. Would be very nice to have a convenient way to access the config when using basic registration.Bruce Hamilton
09/24/2025, 10:57 PMJonas
09/25/2025, 5:40 AMfun Application.main() {
databaseModule()
//... other modules
}
where databaseModule
is definded like this:
fun Application.databaseModule() {
val databaseUrl = environment.config.property("database.url").getString()
dependencies {
provide {
provideDatabase(databaseUrl)
}
}
}
fun provideDatabase(databaseUrl: String): Database {
val config = HikariConfig().apply {
jdbcUrl = databaseUrl
driverClassName = "org.postgresql.Driver"
maximumPoolSize = 10
}
val datasource = HikariDataSource(config)
Database.connect(datasource = datasource)
}
I'd love to be able to define it like this:
fun Application.databaseModule() {
dependencies {
provide(::provideDatabase)
}
}
fun provideDatabase(@Property("database.url") databaseUrl: String): Database {
val config = HikariConfig().apply {
jdbcUrl = databaseUrl
driverClassName = "org.postgresql.Driver"
maximumPoolSize = 10
}
val datasource = HikariDataSource(config)
Database.connect(datasource = datasource)
}
It should be possible since dependencies
are defined within the scope of Application
where the config is available, but I don't know the implementation details of ktor di.JP Sugarbroad
09/25/2025, 6:31 AMfun Application.provideDatabase
instead?Bruce Hamilton
09/25/2025, 6:56 AMfun Application.dbModule() {
dependencies {
provide { provideDatabase(property("database.url")) }
}
}
Bruce Hamilton
09/25/2025, 6:58 AMJonas
09/25/2025, 7:01 AMproperty
shorthand ๐Bruce Hamilton
09/25/2025, 7:16 AMJonas
09/25/2025, 7:17 AM