当前位置:网站首页>@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
边栏推荐
- 富滇银行完成数字化升级|OceanBase数据库助力布局分布式架构中台
- Golang common settings - modify background
- PotPlayer设置最小化的快捷键
- Li Kou brush questions (2022-6-28)
- [favorite poems] OK, song
- Looking at Ctrip's toughness and vision from the Q1 financial report in 2022
- QT qpprogressbar details
- Tiktok actual combat ~ number of likes pop-up box
- 【STL源码剖析】仿函数(待补充)
- 为什么RTOS系统要使用MPU?
猜你喜欢

Print out mode of go

Prometheus deployment
![[npuctf2020]ezlogin XPath injection](/img/6e/dac4dfa0970829775084bada740542.png)
[npuctf2020]ezlogin XPath injection

阿里云有奖体验:如何使用 PolarDB-X

Win11系统explorer频繁卡死无响应的三种解决方法

Alibaba cloud award winning experience: how to use polardb-x

Eight honors and eight disgraces of the programmer version~

Configuration clic droit pour choisir d'ouvrir le fichier avec vs Code
![Eight bit responder [51 single chip microcomputer]](/img/4f/683948fca93a0dc1a0409839f60759.jpg)
Eight bit responder [51 single chip microcomputer]

Win11麦克风测试在哪里?Win11测试麦克风的方法
随机推荐
Cryptographic technology -- key and ssl/tls
[adjustment] postgraduate enrollment of Northeast Petroleum University in 2022 (including adjustment)
密码技术---密钥和SSL/TLS
Detailed explanation and application of merging and sorting
@BindsInstance在Dagger2中怎么使用
Set right click to select vs code to open the file
实现BottomNavigationView和Navigation联动
万物并作,吾以观复|OceanBase 政企行业实践
Talk about memory model and memory order
Use of recyclerview with viewbinding
【STL源码剖析】仿函数(待补充)
[redis notes] compressed list (ziplist)
Win11麦克风测试在哪里?Win11测试麦克风的方法
vim区间删行注释
How difficult is it to be high? AI rolls into the mathematics circle, and the accuracy rate of advanced mathematics examination is 81%!
RuntimeError: no valid convolution algorithms available in CuDNN
What experience is there only one test in the company? Listen to what they say
[favorite poems] OK, song
海思调用接口之Makefile配置
CDN acceleration requires the domain name to be filed first