关于Dagger2
- Dagger基础用法
- 对注解的分类讲解——Inject部分
- 对注解的分类讲解——Module部分(一)
- 对注解的分类讲解——Component部分
- 对注解的分类讲解——Module(二)
- 对注解的分类讲解——Module(三)
- 对Dagger中几个类的说明
- Dagger-Android的使用
前言
本文将通过对注解的分类讲解来交代Dagger2的进阶用法。
关于Dagger的注解,简单将它们分成三类:
- Component部分
- Module部分
- Inject部分

来对应Dagger完成依赖注入的三部分内容,接下来来了解一下关于Module部分的最终注解吧,这部分注解都是通过某种容器来实现对注入对象的管理。
试想一下有这样一个场景,假如我们需要在Module里提供了很多相同类型的 对象,如果我们不使用Qualifer,就会导致同一类型重复绑定的错误。但是如果我们确实需要在一个Module里包含这些对象的创建,又不想创建N多的Qualifer,我们就可以使用MultiBind机制来达到我们的目的。
Module部分注解
@IntoSet && @IntoMap
一般的MultiBind机制提供了两种容器来管理我们的依赖对象:Set和Map两种方式,它们有一个共同的特点都是无序的,由于我们的注入对象只需要在Dagger中提供就可以获取到,所以并不需要顺序。
@IntoSet
@IntoSet注解用来通过Set容器对所要注入的对象进行管理。
@IntoMap
@IntoMap注解用来通过Map容器对所要注入的对象进行管理。
以下注解用于提供不同类型的key:
- @IntKey
- @MapKey
- @ClassKey
- @LongKey
- @StringKey
具体实现
创建一个业务类:
public class IntoAnnotations {
private String config;
public IntoAnnotations(String config) {
this.config = config;
}
public String getConfig() {
return config;
}
}
实现Module,将所需要的依赖提供出来:
@Module
public class IntoAnnotationsModule {
// 通过Set容器对依赖对象进行管理,这里提供了四个不同的对象
@Provides
@IntoSet
IntoAnnotations providerIntoAnnotationsSetFirst() {
return new IntoAnnotations("first");
}
@Provides
@IntoSet
IntoAnnotations providerIntoAnnotationsSetSecond() {
return new IntoAnnotations("second");
}
@Provides
@IntoSet
IntoAnnotations providerIntoAnnotationsSetThird() {
return new IntoAnnotations("third");
}
@Provides
@IntoSet
IntoAnnotations providerIntoAnnotationsSetFourth() {
return new IntoAnnotations("fourth");
}
// 通过Map容器对依赖对象进行管理,这里提供了四个不同的对象
@Provides
@IntoMap
@StringKey("first")
IntoAnnotations providerIntoAnnotationsMapFirst() {
return new IntoAnnotations("first");
}
@Provides
@IntoMap
@StringKey("second")
IntoAnnotations providerIntoAnnotationsMapSecond() {
return new IntoAnnotations("second");
}
@Provides
@IntoMap
@StringKey("third")
IntoAnnotations providerIntoAnnotationsMapThird() {
return new IntoAnnotations("third");
}
@Provides
@IntoMap
@StringKey("fourth")
IntoAnnotations providerIntoAnnotationsMapFourth() {
return new IntoAnnotations("fourth");
}
}
最后创建Component类来建立起对象类和Activity之间的关系:
@Component(modules = IntoAnnotationsModule.class)
public interface IntoAnnotationsComponent {
void inject(IntoAnnotationsActivity activity);
}
在Activity中调用:
public class IntoAnnotationsActivity extends AppCompatActivity {
// 注入Set容器管理的对象集合
@Inject
Set<IntoAnnotations> mAnnotationsSet;
// 注入Map容器管理的对象集合
@Inject
Map<String, IntoAnnotations> mIntoAnnotationsMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_into_annotations);
DaggerIntoAnnotationsComponent.create().inject(this);
// 获取Set集合中的对象
Log.d("Dagger测试", "IntoAnnotationsSet: " + ((IntoAnnotations)mAnnotationsSet.toArray()[0]).getConfig());
Log.d("Dagger测试", "IntoAnnotationsSet: " + ((IntoAnnotations)mAnnotationsSet.toArray()[1]).getConfig());
Log.d("Dagger测试", "IntoAnnotationsSet: " + ((IntoAnnotations)mAnnotationsSet.toArray()[2]).getConfig());
Log.d("Dagger测试", "IntoAnnotationsSet: " + ((IntoAnnotations)mAnnotationsSet.toArray()[3]).getConfig());
// 获取Map集合中的对象
Log.d("Dagger测试", "FirstIntoAnnotationsMap: " + mIntoAnnotationsMap.get("first").getConfig());
Log.d("Dagger测试", "SecondIntoAnnotationsMap: " + mIntoAnnotationsMap.get("second").getConfig());
Log.d("Dagger测试", "ThirdIntoAnnotationsMap: " + mIntoAnnotationsMap.get("third").getConfig());
Log.d("Dagger测试", "FourthIntoAnnotationsMap: " + mIntoAnnotationsMap.get("fourth").getConfig());
}
}
来看一下打印的结果:

可以看到我们分别从Set和Map中获取到了有关的对象的相关信息,通过这种方式生成的不同对象更加直观同时也更加易于管理。
@ElementsIntoSet
@ElementsIntoSet一般不常用,是配合@IntoSet来使用的,通过@ElementsIntoSet注解可以将一个集合注入到Set容器进行管理。
@Multibinds
对于至少有@IntoSet,@ElementsIntoSet,或@IntoMap绑定中一个的set或map没必要使用@Multibinds,但如果用于管理依赖的那个容器可能为空时则必须要添加此声明。
网友评论