我定义了我的Dagger 2限定符
@Qualifier
@MustBeDocumented
@kotlin.annotation.Retention(AnnotationRetention.RUNTIME)
annotation class TrackerQualifier(val value: TrackerType)
enum class TrackerType {
INTERNAL,
EXTERNAL
}
它很好用,
@Provides
@TrackerQualifier(TrackerType.INTERNAL)
@Singleton
UsageTracker providesInternalTracker(InternalTracker analyticsTracker) {
return new UsageTracker(analyticsTracker);
}
@Provides
@TrackerQualifier(TrackerType.EXTERNAL)
@Singleton
UsageTracking providesExternalTracker(ExternalTracker eventTracker) {
return eventTracker;
}
但是,如果我想将其发送到另一个注入的模块中。
@Provides
fun provideCommonImage(
imageEngine: ImageEngine,
externalUsageTracking: UsageTracking) =
CommonImage(imageEngine, externalUsageTracking)
它将编译失败,因为它不知道上面的externalUsageTracking: UsageTracking
是INTERNAL
还是EXTERNAL
。我该如何注释该参数?
答案 0 :(得分:1)
尝试显示限定词。
@Provides
fun provideCommonImage(
imageEngine: ImageEngine,
@TrackerQualifier(TrackerType.EXTERNAL) tracker: UsageTracker) =
CommonImage(imageEngine, tracker)