I'm new to Kotlin and Kodein. I'm trying to use a Java library and I need to pass a singleton to one of my constructors. I can't figure out how to get an actual instance. Since I need to pass an instance to the constructor I believe I need to be using DKodein so lazy loading isn't being used?
val kodein: DKodein = Kodein.direct {
bind<DataSource>() with singleton {
val config = HikariConfig()
config.jdbcUrl = "jdbc:postgresql://localhost:5432/mydb"
config.username = "username"
config.password = "password"
config.driverClassName = "org.postgresql.Driver"
HikariDataSource(config)
}
bind<DatabaseContext>() with singleton {
// I thought kodein.instance() here would provide the DataSource
// instance I declared above. However I get the error (from Intellij)
// TypeInference failed. Expected type mismatch.
// Required: DataSource!
// Found: KodeinProperty<???>
DatabaseContext(kodein.instance())
}
}
Is there any easy way to achieve this? Or am I going about this the wrong way?
Thanks.