任何可能的使用方式使用绑定注释提供此?
@Singleton
@Provides
fun provideUtils(context: Context): Utils {
return Utils(context)
}
如果你的Utils
类有一个@Inject
构造函数,例如
class Utils @Inject constructor(private val context: Context) {
// ...
}
...然后你可以像这样绑定它,Dagger应该能够弄清楚如何创建它(假设你在某处提供了Context
):
@Singleton
@Binds
fun bindUtils(utils: Utils): Utils
你应该在这里使用构造函数注入。如果使用构造函数注入,则不需要@Provides
或@Binds
。
@Singleton // scope on the class
class Utils @Inject constructor(private val context: Context)
就这样。