花了很多时间试图弄清楚为什么我的匕首注射不起作用; 我意识到Kotlin中的"对象"类型是问题所在.
以下不起作用,注入的"属性"为空.
object SomeSingleton {
@Inject
lateinit var property: Property
init {
DaggerGraphController.inject(this)
}
}
Run Code Online (Sandbox Code Playgroud)
但是,以下DID工作正常:
class NotSingleton {
@Inject
lateinit var property: Property
init {
DaggerGraphController.inject(this)
}
}
Run Code Online (Sandbox Code Playgroud)
我试过谷歌,我尝试了文档,但我无法指出这背后的原因.另请注意,我没有尝试使用JAVA,JAVA并没有内置单例的概念.
为什么会这样?为什么kotlin singleton无法注入成员但是常规非单身人士类可以?