- 该系列博客的最终目标: 搭建 MVP + Dagger2 框架
- 该系列博客包含以下几篇内容:
- Dagger 2 系列(一) – 前奏篇:依赖注入的基本介绍
- Dagger 2 系列(二) – 基础篇:@Inject、@Component
- Dagger 2 系列(三) – 基础篇:@Module 和@Provides
- Dagger 2 系列(四) – 基础篇:@Named 和 @Qualifier
- Dagger 2 系列(五) – 进阶篇:@Scope 和 @Singleton
在这篇文章中你会看到什么:
@Named
是什么@Qualifier
是什么@Named
和@Qualifier
用了解决什么问题
也许你会遇到这样的需求: 在同一个 Module 中 通过 @Provides 标记多个提供相同类的不同实现对象,那么你在编译时可以会遇到类似的报错信息:
错误: xxxx.UserThird is bound multiple times:
@Provides xxxUserThird xxxxx.UserThirdModule.provideUserThird()
@Provides xxx.UserThird xxx.UserThirdModule.provideUserThirdWithoutParams()
其大致信息为 UserThird
类被绑定了多次,并列举了绑定的信息。出错的原因是我们提供了返回值相同的创建类实例的方法,但是程序没有那么智能,它是无法判断出应该使用哪一个方法来创建实例,所以在编译器就会抛出异常,这种现象称为 依赖注入迷失
。
但是这样的操作在正常的业务中是在正常不过的了,是无法避免不去使用的。那么此时 @Named 和 @Qualifier 可以用来解决此类问题。
@Named
其具体使用方法为:
- 使用
@Named
标记 Module 中生成类实例的方法 - 使用
@Named
标记目标类中相应类实例
两步骤是缺一不可的。我们来看一下具体代码:
- POJO 类
public class UserThird {
private String mSex = "man";
private int mCarNum = 7;
public UserThird() {
}
public UserThird(String mSex, int mCarNum) {
this.mSex = mSex;
this.mCarNum = mCarNum;
}
// 变量的setter 和 getter 方法
.....
}
- Module 类
@Module
public class UserThirdModule {
@Named("a")
@Provides
UserThird provideUserThird(){
return new UserThird("男",1243);
}
@Named("b")
@Provides
UserThird provideUserThirdWithoutParams() {
return new UserThird();
}
}
- 目标类
public class ThirdActivity extends AppCompatActivity {
@Named("a")
@Inject
UserThird mUserTwoC;
@Named("b")
@Inject
UserThird mUserTwoD;
private static final String TAG = "SecondActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
DaggerUserThirdComponent.builder().userThirdModule(new UserThirdModule()).build().injectToThirdActivity(this);
Log.e(TAG, "onCreate: " + "sex" + mUserTwoC.getSex() + " number:" + mUserTwoC.getCarNum());
mUserTwoC.setCarNum(46);
mUserTwoC.setSex("女");
Log.e(TAG, "onCreate: " + "sex" + mUserTwoC.getSex() + " number:" + mUserTwoC.getCarNum());
Log.e(TAG, "onCreate: " + "sex" + mUserTwoD.getSex() + " number:" + mUserTwoD.getCarNum());
}
}
以上代码需要重点关注的是 Module 类 中使用 @Named
注解方法和在 目标类 中使用 @Named
注解标记类的实例变量,并且 Module 中的 @Named("a")
和 目标类中的 @Named("a")
是一一对应的。
@Qualifier
以上通过 @Named 实现的标识功能 @Qualifier 同样可以实现,但是需要我们自定义注解来完成,具体一个使用场景如下:
- 自定义注解
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface UserThirdQualifier {
String value() default "";
}
注意,这里自定义注解需要使用 @Qualifier
进行标注。
- Module 类中标记
@Module
public class UserThirdModule {
@UserThirdQualifier("c")
@Provides
UserThird provideUserThird(){
return new UserThird("男",1243);
}
@UserThirdQualifier("d")
@Provides
UserThird provideUserThirdWithoutParams() {
return new UserThird();
}
}
- 目标类
public class ThirdActivity extends AppCompatActivity {
@UserThirdQualifier("c")
@Inject
UserThird mUserTwoC;
@UserThirdQualifier("d")
@Inject
UserThird mUserTwoD;
private static final String TAG = "SecondActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
DaggerUserThirdComponent.builder().userThirdModule(new UserThirdModule()).build().injectToThirdActivity(this);
Log.e(TAG, "onCreate: " + "sex" + mUserTwoC.getSex() + " number:" + mUserTwoC.getCarNum());
mUserTwoC.setCarNum(46);
mUserTwoC.setSex("女");
Log.e(TAG, "onCreate: " + "sex" + mUserTwoC.getSex() + " number:" + mUserTwoC.getCarNum());
Log.e(TAG, "onCreate: " + "sex" + mUserTwoD.getSex() + " number:" + mUserTwoD.getCarNum());
}
}
同样的,以上代码需要重点关注的是 Module 类 中使用 @UserThirdQualifier
注解方法和在 目标类 中使用 @UserThirdQualifier
注解标记类的实例变量,并且 Module 中的 @UserThirdQualifier("c")
和 目标类中的 @UserThirdQualifier("c")
是一 一对应的。
总结
通过 @Named
和 @Qualifier
两个注解就可以做到标识在同一个 Module 中提供相同类实例对象而造成的 依赖注入迷失
问题 。