【发布时间】:2018-04-15 00:19:27
【问题描述】:
我是 Kotlin 和 Kodein 的新手。我正在尝试使用 Java 库,并且需要将单例传递给我的一个构造函数。我不知道如何获得一个实际的实例。由于我需要将一个实例传递给构造函数,我相信我需要使用 DKodein,所以没有使用延迟加载?
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())
}
}
有没有什么简单的方法可以做到这一点?还是我走错了路?
谢谢。
【问题讨论】:
-
kodein不能在其自己的初始化表达式中访问。