前言:
在上一篇Dagger 2 系列(四)——@Named 和 @Qualifier注解介绍,了@Named 和 @Qualifier注解,这篇文章,我们将会了解另外俩个注解:@Scope 和 @Singleton。
在这篇文章中你会了解到:
- @Scope 是什么
- @Singleton 是什么
- @Scope和 @Component 如何协同作战。
Dagger2 的学习曲线确实是比较陡的,个人认为陡的点一是对依赖注入(控制反转)概念的理解,所以有了Dagger 2 系列(一)——基础篇之依赖注入的介绍,另外一个就是 对 Scope 的理解,对于此我也是翻看了大量的博客,经过了反复学习、再学习的往复过程,才对 Scope 的概念有了基本的认识。
1. @Scope
我们首先看一下 froer_mcs 在 一文中谈到 Scope 能给我们带来什么
翻译过来就是:
Dagger2 中 Scope 机制保证在 Scope 的作用域内类会保持单例。在实际开发中这意味着在 @ApplicationScope 对应的作用域中类的实例对象的生命会像 Application 一样长,在 @ActivityScope 的作用域内的类实例的生命周期和相应的 Activity 一样长。(不要想当然的认为 Dagger2 会根据 Scope 注解的字面意义实现相应的类实例的单例效果,实现这样的效果是需要具体实现的。)
总的来说, Scope 机制会保证在 Scope 的生命周期内实现 “本地单例”
在 Component 的生命周期内,Scope 注解依赖会保证单例。(也就是说,此处的单例是 Component 生命周期内的单例,如果 Component 实例对象重新实例化的,则单例效果失效。)
通过以上的引用和翻译不知道你是否重新认识了 Scope ,在上文中一个反复强调的概念:
在 Dagger2 中 Scope 机制可以保证在 Scope 标记的 Component 作用域内 ,类会保持单例 。
2. @Singleton
重申一遍:
在 Dagger2 中 Scope 机制可以保证在 Scope 标记的 Component 作用域内 ,类会保持单例 。
如果理解了这句话,那么回过头来看 @Singleton 这个注解,是不是有一种豁然开朗的感觉。并不是只有 @Singleton 注解标记的相关类生产的实例是单例的,是所有的 Scope(自定义 Scope) 标记的相关类生产的实例 都是单例 的,只不过这个单例是有条件的 – 在 Scope 注解标记 Component 的作用域内生产的实例是单例的 。
Scope 机制下的单例其实和 @Singleton 的字面意义 没有半毛钱关系,当初自己就是被这种错误的思想误导了很长时间。其实如果你愿意你,可以把 @Singleton 换成任意单词,什么 @Dog、@Cat、@XXx 都可以,你只要保证这个注解标记的 Component 在 App 进程中为单例的,并且得到正确的实现(被正确的标记到 类构造器 或 Module 中的 @Provides 标记的方法),那么它对应生成的类实例就是 单例 的。
@Singleton 之所以被默认实现,只是因为这可以让人根据它的字面意思,知道被他标记的相关生成的类实例为单例,这符合了 Java 的命名规范。
3. 举个栗子:
上面谈到的全都是理论,那么我们就是用相应的代码来验证他们。
3.1 自定义 Scope
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface AnyOneScope {
}
这里为了表明最后的 单例 和 Scope 的命名没有任何关系,名字避免使用了容易给人造成疑惑的 ApplicationScope 、ActivityScope 等,而使用了 AnyOneScope,但是其实这些名字都是无所谓的 。
3.2 POJO – AppleBean
public class AppleBean {private String color;private int weight;public AppleBean() {Log.e("TAG", "AppleBean");}
}
3.3 POJO – OrgranBean
public class OrgranBean {private String color;private int weight;public OrgranBean() {Log.e("TAG", "OrgranBean");}
}
3.4 Module
@Module
public class FruitModule {@AnyOneScope@ProvidesAppleBean provideApple() {return new AppleBean();}@AnyOneScope@ProvidesOrgranBean provideOrgran() {return new OrgranBean();}
}
该 Module 提供 AppleBean 、OrgranBean 实例对象的方法,两个方法使用 @AnyOneScope 进行注解。
3.5 Component
@AnyOneScope
@Component(modules = {FruitModule.class})
public interface FruitComponent {void inject(FuriteScopeActivity mTestScopeActivity);
}
3.6 目标类 (注入类)
public class FuriteScopeActivity extends AppCompatActivity {@InjectAppleBean mAppleBeanA;@InjectAppleBean mAppleBeanB;@InjectOrgranBean mOrgranBeanA;@InjectOrgranBean mOrgranBeanB;FruitComponent mFruitComponent;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_test_scope);mFruitComponent = DaggerFruitComponent.builder().fruitModule(new FruitModule()).build();mFruitComponent.inject(this);// 完成注入,没有这句话是不行的Log.e("TAG", "mFruitComponent1:" + mFruitComponent.toString());Log.e("TAG", "mAppleBeanA:" + mAppleBeanA.toString());Log.e("TAG", "mAppleBeanB:" + mAppleBeanB.toString());Log.e("TAG", "mOrgranBeanA:" + mOrgranBeanA.toString());Log.e("TAG", "mOrgranBeanB:" + mOrgranBeanB.toString());}
}
如代码所示,在完成注入后分别对 AppleBean 、OrgranBean 分别调用了两次实例,按照上文中 我们对 @Scope 的理解,那么在这里两个类分别生成的类实例为同一个,下面我们运行代码,查看日志来验证一下。
打印日志如下:
E/TAG: mFruitComponent1:com.hirezy.dagger2demo.practiceFifth.DaggerFruitComponent@7c6e469
mAppleBeanA:com.hirezy.dagger2demo.practiceFifth.AppleBean@b7d6eee
mAppleBeanB:com.hirezy.dagger2demo.practiceFifth.AppleBean@b7d6eee
mOrgranBeanA:com.hirezy.dagger2demo.practiceFifth.OrgranBean@648ef8f
mOrgranBeanB:com.hirezy.dagger2demo.practiceFifth.OrgranBean@648ef8f
可以看到日志分别打印了 FruitComponent 的实例 – mFruitComponent,AppleBean 的两个实例 – mAppleBeanA、mAppleBeanB,OrgranBean 的两个实例 – mOrgranBeanA、mOrgranBeanB,发现 mAppleBeanA 和 mAppleBeanB 的哈希值相同即为 同一个对象、mOrgranBeanA、mOrgranBeanB 的哈希值相同即为 同一个对象,验证了我们对 @Scope 的认识 – 实现单例 。
public void jump(View view) {mFruitComponent = DaggerFruitComponent.builder().fruitModule(new FruitModule()).build();mFruitComponent.inject(this);// 完成注入,没有这句话是不行的Log.e("TAG", "mFruitComponent2:" + mFruitComponent.toString());Log.e("TAG", "mAppleBeanA:" + mAppleBeanA.toString());Log.e("TAG", "mAppleBeanB:" + mAppleBeanB.toString());Log.e("TAG", "mOrgranBeanA:" + mOrgranBeanA.toString());Log.e("TAG", "mOrgranBeanB:" + mOrgranBeanB.toString());}
3.7 运行程序查看日志信息:
03-10 22:15:54.903 14184-14184/com.hirezy.dagger2demo E/TAG: AppleBeanOrgranBeanmFruitComponent1:com.hirezy.dagger2demo.practiceFifth.DaggerFruitComponent@7c6e469mAppleBeanA:com.hirezy.dagger2demo.practiceFifth.AppleBean@b7d6eeemAppleBeanB:com.hirezy.dagger2demo.practiceFifth.AppleBean@b7d6eeemOrgranBeanA:com.hirezy.dagger2demo.practiceFifth.OrgranBean@648ef8f
03-10 22:15:54.904 14184-14184/com.hirezy.dagger2demo E/TAG: mOrgranBeanB:com.hirezy.dagger2demo.practiceFifth.OrgranBean@648ef8f
生成的对象和哈希值的对应关系为:
DaggerFruitComponent --> 7c6e469
AppleBean --> b7d6eee
OrgranBean --> 648ef8f
3.8 触发 jump() 方法
触发 jump() 方法后的日志信息如下:
03-10 22:16:22.624 14184-14184/com.hirezy.dagger2demo E/TAG: AppleBeanOrgranBeanmFruitComponent2:com.hirezy.dagger2demo.practiceFifth.DaggerFruitComponent@8196f9emAppleBeanA:com.hirezy.dagger2demo.practiceFifth.AppleBean@50ada7fmAppleBeanB:com.hirezy.dagger2demo.practiceFifth.AppleBean@50ada7fmOrgranBeanA:com.hirezy.dagger2demo.practiceFifth.OrgranBean@16bea4cmOrgranBeanB:com.hirezy.dagger2demo.practiceFifth.OrgranBean@16bea4c
生成的对象和哈希值的对应关系为:
FruitComponent --> 8196f9e
AppleBean --> 50ada7f
OrgranBean --> 16bea4c
很明显 AppleBean 、OrgranBean 重新生成了新的对象,难道不是单例了?难道上文的结论是错误的?其实不是这样的,因为 FruitComponent 生成了新的对象,所以其作用域下的类重新生成了新的实例。
证明了:在 Scope 注解标记 Component 的作用域内生产的实例是单例的。
4. 总结
至此,对 Dagger2 中的 Scope 的理解就如上文所示了。如果你是新手可以从头看Dagger 2 系列。
- Dagger 2 系列(一)——基础篇之依赖注入
- Dagger 2 系列(二)——@Inject、@Component 注解介绍
- Dagger 2 系列(三)——@Module 和 @Provides注解介绍
- Dagger 2 系列(四)——@Named 和 @Qualifier注解介绍
5. 参考资料
Dependency injection with Dagger 2 - the API
Dependency injection with Dagger 2 - Custom scopes