开发配置与安卓第三方库应用Android开发Android技术知识

Android开发之Dagger2--Module&Co

2018-03-26  本文已影响657人  Jackson杰

前言

上一篇文章中Android开发之Dagger2--源码角度分析inject过程(二),主要通过源码分析了注入过程,但是还有一个遗留问题,那就是Module和Component各有什么用,该怎么理解,我们将在这篇文章中讨论。

代码回顾分析

public class Student {

    private String mess="Student的实例是注解方式注入的";

   @Inject
    public Student(){

    }

    public String showMessage(){
        return mess;
    }

}
@Module
public class MainModule {

}
@Singleton
@Component(modules = MainModule.class)
public interface MainComponent {
    void inject(Daggertest1Activity daggertest1Activity);
}
@Inject
    Student mStudent;  // 注入Studnet的实例

  DaggerMainComponent.builder()
                .mainModule(new MainModule())
                .build()
                .inject(this);

Module&Component

那么Module到底是做什么的呢,我们修改一下代码。

public class Student {

    private String mess="Student的实例是注解方式注入的";

    public Student(){

    }

    public String showMessage(){
        return mess;
    }


@Module
public class MainModule {

    public MainModule(){

    }

    @Provides
    @Singleton
    Student provideStudent(){
        return new Student();
    }
}

其他地方都不变,先把app/build文件夹删除,我们编译后重新运行。
效果图就不贴了,一样的效果。
这时打开app\build\generated\source\apt\debug\com\jackson\daggertest目录,可以看到照样是生成了三个文件, MainModule_ProvideStudentFactory.java,DaggerMainComponent.java,Daggertest1Activity_MembersInjector.java,我们继续简要的分析一下源码。

public final class MainModule_ProvideStudentFactory implements Factory<Student> {
  private final MainModule module;

  public MainModule_ProvideStudentFactory(MainModule module) {
    this.module = module;
  }

  @Override
  public Student get() {
    return Preconditions.checkNotNull(
        module.provideStudent(), "Cannot return null from a non-@Nullable @Provides method");
  }

  public static MainModule_ProvideStudentFactory create(MainModule module) {
    return new MainModule_ProvideStudentFactory(module);
  }

  public static Student proxyProvideStudent(MainModule instance) {
    return Preconditions.checkNotNull(
        instance.provideStudent(), "Cannot return null from a non-@Nullable @Provides method");
  }
}

我们是在MainModule里缇欧拱了一个Provide方法,在方法里提供了Student的实体类,可以看到,编译以后生成一个MainModule_ProvideStudentFactory工厂,只不过这个工厂必须要传入MainModule的实例才能实例化。

同样,create()方法提供工厂实例

 public static MainModule_ProvideStudentFactory create(MainModule module) {
    return new MainModule_ProvideStudentFactory(module);
  }

get()方法获取Student实例

@Override
  public Student get() {
    return Preconditions.checkNotNull(
        module.provideStudent(), "Cannot return null from a non-@Nullable @Provides method");
  }
public final class DaggerMainComponent implements MainComponent {
  private Provider<Student> provideStudentProvider;

  private DaggerMainComponent(Builder builder) {
    initialize(builder);
  }

  public static Builder builder() {
    return new Builder();
  }

  public static MainComponent create() {
    return new Builder().build();
  }

  @SuppressWarnings("unchecked")
  private void initialize(final Builder builder) {
    this.provideStudentProvider =
        DoubleCheck.provider(MainModule_ProvideStudentFactory.create(builder.mainModule));
  }

  @Override
  public void inject(Daggertest1Activity daggertest1Activity) {
    injectDaggertest1Activity(daggertest1Activity);
  }

  private Daggertest1Activity injectDaggertest1Activity(Daggertest1Activity instance) {
    Daggertest1Activity_MembersInjector.injectMStudent(instance, provideStudentProvider.get());
    return instance;
  }

  public static final class Builder {
    private MainModule mainModule;

    private Builder() {}

    public MainComponent build() {
      if (mainModule == null) {
        this.mainModule = new MainModule();
      }
      return new DaggerMainComponent(this);
    }

    public Builder mainModule(MainModule mainModule) {
      this.mainModule = Preconditions.checkNotNull(mainModule);
      return this;
    }
  }
}

变化不大,多了一个initialize()方法

@SuppressWarnings("unchecked")
  private void initialize(final Builder builder) {
    this.provideStudentProvider =
        DoubleCheck.provider(MainModule_ProvideStudentFactory.create(builder.mainModule));
  }

我们看到有个DoubleCheck,调用了DoubleCheck.provider()方法,我们点进去

 /** Returns a {@link Provider} that caches the value from the given delegate provider. */
  // This method is declared this way instead of "<T> Provider<T> provider(Provider<T> delegate)"
  // to work around an Eclipse type inference bug: https://github.com/google/dagger/issues/949.
  public static <P extends Provider<T>, T> Provider<T> provider(P delegate) {
    checkNotNull(delegate);
    if (delegate instanceof DoubleCheck) {
      /* This should be a rare case, but if we have a scoped @Binds that delegates to a scoped
       * binding, we shouldn't cache the value again. */
      return delegate;
    }
    return new DoubleCheck<T>(delegate);
  }

可以看到,这是一个双重锁提供的单例方法,这是延迟加载的需要,更详细的请参考<Effective Java 2> item 71

总结

综上,通过对上述两种注入方法的分析,可以看到,要注入一个类,可以通过两种方法:

上一篇下一篇

猜你喜欢

热点阅读