andorid jar/库源码解析之Dagger/Dagger2
Dagger、Dagger2:
作用:
1、用于解耦Activity和业务逻辑
2、在使用业务的时候,不需要重复编写new代码。
3、当业务变化的时候,不需要对所有的UI,进行修改。
4、便于测试,和正式,替换指定Module即可。
栗子:
需要:
1、定义 CommonComponent 的接口 标记 @Component(modules = XXXModule.class) 的注解
2、定义XXXModule 模块,并标注 @Module 的注解 ,对添加的方法使用 provide开头,这些 provide方法需要添加 @Provides注解
A业务类:
构造函数使用 @Inject 注解
B页面:
1、定义A业务类的变量,使用@Inject注解
2、在OnCreate中,使用如下代码进行初始调用
DaggerCommonComponent.builder().commonModule(new CommonModule(this)).build().inject(this);
3、DaggerCommonComponent由,使用Dagger自动生成的类(Dagger使用APT技术实现),使用apt生成如下代码
编译之后查看APK是下面这样(红色部分为自动生成)
4、然后就可以使用了。。。。
源码解读:
①:创建一个 DaggerCommonComponent$Builder对象,用于接收 CommonModule 和返回 CommonComponent的接口对象
②:创建一个 CommonModule对象,参数是当前对象,并且赋值CommonModule对象给 DaggerCommonComponent$Builder对象的成员
③:检查 CommonModule对象,并且返回一个 DaggerCommonComponent对象。且该对象创建了一个Prodiver对象。且赋值为 新增类 CommonModule_ProvideIcommonViewFactory 继承于Factory(Factory extends Provider),且包含成员CommonModule
④:调用DaggerCommonComponent对象的inject并传入当前对象,创建一个 LoginPresenter 对象,并且赋值给this对象的presenter对象(该对象标记过Inject)
源码:https://github.com/square/dagger (Dagger) https://github.com/google/dagger (Dagger2)
引入:
// dagger2
implementation 'com.google.dagger:dagger:2.23.1'
annotationProcessor 'com.google.dagger:dagger-compiler:2.23.1'
andorid jar/库源码解析之Dagger/Dagger2的更多相关文章
- andorid jar/库源码解析之Bolts
目录:andorid jar/库源码解析 Bolts: 作用: 用于链式执行跨线程代码,且传递数据 栗子: Task.call(new Callable<Boolean>() { @Ove ...
- andorid jar/库源码解析之EventBus
目录:andorid jar/库源码解析 EventBus: 作用: 用于不同Activity,Service等之间传递消息(数据). 栗子: A页面:onCreate定义 EventBus.ge ...
- andorid jar/库源码解析之okhttp3
目录:andorid jar/库源码解析 Okhttp3: 作用: 用于网络编程(http,https)的快速开发. 栗子: // okHttpClient定义成全局静态,或者单例,不然重复new可能 ...
- andorid jar/库源码解析之okio
目录:andorid jar/库源码解析 Okio: 作用: 说白了,就是一个IO库,基于java原生io.来进行操作,内部做了优化,简洁,高效.所以受到了一部分人的喜欢和使用 栗子: 读写文件. p ...
- andorid jar/库源码解析之retrofit2
目录:andorid jar/库源码解析 Retrofit2: 作用: 通过封装okhttp库,来进行web通讯,并且使用动态代理的方式,来调用接口地址,通过回调赋值结果. 栗子: 定义一个接口,用于 ...
- andorid jar/库源码解析之Butterknife
目录:andorid jar/库源码解析 Butterknife: 作用: 用于初始化界面控件,控件方法,通过注释进行绑定控件和控件方法 栗子: public class MainActivity e ...
- andorid jar/库源码解析之zxing
目录:andorid jar/库源码解析 Zxing: 作用: 生成和识别,二维码,条形码. 栗子: 生成二维码,赋值到ImageView上 QRCodeWriter qrCodeWriter = n ...
- andorid jar/库源码解析之错误提示
目录:andorid jar/库源码解析 错误: 错误1: Error: Static interface methods are only supported starting with Andro ...
- andorid jar/库源码解析
前言 本篇作为开篇,会大体上说明,需要解读源码的,类库,或者jar. 序 原本,类库和jar的系列准备写到逆向系列课程的,但是那个东西,在写了两篇,就没有后续了,现在也不知道从哪里开始了, 只能等后期 ...
随机推荐
- 29.2 Iterator 迭代器ConcurrentModificationException:并发修改异常处理
/** Iterator:迭代器* * 需求:判断集合中是否包含元素java,如果有则添加元素android * Exception in thread "main" java.u ...
- Maybatis的一些总结(二:基本使用过程)
理清一下使用需要做的步骤 建项目,导入mybatis(3.5.2)和mysql(5.1.47)进pom.xml pom.xml需配置build时过滤器,否则会出现xml文件导出不了的问题 resour ...
- spring 管理事务配置时,结果 报错: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个异常
java.lang.IllegalStateException: No Hibernate Session bound to thread, and configuration does not al ...
- 文档根元素 "beans" 必须匹配 DOCTYPE 根 "null"
文档根元素 "beans" 必须匹配 DOCTYPE 根 "null" (2011-11-20 21:26:41) 转载▼ 标签: 杂谈 分类: spring- ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十三)之Strings
Immutable Strings Objects of the String class are immutable. If you examine the JDK documentation fo ...
- 为什么选择python?
Why python? 那些最好的程序员不是为了得到更高的薪水或者得到公众的仰慕而编程,他们只是觉得这是一件有趣的事情. —— Linux 之父 Linux Torvalds 作为一个使用主义的学习者 ...
- 彻底弄懂GMT、UTC、时区和夏令时
前言 格林威治时间.世界时.祖鲁时间.GMT.UTC.跨时区.夏令时,这些眼花缭乱的时间术语,我们可能都不陌生,但是真正遇到问题,可能又不那么确定,不得不再去查一查,处理完可能过段时间又忘记.今天,我 ...
- 初学者的Pygame安装教程
最近在自学python,在看完了些基础知识之后,准备写个小项目[外星人入侵],这个项目需要安装pygame. 所以就在网上找到了两个下载地址https://bitbucket.org/pygame/p ...
- PHP的yield是个什么玩意
来源:https://segmentfault.com/a/1190000018457194 其实,我并不是因为迭代或者生成器或者研究PHP手册才认识的yield,要不是协程,我到现在也不知道PHP中 ...
- Cannot find libcrypto in Ubuntu
https://stackoverflow.com/questions/13811889/cannot-find-libcrypto-in-ubuntu sudo apt-get install li ...