当前位置:网站首页>@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
边栏推荐
- Quantitative analysis of PSNR, SSIM and RMSE
- Application of containerization technology in embedded field
- Sword finger offer II 099 Sum of minimum paths - double hundred code
- STM32串口DAM接收253字节就死机原因排查
- Prometheus deployment
- Typical case of data annotation: how does jinglianwen technology help enterprises build data solutions
- Eight honors and eight disgraces of the programmer version~
- [redis notes] compressed list (ziplist)
- 設置單擊右鍵可以選擇用VS Code打開文件
- 第三方支付功能测试点【杭州多测师_王sir】【杭州多测师】
猜你喜欢
抖音实战~点赞数量弹框
Tiktok actual combat ~ number of likes pop-up box
[analysis of STL source code] imitation function (to be supplemented)
Introduction to the latest plan of horizon in April 2022
[adjustment] postgraduate enrollment of Northeast Petroleum University in 2022 (including adjustment)
理想汽车×OceanBase:当造车新势力遇上数据库新势力
Looking at Ctrip's toughness and vision from the Q1 financial report in 2022
公司里只有一个测试是什么体验?听听他们怎么说吧
Three solutions to frequent sticking and no response of explorer in win11 system
Win11麦克风测试在哪里?Win11测试麦克风的方法
随机推荐
Submit code process
Markdown basic grammar
ServletContext learning diary 1
Strictly abide by the construction period and ensure the quality, this AI data annotation company has done it!
程序员版本的八荣八耻~
Win11系统explorer频繁卡死无响应的三种解决方法
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:
Static file display problem
Pandora IOT development board learning (HAL Library) - Experiment 4 serial port communication experiment (learning notes)
密码技术---分组密码的模式
Cryptographic technology -- key and ssl/tls
Numerical solution of partial differential equations with MATLAB
Realize the linkage between bottomnavigationview and navigation
SQL advanced syntax
Go basic anonymous variable
Implementation of VGA protocol based on FPGA
[npuctf2020]ezlogin XPath injection
@BindsInstance在Dagger2中怎么使用
Tiktok actual combat ~ number of likes pop-up box
How difficult is it to be high? AI rolls into the mathematics circle, and the accuracy rate of advanced mathematics examination is 81%!