当前位置:网站首页>@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)
边栏推荐
- MySQL queries nearby data And sort by distance
- Numerical solution of partial differential equations with MATLAB
- 程序员版本的八荣八耻~
- Jerry's built-in short press and long press, no matter how long it is, it is a short press [chapter]
- P7072 [csp-j2020] live broadcast Award
- STM32串口DAM接收253字节就死机原因排查
- Jerry's prototype has no touch, and the reinstallation becomes normal after dismantling [chapter]
- Splunk audit 的设定
- Realize the linkage between bottomnavigationview and navigation
- Pandora IOT development board learning (HAL Library) - Experiment 3 key input experiment (learning notes)
猜你喜欢

“一个优秀程序员可抵五个普通程序员!”

China Academy of information technology, Tsinghua University, Tencent security, cloud native security, industry university research and use strong alliance!

Realize the linkage between bottomnavigationview and navigation
![[favorite poems] OK, song](/img/1a/e4a4dcca494e4c7bb0e3568f708288.png)
[favorite poems] OK, song

BBR encounters cubic

AES高级加密协议的动机阐述

解决:exceptiole ‘xxxxx.QRTZ_LOCKS‘ doesn‘t exist以及mysql的my.cnf文件追加lower_case_table_names后启动报错

泛型与反射,看这篇就够了

ADC of stm32

Innovation strength is recognized again! Tencent security MSS was the pioneer of cloud native security guard in 2022
随机推荐
Generics and reflection, this is enough
Editor Caton
ADC of stm32
[Solved] Splunk: Cannot get username when all users are selected“
Cryptographic technology -- key and ssl/tls
AES高級加密協議的動機闡述
RecyclerView结合ViewBinding的使用
Win11启用粘滞键关闭不了怎么办?粘滞键取消了但不管用怎么解决
Eight honors and eight disgraces of the programmer version~
China Academy of information technology, Tsinghua University, Tencent security, cloud native security, industry university research and use strong alliance!
严守工期,确保质量,这家AI数据标注公司做到了!
SQL advanced syntax
Pandora IOT development board learning (HAL Library) - Experiment 4 serial port communication experiment (learning notes)
Lambda expression: an article takes you through
Distributed monitoring system ZABBIX
PMP project integration management
SQL进阶语法
详解Promise使用
景联文科技低价策略帮助AI企业降低模型训练成本
“一个优秀程序员可抵五个普通程序员!”