当前位置:网站首页>@BindsInstance在Dagger2中怎么使用
@BindsInstance在Dagger2中怎么使用
2022-07-02 22:13:00 【WongKyunban】
不使用 @BindsInstance
@BindsInstance
的使用是为了移除Module模块的有参构造函数。比如我们现在有一个依赖是Context,那么如何提供它呢?一种方法是通过一个module类的构造函数注入,并由module提供Context给其他module类使用:
@Module
class AppModule(val app: Application) {
@Provides
@Singleton
fun provideApplication(): Application = app
@Provides
@Singleton
fun provideSharedPreferences(app: Application): SharedPreferences = app.getSharedPreferences("Share", Context.MODE_PRIVATE)
}
@Singleton
@Component(modules = [AndroidInjectionModule::class,AppModule::class,ToastMakerModule::class,MainActivityModule::class])
interface AppComponent {
@Component.Builder
interface Builder {
// without @BindsInstance
fun appModule(appModule: AppModule):Builder
fun build():AppComponent
}
fun inject(app:StApplication)
}
appComponent = DaggerAppComponent
.builder()
.appModule(AppModule(this))
.build()
appComponent.inject(this)
使用@BindsInstance
使用@BindsInstance的目的就是不再通过一个module类的构造函数来注入依赖,直接在component初始化时提供,如:
首先移除module类的构造函数:
@Module
class AppModule {
@Provides
@Singleton
fun provideSharedPreferences(app: Application): SharedPreferences = app.getSharedPreferences("Share", Context.MODE_PRIVATE)
}
在Component里流入依赖:
@Singleton
@Component(modules = [AndroidInjectionModule::class,AppModule::class,ToastMakerModule::class,MainActivityModule::class])
interface AppComponent {
@Component.Builder
interface Builder {
// with @BindsInstance
@BindsInstance
fun provideContext(app: Application):Builder
fun build():AppComponent
}
fun inject(app:StApplication)
}
// with @BindsInstance
appComponent = DaggerAppComponent
.builder()
.provideContext(this)
.build()
appComponent.inject(this)
边栏推荐
- Chow-Liu Tree
- Jerry's prototype has no touch, and the reinstallation becomes normal after dismantling [chapter]
- Pandora IOT development board learning (HAL Library) - Experiment 3 key input experiment (learning notes)
- 损失函数~
- Splunk audit 的设定
- Motivation du Protocole de chiffrement avancé AES
- 20220527_ Database process_ Statement retention
- C#中Linq用法汇集
- Application of containerization technology in embedded field
- xshell配置xforward转发火狐浏览器
猜你喜欢
解决:exceptiole ‘xxxxx.QRTZ_LOCKS‘ doesn‘t exist以及mysql的my.cnf文件追加lower_case_table_names后启动报错
P1007 single log bridge
严守工期,确保质量,这家AI数据标注公司做到了!
Is 408 not fragrant? The number of universities taking the 408 examination this year has basically not increased!
AES高級加密協議的動機闡述
Explain promise usage in detail
The motivation of AES Advanced Encryption Protocol
Successfully changed Splunk default URL root path
Data analysis learning records -- complete a simple one-way ANOVA with Excel
[chestnut sugar GIS] how does global mapper batch produce ground contour lines through DSM
随机推荐
QT qpprogressbar details
Tiktok actual combat ~ number of likes pop-up box
Solving ordinary differential equations with MATLAB
海思调用接口之Makefile配置
SharedPreferences 保存List<Bean> 到本地并解决com.google.gson.internal.LinkedTreeMap cannot be cast to异常
Motivation du Protocole de chiffrement avancé AES
Jerry's fast touch does not respond [chapter]
P7072 [csp-j2020] live broadcast Award
MySQL queries nearby data And sort by distance
Freshman learning sharing
Lambda expression: an article takes you through
Xshell configuration xforward forwarding Firefox browser
20220527_ Database process_ Statement retention
【Redis笔记】压缩列表(ziplist)
Use the scroll bar of souI when using the real window in souI
Quantitative analysis of PSNR, SSIM and RMSE
为什么RTOS系统要使用MPU?
Learning Websites commonly used by circuit designers
编辑卡顿
潘多拉 IOT 开发板学习(HAL 库)—— 实验3 按键输入实验(学习笔记)