Kotlin对象中的Dagger 2注入上下文 [英] Dagger 2 Inject Context in Kotlin Object
问题描述
我正在尝试使用Dagger 2注入Context
.
I am trying to inject Context
using Dagger 2.
AppComponent.kt:
AppComponent.kt:
@Singleton
@Component(
modules = [
AppModule::class
]
)
interface AppComponent {
fun context(): Context
}
AppModule.kt:
AppModule.kt:
@Module
class AppModule(private val application: Application) {
@Provides
@Singleton
fun providesApplicationContext(): Context = application
}
MainApp.kt:
MainApp.kt:
class MainApp : Application() {
lateinit var appComponent: AppComponent
override fun onCreate() {
super.onCreate()
appComponent = initDagger()
}
private fun initDagger() = DaggerAppComponent.builder()
.appModule(AppModule(this))
.build()
}
Manager.kt :(我要在其中注入Context
的类)
Manager.kt: (Class where I want to inject Context
)
object Manager {
@Inject
lateinit var context: Context
}
但是,我遇到以下错误:
However, I am getting following error:
error: Dagger does not support injection into static fields
public static android.content.Context context;
^
这是因为我正在使用object
(Singleton)吗?
如果您对问题有任何疑问,请在下面评论.谢谢.
Is this because I am using an object
(Singleton)?
If you have any doubts related to question, please comment below. Thank you.
推荐答案
这是因为我使用的是
object
(Singleton)吗?
Is this because I am using an
object
(Singleton)?
是的,object
s的属性是Java的底层静态字段.您的Manager
类将反编译为类似于以下内容的文件:
Yes, object
s' properties are Java's static fields under the hood. Your Manager
class would decompile to something similar to:
public final class Manager {
@Inject
@NotNull
public static Context context;
public static final Manager INSTANCE;
static {
INSTANCE = new Manager();
}
private Manager() {
}
@NotNull
public final Context getContext() {
return context;
}
public final setContext(Context var1) {
context = var1;
}
}
和Dagger 2只需does not support injection into static fields
.但是,即使Dagger允许您这样做,依存关系也不会得到满足.这是因为,除非明确告知Dagger,否则Dagger无法将其注入到对象中(就像您将其注入到活动中一样)或自行创建对象.显然,当涉及Kotlin的object
时,后者不适用.为了要求Dagger注入依赖关系,您必须以某种方式提供一个MainApp
实例:
and Dagger 2 simply does not support injection into static fields
. However, even if Dagger let you do it, the dependency wouldn't be satisfied. That's because Dagger won't be able to inject into an object unless is explicitly told to do so (like you do when injecting for example into activities) or creates the object by itself. Obviously the latter doesn't apply when it comes to Kotlin's object
s. In order to ask Dagger to inject the dependency you would have to somehow provide a MainApp
instance:
init {
mainApp.appComponent.inject(this)
}
这没有多大意义,因为您首先想将其(作为Context
)注入.
which doesn't make much sense since you'd like to inject it (as a Context
) in the first place.
因此,您必须手动满足依赖关系,而不必在此麻烦Dagger或将object
想法抛在脑后,仅使用标准类并让Dagger处理其范围:
Therefore you have to either satisfy the dependency manually and don't bother with Dagger in this one or leave the object
idea behind, use just a standard class and let Dagger handle its scope:
@Singleton
class Manager @Inject constructor(private val context: Context) {
}
唯一的缺点是您必须将一个Manager
实例(由Dagger创建)注入每个需要使用它的类中,而不是静态地调用其方法.
The only drawback is that you would have to inject a Manager
instance (created by Dagger) into every class that needs to use it instead of calling its methods statically.
这篇关于Kotlin对象中的Dagger 2注入上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!