Dagger2入门系列二:Module&Component源码分析
发布日期:2021-05-14 17:56:16 浏览次数:46 分类:精选文章

本文共 3184 字,大约阅读时间需要 10 分钟。

Dagger2 是一款功能强大的依赖注入框架,它通过注解简化了对依赖的管理。在 Dagger2 中,@Inject、@Component、@Module 是最常用的三个注解,它们共同构成了一个高效的依赖注入系统。

1、@Inject:自动生成工厂类

在 Dagger2 中,@Inject 注解被用来注入依赖关系。使用 @Inject 注解的构造方法会自动编译生成一个 Factory 工厂类。这意味着当系统需要创建某个类的对象时,可以直接通过工厂类获取实例,而无需显式地调用新建方法。

例如,以下代码中,Student 类的构造方法被 @Inject 注解:

public class Student {    
@Inject
public Student() {}
}

编译器会自动生成一个 Student_Factory 类,该类负责生产 Student 对象。这个工厂类的作用类似于传统的工厂模式。

2、@Component:注入器与模块

@Component 是 Dagger2 中的核心注解之一,它用于定义一个注入器(相当于快递员)。这个注入器的职责是将需要的对象注入到其他地方。与此同时,@Component 也可以指定需要使用的模块,模块类似于快递箱子,用于集中管理和提供所需的依赖。

例如,以下代码定义了一个 Test1Component:

@Service
public interface Test1Component {
void inject(Test1Activity activity);
}

在 @Component 注解中,我们可以指定需要使用的模块:

@Component(modules = Test1Module.class)
public interface Test1Component {
void inject(Test1Activity activity);
}

3、@Module:依赖管理中心

@Module 类似于一个快递箱子,用来集中管理所有需要的依赖。在 @Module 中,我们可以定义需要的字段以及它们的生命周期。例如:

@Module
public class Test1Module {
private Test1Activity activity;
Test1Module(Test1Activity activity) {
this.activity = activity;
}
}

通过 @Module 注解,我们告诉 Dagger2 这个模块提供了 Test1Activity 对象。Dagger2 会在运行时将 Test1Activity 实例注入到其他地方需要的位置。

4、代码与源码分析

假设我们有以下代码:

public class Student {    
@Inject
public Student() {}
}
@Module
public class Test1Module {
private Test1Activity activity;
Test1Module(Test1Activity activity) {
this.activity = activity;
}
}
@Component(modules = Test1Module.class)
public interface Test1Component {
void inject(Test1Activity activity);
}
public class Test1Activity extends AppCompatActivity {
@Inject
Student student;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test1);
ButterKnife.bind(this);
initDatas();
}
private void initDatas() {
DaggerTest1Component.builder()
.build()
.inject(this);
}
@OnClick(R.id.btn1)
public void onViewClicked() {
Toast.makeText(this, student.toString(), Toast.LENGTH_SHORT).show();
}
}

当代码运行时,Dagger2 会生成以下文件:

  • Student_Factory:负责生产 Student 对象
  • DaggerTest1Component:注入器,负责将 Test1Module 提供的依赖注入到 Test1Activity 中
  • Test1Activity_MembersInjector:负责将 Student 对象注入到 Test1Activity 中

5、源码解析

让我们深入分析这些生成的文件。

5.1、Student_Factory

Student_Factory 是编译生成的工厂类,用于生产 Student 对象。它继承自 Factory 接口,并提供一个 newStudent() 方法。

5.2、DaggerTest1Component

DaggerTest1Component 是一个由 Dagger2 自动生成的注入器。它通过注入模块和依赖关系,将 Test1Activity 注入相关位置。

5.3、Test1Activity_MembersInjector

Test1Activity_MembersInjector 负责将 Student 对象注入到 Test1Activity 中。它通过 injectMembers 方法将不含依赖关系的 Test1Activity 注入到 Test1Activity_MembersInjector 中,并调用 injectStudent 方法,将 Student 对象注入到 Test1Activity 中。

6、实际应用

在 Test1Activity 中,我们可以通过以下方式注入 Student 对象:

DaggerTest1Component.builder()
.build()
.inject(this);

这条代码实际上是调用 DaggerTest1Component 的建造者模式,创建一个新的 DaggerTest1Component 实例,并调用 inject 方法,将 Test1Activity 注入到 Test1Component 中。

经过以上步骤,可以看到 Dagger2 是如何通过编译生成的 Factory、Component、MembersInjector 类,实现了依赖注入的高效管理。

通过理解这些概念和机制,我们可以更好地利用 Dagger2 来管理应用程序中的依赖关系,提升代码的可维护性和灵活性。

上一篇:Anim文件(动画)
下一篇:Dagger2入门系列一:基础使用

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2025年04月20日 19时52分04秒