当前位置:网站首页>@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
边栏推荐
- Hisilicon VI access video process
- Where is the win11 microphone test? Win11 method of testing microphone
- CDN 加速,需要域名先备案
- 密码技术---分组密码的模式
- golang中new与make的区别
- 富滇银行完成数字化升级|OceanBase数据库助力布局分布式架构中台
- [hardware] origin of standard resistance value
- 为什么RTOS系统要使用MPU?
- Go basic data type
- Brief introduction of emotional dialogue recognition and generation
猜你喜欢

程序员版本的八荣八耻~

内网渗透 | 手把手教你如何进行内网渗透

Implementation of VGA protocol based on FPGA

潘多拉 IOT 开发板学习(HAL 库)—— 实验4 串口通讯实验(学习笔记)

MarkDown基本语法

Writing of head and bottom components of non routing components
![[favorite poems] OK, song](/img/1a/e4a4dcca494e4c7bb0e3568f708288.png)
[favorite poems] OK, song

Prometheus deployment

Start from the bottom structure to learn the customization and testing of FPGA --- Xilinx ROM IP

FOC矢量控制及BLDC控制中的端电压、相电压、线电压等概念别还傻傻分不清楚
随机推荐
SharedPreferences save list < bean > to local and solve com google. gson. internal. Linkedtreemap cannot be cast to exception
YOLOX加强特征提取网络Panet分析
“一个优秀程序员可抵五个普通程序员!”
2022年最新最全软件测试面试题大全
C MVC creates a view to get rid of the influence of layout
Tronapi wave field interface - source code without encryption - can be opened twice - interface document attached - packaging based on thinkphp5 - detailed guidance of the author - July 1, 2022 08:43:
C#中Linq用法汇集
力扣刷题(2022-6-28)
从底层结构开始学习FPGA----Xilinx ROM IP的定制与测试
Typical case of data annotation: how does jinglianwen technology help enterprises build data solutions
Where is the win11 automatic shutdown setting? Two methods of setting automatic shutdown in win11
2016. maximum difference between incremental elements
高数有多难?AI 卷到数学圈,高数考试正确率 81%!
Temperature measurement and display of 51 single chip microcomputer [simulation]
Quantitative analysis of PSNR, SSIM and RMSE
Three solutions to frequent sticking and no response of explorer in win11 system
@BindsInstance在Dagger2中怎么使用
Third party payment function test point [Hangzhou multi tester _ Wang Sir] [Hangzhou multi tester]
Implementation of VGA protocol based on FPGA
[analysis of STL source code] imitation function (to be supplemented)