I have this dependency:
@Singleton
class SpiceMix @Inject constructor(@field:[Named("oregano")] private val oregano: Spice,
@field:[Named("sage")] private val sage: Spice,
@field:[Named("rosemary")] private val rosemary: Spice)
以及一个模块来实现其依赖性:
@Module
class SpiceModule {
@Provides
@Named("oregano")
@Singleton
fun provideOregano(): Spice = Oregano()
@Provides
@Named("sage")
@Singleton
fun provideSage(): Spice = Sage()
@Provides
@Named("rosemary")
@Singleton
fun provideRosemary(): Spice = Rosemary()
The SpiceMix
is then injected in various locations of my app.
但是,这不会编译,我得到一个错误:
Spice cannot be provided without an @Provides-annotated method
I think the @Named annotations do not quite work in my constructor signature. I am not quite sure how I can make it work.
Note: this compiles fine if I ditch the Named annotations and change the types of the constructor parameters to their concrete forms. However, Spice is an interface, and I need it for mocking purposes in my tests.
我能做什么?