当前位置:网站首页>@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)
边栏推荐
- Deep analysis of data storage in memory - C language
- 抖音实战~点赞数量弹框
- 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:
- 分布式监控系统zabbix
- 损失函数~
- Configuration clic droit pour choisir d'ouvrir le fichier avec vs Code
- [Solved] Splunk: Cannot get username when all users are selected“
- Learning Websites commonly used by circuit designers
- [chestnut sugar GIS] how does global mapper batch produce ground contour lines through DSM
- [favorite poems] OK, song
猜你喜欢
Typical case of data annotation: how does jinglianwen technology help enterprises build data solutions
Go language sqlx library operation SQLite3 database addition, deletion, modification and query
聊聊内存模型与内存序
BBR encounters cubic
AES高级加密协议的动机阐述
Construction of Hisilicon 3559 universal platform: rotation operation on the captured YUV image
【Redis笔记】压缩列表(ziplist)
Generics and reflection, this is enough
[npuctf2020]ezlogin XPath injection
景联文科技低价策略帮助AI企业降低模型训练成本
随机推荐
归并排序详解及应用
Why does RTOS system use MPU?
The motivation of AES Advanced Encryption Protocol
在SOUI里使用真窗口时使用SOUI的滚动条
密码技术---密钥和SSL/TLS
Loss function~
Eight honors and eight disgraces of the programmer version~
Win11启用粘滞键关闭不了怎么办?粘滞键取消了但不管用怎么解决
阿里云有奖体验:如何使用 PolarDB-X
golang入门:for...range修改切片中元素的值的另类方法
Static file display problem
Stop slave is stuck -- the event of the transaction is not copied completely
聊聊内存模型与内存序
[hardware] origin of standard resistance value
Lc173. Binary search tree iterator
[chestnut sugar GIS] how does global mapper batch produce ground contour lines through DSM
用matlab调用vs2015来编译vs工程
Jerry's prototype has no touch, and the reinstallation becomes normal after dismantling [chapter]
Distributed monitoring system ZABBIX
Set right click to select vs code to open the file