当前位置:网站首页>@How to use bindsinstance in dagger2
@How to use bindsinstance in dagger2
2022-07-02 23:25:00 【WongKyunban】
Don't use @BindsInstance
@BindsInstance
Is used to remove Module The parameterized constructor of the module . For example, we now have a dependency is Context, So how to provide it ? One way is through a module Class's constructor injection , And by the module Provide Context To others module Class uses :
@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)
Use @BindsInstance
Use @BindsInstance The purpose of is not to pass a module Class to inject dependencies , Directly in component Provide during initialization , Such as :
Remove first module Class constructor :
@Module
class AppModule {
@Provides
@Singleton
fun provideSharedPreferences(app: Application): SharedPreferences = app.getSharedPreferences("Share", Context.MODE_PRIVATE)
}
stay Component Inflow dependence :
@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)
For example :
https://github.com/wongkyunban/MyDaggerDemo
边栏推荐
- Set right click to select vs code to open the file
- Editor Caton
- Strictly abide by the construction period and ensure the quality, this AI data annotation company has done it!
- Implementation of VGA protocol based on FPGA
- psnr,ssim,rmse三个指标的定量分析
- 内网渗透 | 手把手教你如何进行内网渗透
- Where is the win11 automatic shutdown setting? Two methods of setting automatic shutdown in win11
- Win11麦克风测试在哪里?Win11测试麦克风的方法
- Go basic data type
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验3 按键输入实验(学习笔记)
猜你喜欢
4 special cases! Schools in area a adopt the re examination score line in area B!
What if win11 can't turn off the sticky key? The sticky key is cancelled but it doesn't work. How to solve it
Use of recyclerview with viewbinding
Brief introduction of emotional dialogue recognition and generation
Li Kou brush questions (2022-6-28)
Talk about memory model and memory order
海思 VI接入视频流程
基于Pyqt5工具栏按钮可实现界面切换-2
(毒刺)利用Pystinger Socks4上线不出网主机
Jinglianwen technology's low price strategy helps AI enterprises reduce model training costs
随机推荐
Brief introduction of emotional dialogue recognition and generation
Three solutions to frequent sticking and no response of explorer in win11 system
20220527_ Database process_ Statement retention
The first batch of Tencent cloud completed the first cloud native security maturity assessment in China
Static file display problem
密码技术---分组密码的模式
Set right click to select vs code to open the file
Simple square wave generating circuit [51 single chip microcomputer and 8253a]
Value sequence < detailed explanation of daily question >
All things work together, and I will review oceanbase's practice in government and enterprise industry
解决:exceptiole ‘xxxxx.QRTZ_LOCKS‘ doesn‘t exist以及mysql的my.cnf文件追加lower_case_table_names后启动报错
Cryptography -- the mode of block cipher
[npuctf2020]ezlogin XPath injection
基于FPGA的VGA协议实现
Sword finger offer II 099 Sum of minimum paths - double hundred code
Makefile configuration of Hisilicon calling interface
Use the scroll bar of souI when using the real window in souI
Writing of head and bottom components of non routing components
面试过了,起薪16k
LINQ usage collection in C #