依赖注入 Dagger 2学习
2015-12-30 22:48
141 查看
参考:
1. Dagger2 基础注入(极大促进理解)
2. 官方文档
3. Dagger 2 on Android 前半部分
4. Dagger 2 - the API
gradle(app)中添加apt插件:
gradle(app)中添加compile:
声明依赖。注入器,标识被注入的对象。会调用其构造方法来创建对象
Use @Inject to annotate the constructor that Dagger should use to create instances of a class.
When a new instance is requested, Dagger will obtain the required parameters values and invoke this constructor.
Dagger can inject fields directly.
给无参构造函数添加@Inject注解则会创建其对象
If your class has @Inject-annotated fields but no @Inject-annotated constructor, Dagger will inject those fields if requested, but will not create new instances. Add a no-argument constructor with the @Inject annotation to indicate that Dagger may create instances as well.
@Provides :satisfy a dependency.
提供依赖,所有的Provides方法必须属于Module
provideXxx() is invoked whenever a Xxx is required.It contact with the @Inject
@Module:Modules are classes whose methods provide dependencies
@Component:an Interface
连接依赖和消费依赖的组件
annotate to such an interface and passing the module types to the modules parameter, Dagger 2 can form a fully object graph.
接口实现类:The implementation has the same name as the interface prefixed with Dagger. Obtain an instance by invoking the builder() method on that implementation and use the returned builder to set dependencies and build() a new instance.
简写:Any module with an accessible default constructor can use .create() to initiate an instance.
@Singleton:单例
The graph will use a single instance of the value for all of its clients. It can decorate the @Provides and the class.
懒加载
you can create a Lazy which defers instantiation until the first call to Lazy< T >’s get() method
Provider injections
@Module classes are named with a Module suffix.
Provider injections
自定义注解 Qualifiers
还需要在实战中增强理解
单方面理解概念总会忘记,不能形成全貌。不太理解所谓的ObjectGraph怎么形成的,依赖之间的相互依赖,提供依赖的还要消费别的依赖。所以从生成代码上理解下,可能对以后大有帮助。
打断点会帮助理解
1. Dagger2 基础注入(极大促进理解)
2. 官方文档
3. Dagger 2 on Android 前半部分
4. Dagger 2 - the API
使用配置
项目根project.gradle内dependencies中添加classpath:dependencies{ classpath'com.android.tools.build:gradle:1.5.0'//gradle 版本号 classpath'com.neenbedankt.gradle.plugins:android-apt:1.4' }
gradle(app)中添加apt插件:
apply plugin: 'com.neenbedankt.android-apt'
gradle(app)中添加compile:
apt 'com.google.dagger:dagger-compiler:2.0' compile 'com.google.dagger:dagger:2.0' provided 'javax.annotation:jsr250-api:1.0'
Dagger2注解介绍
@Inject:Declaring Dependencies声明依赖。注入器,标识被注入的对象。会调用其构造方法来创建对象
Use @Inject to annotate the constructor that Dagger should use to create instances of a class.
When a new instance is requested, Dagger will obtain the required parameters values and invoke this constructor.
Dagger can inject fields directly.
给无参构造函数添加@Inject注解则会创建其对象
If your class has @Inject-annotated fields but no @Inject-annotated constructor, Dagger will inject those fields if requested, but will not create new instances. Add a no-argument constructor with the @Inject annotation to indicate that Dagger may create instances as well.
@Provides :satisfy a dependency.
提供依赖,所有的Provides方法必须属于Module
provideXxx() is invoked whenever a Xxx is required.It contact with the @Inject
@Module:Modules are classes whose methods provide dependencies
@Component:an Interface
连接依赖和消费依赖的组件
annotate to such an interface and passing the module types to the modules parameter, Dagger 2 can form a fully object graph.
接口实现类:The implementation has the same name as the interface prefixed with Dagger. Obtain an instance by invoking the builder() method on that implementation and use the returned builder to set dependencies and build() a new instance.
简写:Any module with an accessible default constructor can use .create() to initiate an instance.
@Singleton:单例
The graph will use a single instance of the value for all of its clients. It can decorate the @Provides and the class.
Dagger 2特殊
Lazy injections :Lazy< T >懒加载
you can create a Lazy which defers instantiation until the first call to Lazy< T >’s get() method
Provider injections
命名规范:
@Provides methods are named with a provide prefix@Module classes are named with a Module suffix.
需要继续理解的:
ScopeProvider injections
自定义注解 Qualifiers
还需要在实战中增强理解
单方面理解概念总会忘记,不能形成全貌。不太理解所谓的ObjectGraph怎么形成的,依赖之间的相互依赖,提供依赖的还要消费别的依赖。所以从生成代码上理解下,可能对以后大有帮助。
打断点会帮助理解
相关文章推荐
- [LeetCode]045-Jump Game II
- js关于省市的二级下拉菜单联动
- 最新版db2 express c 10.1安装图解
- 离元旦还有一天的日记:真实的创作总会带来一定的启发
- 怎样使用jsp实现header和footer与网页内容的分离
- 循环赛日程表(用来说明算法导论上的题目!!)
- 工作总总结1 - osgi插件多并发产生的问题解决思路
- [引言]学习编程技术需要的基本条件
- 安装Win7和Ubuntu双系统后,Win7耳机无声音的解决办法
- win7下在VMware中修改Centos7的主机名
- 《Windows小应用》如何将一个自己download的zip文件安装的程序添加到系统服务中
- 数据库之基础3
- UIApplication的delegate说明
- 创建一个透明的UIToolbar
- 解决Access连接 accdb 不可识别的数据库格式异常
- 微信JS SDK PHP Demo
- P12 (**) 解码游程编码过的列表
- P12 (**) 解码游程编码过的列表
- PHP查看常量
- Mysql 日志