Dagger2入门系列二:Module&Component源码分析,android性能优化和内存优化

@OnClick(R.id.btn1)

public void onViewClicked() {

Toast.makeText(this, student.toString(), Toast.LENGTH_SHORT).show();

}

}

2、源码解析

======

打开目录结构:

Dagger2入门系列二:Module&Component源码分析,android性能优化和内存优化

我们不难发现,编译器已经帮我们生成了这样几个文件:

  • Student_Factory

  • DaggerTest1Component

  • Test1Activity_MembersInjector

  • Test1Activity_ViewBinding(不用管,butterknife)

我们一一进行分析:

2.1、Student_Factory


上一篇文章我们已经进行了分析,很简单,当我们@Inject注解一个类的构造方法时,编译器会自动帮我们生成一个工厂类,负责生产该类的对象,类似于商品的厂家

package com.gs.dagtest1.bean;

import dagger.internal.Factory;

public final class Student_Factory implements Factory {

private static final Student_Factory INSTANCE = new Student_Factory();

@Override

public Student get() {

return provideInstance();

}

public static Student provideInstance() {

return new Student();

}

public static Student_Factory create()

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

{

return INSTANCE;

}

public static Student newStudent() {

return new Student();

}

}

2.2、DaggerTest1Component


public final class DaggerTest1Component implements Test1Component {

private DaggerTest1Component(Builder builder) {}

public static Builder builder() {

return new Builder();

}

public static Test1Component create() {

return new Builder().build();

}

@Override

public void inject(Test1Activity activity) {

injectTest1Activity(activity);

}

private Test1Activity injectTest1Activity(Test1Activity instance) {

Test1Activity_MembersInjector.injectStudent(instance, new Student());

return instance;

}

public static final class Builder {

private Builder() {}

public Test1Component build() {

return new DaggerTest1Component(this);

}

/**

  • @deprecated This module is declared, but an instance is not used in the component. This

  • method is a no-op. For more, see https://google.github.io/dagger/unused-modules.
    

*/

@Deprecated

public Builder test1Module(Test1Module test1Module) {

Preconditions.checkNotNull(test1Module);

return this;

}

}

}

2.3、Test1Activity_MembersInjector


public final class Test1Activity_MembersInjector implements MembersInjector {

private final Provider studentProvider;

public Test1Activity_MembersInjector(Provider studentProvider) {

this.studentProvider = studentProvider;

}

public static MembersInjector create(Provider studentProvider) {

return new Test1Activity_MembersInjector(studentProvider);

}

@Override

public void injectMembers(Test1Activity instance) {

injectStudent(instance, studentProvider.get());

}

public static void injectStudent(Test1Activity instance, Student student) {

instance.student = student;

}

}

很熟悉,我们在Activity中就用到了这个生成的类,编译器起名方式也很简洁:Dagger+你的Component接口名。

在我们的Activity中我们是这样使用:

DaggerTest1Component.builder()

// .test1Module(new Test1Module(this))

.build()

.inject(this);

我们根据这个步骤查看源码,发现

DaggerTest1Component.builder().build()

实际上是通过建造者模式创建了一个新的DaggerTest1Component对象而已。

然后通过调用

DaggerTest1Component.java

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

@Override

public void inject(Test1Activity activity) {

injectTest1Activity(activity);

}

private Test1Activity injectTest1Activity(Test1Activity instance) {

Test1Activity_MembersInjector.injectStudent(instance, new Student());

return instance;

}

Test1Activity_MembersInjector.java

public static void injectStudent(Test1Activity instance, Student student) {

instance.student = student;

}

可以看到这些代码最终是把activity对象和student对象都 传入到了Test1Activity_MembersInjector的injectStudent()方法中。

2.4、Test1Activity_MembersInjector类,将Student和Activity进行连接


public final class Test1Activity_MembersInjector implements MembersInjector {

private final Provider studentProvider;

public Test1Activity_MembersInjector(Provider studentProvider) {

this.studentProvider = studentProvider;

}

public static MembersInjector create(Provider studentProvider) {

return new Test1Activity_MembersInjector(studentProvider);

}

@Override

public void injectMembers(Test1Activity instance) {

injectStudent(instance, studentProvider.get());

}

public static void injectStudent(Test1Activity instance, Student student) {

instance.student = student;

}

}

其实已经很简单了,在该Injector的injectMembers()方法中,已经将Student对象通过Student_Factory的get()方法获得,然后直接赋值给Activity的student对象了!

而create() 方法中传入的参数 Provider 就是Student_Factory工厂类。

public final class Student_Factory implements Factory {

上一篇:Singleton单例模式


下一篇:设计模式—单例模式