当前位置:网站首页>@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)
边栏推荐
- Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年7月1日08:43:06
- [favorite poems] OK, song
- Chow-Liu Tree
- psnr,ssim,rmse三个指标的定量分析
- Data analysis learning records -- complete a simple one-way ANOVA with Excel
- Lc173. Binary search tree iterator
- Catalogue of digital image processing experiments
- [Yangcheng cup 2020] easyphp
- Brief introduction of emotional dialogue recognition and generation
- Odoo13 build a hospital HRP environment (detailed steps)
猜你喜欢
P7072 [csp-j2020] live broadcast Award
[npuctf2020]ezlogin XPath injection
ServletContext learning diary 1
Getting started with golang: for Range an alternative method of modifying the values of elements in slices
详解Promise使用
C#中Linq用法汇集
[Solved] Splunk: Cannot get username when all users are selected“
STM32之ADC
Simple square wave generating circuit [51 single chip microcomputer and 8253a]
Why does RTOS system use MPU?
随机推荐
Learning Websites commonly used by circuit designers
Why does RTOS system use MPU?
Introduction to the latest plan of horizon in April 2022
Win11系统explorer频繁卡死无响应的三种解决方法
Odoo13 build a hospital HRP environment (detailed steps)
归并排序详解及应用
P1007 single log bridge
移动端 1px 像素兼容性问题,实现1px 边框
The difference between new and make in golang
Static file display problem
Looking at Ctrip's toughness and vision from the Q1 financial report in 2022
Li Kou brush questions (2022-6-28)
Jerry's fast touch does not respond [chapter]
深度剖析数据在内存中的存储----C语言篇
AES高級加密協議的動機闡述
Splunk audit setting
Win11自动关机设置在哪?Win11设置自动关机的两种方法
设置单击右键可以选择用VS Code打开文件
[chestnut sugar GIS] ArcScene - how to make elevation map with height
golang入门:for...range修改切片中元素的值的另类方法