Skip to content

Latest commit

 

History

History
196 lines (154 loc) · 5.5 KB

9.Dagger2原理分析(九).md

File metadata and controls

196 lines (154 loc) · 5.5 KB

Dagger2原理分析(九)

在看这篇文章前,强烈建议看一下注解使用 因为这篇文章只是分析Dagger2自动生成的代码,有关如何把注解生成代码的过程,就需要通过上面这篇文章来看了。

写完用法之后,不看看源码,老是感觉好像还少了点什么?那这里就分析下源码。 用一个最简单的例子来分析:

public class A {
    void print() {
        Log.i("@@@", "A");
    }
}

@Module
public class AModule {
    @Provides
    A provideA() {
        return new A();
    }
}

@Component(modules = AModule.class)
public interface AComponent {
    void inject(MainActivity activity);
}

public class MainActivity extends Activity {
    @Inject A a;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DaggerAComponent.builder().build().inject(this);
        a.print();
    }
}

首先我们看一下dagger自动生成的代码:

这里一共自动生成了三个类,通过名字我们很简单的能看出他们的对应关系,这里就从Module类开始一个个的说:

public final class AModule_ProvideAFactory implements Factory<A> {
  private final AModule module;

  public AModule_ProvideAFactory(AModule module) {
    this.module = module;
  }

  @Override
  public A get() {
    return provideInstance(module);
  }

  public static A provideInstance(AModule module) {
    return proxyProvideA(module);
  }

  public static AModule_ProvideAFactory create(AModule module) {
    return new AModule_ProvideAFactory(module);
  }

  public static A proxyProvideA(AModule instance) {

  	// 这里可以看到他内部其实就是调用了我们写的AModule里面的provideA()方法
    return Preconditions.checkNotNull(
        instance.provideA(), "Cannot return null from a non-@Nullable @Provides method");
  }
}

从前面几篇文章我们知道ComponentModule和需求方的桥梁,所以我们看一下对应的DaggerAComponent的源码:

public final class DaggerAComponent implements AComponent {
  private AModule aModule;

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

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

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

  @SuppressWarnings("unchecked")
  private void initialize(final Builder builder) {
    this.aModule = builder.aModule;
  }

  @Override
  public void inject(MainActivity activity) {
    injectMainActivity(activity);
  }

  private MainActivity injectMainActivity(MainActivity instance) {
  	// 会将创建的Module对象传递到AModule_ProvideAFactory中,AModule_ProvideAFactory.proxyProvideA就是调用Module中的provideA()方法
    MainActivity_MembersInjector.injectA(instance, AModule_ProvideAFactory.proxyProvideA(aModule));
    return instance;
  }

  public static final class Builder {
    private AModule aModule;

    private Builder() {}

    public AComponent build() {
    	// Component里面会创建Module类
      if (aModule == null) {
        this.aModule = new AModule();
      }
      return new DaggerAComponent(this);
    }

    public Builder aModule(AModule aModule) {
      this.aModule = Preconditions.checkNotNull(aModule);
      return this;
    }
  }
}

我们在Activity中都要手动的去调用DaggerAComponentinject方法:

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

我们看一下inject方法的源码:

  @Override
  public void inject(MainActivity activity) {
    injectMainActivity(activity);
  }

  private MainActivity injectMainActivity(MainActivity instance) {
    MainActivity_MembersInjector.injectA(instance, AModule_ProvideAFactory.proxyProvideA(aModule));
    return instance;
  }

上面AModule_ProvideAFactory.proxyProvideA(aModule)其实就是调用了AModule里面的provideA()方法,这里会生成一个A类的对象。 那MainActivity_MembersInjector.injectA()方法的源码又是什么呢? 这里继续看一下MainActivity_MembersInjector类的源码:

public final class MainActivity_MembersInjector implements MembersInjector<MainActivity> {
  private final Provider<A> aProvider;

  public MainActivity_MembersInjector(Provider<A> aProvider) {
    this.aProvider = aProvider;
  }

  public static MembersInjector<MainActivity> create(Provider<A> aProvider) {
    return new MainActivity_MembersInjector(aProvider);
  }

  @Override
  public void injectMembers(MainActivity instance) {
    injectA(instance, aProvider.get());
  }

  public static void injectA(MainActivity instance, A a) {
    instance.a = a;
  }
}

它里面其实就是讲MainActivity对象里面的成员变量a复制给我们通过参数传递过来的A类的对象。

看到这里明白了吗?

public class MainActivity extends Activity {
    @Inject A a;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DaggerAComponent.builder().build().inject(this);
        a.print();
    }
}

所以我们在MainActivity中直接使用变量a的时候,其实a已经复制给了我们在Module.provideA()中创建的A类的对象。