上次我们对dagger2进行简单的使用,这次我们来重点看下dagger2是怎么帮助我们实现对象的注入的。
当我们完成简单的代码后,进行编译,通过apt技术帮我们在build文件夹下生成下面的几个类。
编译后生成的代码.png
下面我们来详细拆解下APT生成的这三个类:
-
CommonMainActivity_MembersInjector:Activity的@Inject后编译APT生成,专门用来注入东西到-->MainActivity的
public final class CommonMainActivity_MembersInjector implements MembersInjector<CommonMainActivity> { // StudentModule_GetStudentFactory private final Provider<Student> studentProvider; public CommonMainActivity_MembersInjector(Provider<Student> studentProvider) { assert studentProvider != null; this.studentProvider = studentProvider; } // 提供一个静态创建的方法 public static MembersInjector<CommonMainActivity> create(Provider<Student> studentProvider) { return new CommonMainActivity_MembersInjector(studentProvider); } // 注入的方法 @Override public void injectMembers(CommonMainActivity instance) { if (instance == null) { throw new NullPointerException("Cannot inject members into a null reference"); } instance.student = studentProvider.get(); } public static void injectStudent(CommonMainActivity instance, Provider<Student> studentProvider) { instance.student = studentProvider.get(); } }
-
StudentModule_GetStudentFactory:StudentModule后编译APT生成,专门用来提供暴露出去的 StudentModule工厂
public final class StudentModule_GetStudentFactory implements Factory<Student> { private final StudentModule module; public StudentModule_GetStudentFactory(StudentModule module) { assert module != null; this.module = module; } @Override public Student get() { return Preconditions.checkNotNull(module.getStudent(), "Cannot return null from a non-@Nullable @Provides method"); } // 额外生成一个方法,为后续提供 public static Factory<Student> create(StudentModule module) { return new StudentModule_GetStudentFactory(module); } }
-
DaggerStudentComponent:StudentComponent后编译APT生成,对应StudentComponent生成的类文件
// 这个是编译期 APT 自动生成的 public final class DaggerStudentComponent implements StudentComponent { // StudentModule_GetStudentFactory , private Provider<Student> getStudentProvider; // CommonMainActivity_MembersInjector private MembersInjector<CommonMainActivity> commonMainActivityMembersInjector; private DaggerStudentComponent(Builder builder) { assert builder != null; initialize(builder); } public static Builder builder() { return new Builder(); } public static StudentComponent create() { return builder().build(); } @SuppressWarnings("unchecked") private void initialize(final Builder builder) { this.getStudentProvider = StudentModule_GetStudentFactory.create(builder.studentModule); this.commonMainActivityMembersInjector = CommonMainActivity_MembersInjector.create(getStudentProvider); } @Override public void injectMainActivity(CommonMainActivity commonMainActivity) { commonMainActivityMembersInjector.injectMembers(commonMainActivity); } public static final class Builder { // private StudentModule studentModule; private Builder() {} public StudentComponent build() { if (studentModule == null) { this.studentModule = new StudentModule(); } return new DaggerStudentComponent(this); } public Builder studentModule(StudentModule studentModule) { this.studentModule = Preconditions.checkNotNull(studentModule); return this; } } }
具体执行流程
DaggerStudentComponent.create()
--> return builder()
// 这里使用了构建者模式,返回了DaggerStudentComponent对象
--> return new DaggerStudentComponent(this);
// 在DaggerStudentComponent的构造方法中,对getStudentProvider和commonMainActivityMembersInjector进行了初始化
--> this.getStudentProvider = StudentModule_GetStudentFactory.create(builder.studentModule);@DaggerStudentComponent
--> return new StudentModule_GetStudentFactory(module);
--> this.module = module;@StudentModule_GetStudentFactory
this.commonMainActivityMembersInjector = CommonMainActivity_MembersInjector.create(getStudentProvider);
--> return new CommonMainActivity_MembersInjector(studentProvider);
--> this.studentProvider = studentProvider;@CommonMainActivity_MembersInjector
.build(); @DaggerStudentComponent
--> if (studentModule == null) {
this.studentModule = new StudentModule();
--> return new Student();
}
return new DaggerStudentComponent(this);
.injectMainActivity(this);
// commonMainActivityMembersInjector 是在DaggerStudentComponent构造方法中赋值的。
// this.commonMainActivityMembersInjector = CommonMainActivity_MembersInjector.create(getStudentProvider);
--> commonMainActivityMembersInjector.injectMembers(commonMainActivity);@DaggerStudentComponent
// studentProvider是CommonMainActivity_MembersInjector构造方法传进来的。
// 对activity中的student对象进行赋值。这就是为什么acitivty定义的时候不能写private。
--> instance.student = studentProvider.get();@CommonMainActivity_MembersInjector
网友评论