Dagger2原理直白

Dagger2原理直白

Android小彩虹2021-07-09 16:14:36120A+A-

还在使用接口解耦吗,为什么有看起来更加简洁的不开始使用尼?
我们项目中使用的目的:
以解耦为目的,避免了大量接口传参进行构造对象。

基于这个目的,我们再去看看他为我们到底做了什么事的。

使用的操作三联 1.@Inject

 public class PhotoNewsFragment extends BaseFragment<IBasePresenter> implements ILoadDataView<List<PhotoInfo>> {

    // 这个这BaseFragment中有使用
    //@Inject
    //protected T mPresenter;

    @Inject
    BaseQuickAdapter mAdapter;

    @Override
    protected void initInjector() {
        DaggerPhotoNewsComponent.builder()
                // 对全局的进行依赖  这里面可能会使用到这个全局的变量             .applicationComponent(getAppComponent())
                .photoNewsModule(new PhotoNewsModule(this))
                .build()
                .inject(this);
    }    
}

2.@module

@Module
public class PhotoNewsModule {

    private final PhotoNewsFragment mView;

    public PhotoNewsModule(PhotoNewsFragment view) {
        this.mView = view;
    }

    @PerFragment
    @Provides
    public BaseQuickAdapter provideAdapter() {
        return new PhotoListAdapter(mView.getContext());
    }
}

3.@Component

@PerFragment
@Component(modules = PhotoNewsModule.class)
public interface PhotoNewsComponent {
    void inject(PhotoNewsFragment fragment);
}

点击make Project 就可以是在第一个class中使用这个adapter了。

简单用会了,那就看看为什么我们能使用了,这个dagger2到底为我们私下做了什么操作。 我们将可以在这里看到它生成的一些列方法

app/build/generated/source/apt/debug/....

二,对应生成

1,@module

public final class PhotoNewsModule_ProvideAdapterFactory implements Factory<BaseQuickAdapter> {
  private final PhotoNewsModule module;

  public PhotoNewsModule_ProvideAdapterFactory(PhotoNewsModule module) {
    assert module != null;
    this.module = module;
  }

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

  public static Factory<BaseQuickAdapter> create(PhotoNewsModule module) {
    return new PhotoNewsModule_ProvideAdapterFactory(module);
  }
}

2,@Component

public final class DaggerPhotoNewsComponent implements PhotoNewsComponent {
  private Provider<IBasePresenter> providePresenterProvider;

  private Provider<BaseQuickAdapter> provideAdapterProvider;

  private MembersInjector<PhotoNewsFragment> photoNewsFragmentMembersInjector;

  private DaggerPhotoNewsComponent(Builder builder) {
    assert builder != null;
    initialize(builder);
  }

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

  @SuppressWarnings("unchecked")
  private void initialize(final Builder builder) {

    this.providePresenterProvider =
        DoubleCheck.provider(
            PhotoNewsModule_ProvidePresenterFactory.create(builder.photoNewsModule));

    this.provideAdapterProvider =
        DoubleCheck.provider(PhotoNewsModule_ProvideAdapterFactory.create(builder.photoNewsModule));

    this.photoNewsFragmentMembersInjector =
        PhotoNewsFragment_MembersInjector.create(providePresenterProvider, provideAdapterProvider);
  }

  @Override
  public void inject(PhotoNewsFragment fragment) {
    photoNewsFragmentMembersInjector.injectMembers(fragment);
  }

  public static final class Builder {
    private PhotoNewsModule photoNewsModule;

    private ApplicationComponent applicationComponent;

    private Builder() {}

    public PhotoNewsComponent build() {
      if (photoNewsModule == null) {
        throw new IllegalStateException(PhotoNewsModule.class.getCanonicalName() + " must be set");
      }
      if (applicationComponent == null) {
        throw new IllegalStateException(
            ApplicationComponent.class.getCanonicalName() + " must be set");
      }
      return new DaggerPhotoNewsComponent(this);
    }

    public Builder photoNewsModule(PhotoNewsModule photoNewsModule) {
      this.photoNewsModule = Preconditions.checkNotNull(photoNewsModule);
      return this;
    }

    public Builder applicationComponent(ApplicationComponent applicationComponent) {
      this.applicationComponent = Preconditions.checkNotNull(applicationComponent);
      return this;
    }
  }
}

3,当前Fragment生成的注解类

public final class PhotoNewsFragment_MembersInjector implements MembersInjector<PhotoNewsFragment> {
  private final Provider<IBasePresenter> mPresenterProvider;

  private final Provider<BaseQuickAdapter> mAdapterProvider;

  public PhotoNewsFragment_MembersInjector(
      Provider<IBasePresenter> mPresenterProvider, Provider<BaseQuickAdapter> mAdapterProvider) {
    assert mPresenterProvider != null;
    this.mPresenterProvider = mPresenterProvider;
    assert mAdapterProvider != null;
    this.mAdapterProvider = mAdapterProvider;
  }

  public static MembersInjector<PhotoNewsFragment> create(
      Provider<IBasePresenter> mPresenterProvider, Provider<BaseQuickAdapter> mAdapterProvider) {
    return new PhotoNewsFragment_MembersInjector(mPresenterProvider, mAdapterProvider);
  }

  @Override
  public void injectMembers(PhotoNewsFragment instance) {
    if (instance == null) {
      throw new NullPointerException("Cannot inject members into a null reference");
    }
    com.dl7.mvp.module.base.BaseFragment_MembersInjector.injectMPresenter(
        instance, mPresenterProvider);
        // 这里赋值.....
    instance.mAdapter = mAdapterProvider.get();
  }

  public static void injectMAdapter(
      PhotoNewsFragment instance, Provider<BaseQuickAdapter> mAdapterProvider) {
    instance.mAdapter = mAdapterProvider.get();
  }
}

4,BaseFragment生成的注解类

public final class BaseFragment_MembersInjector<T extends IBasePresenter>
    implements MembersInjector<BaseFragment<T>> {
  private final Provider<T> mPresenterProvider;

  public BaseFragment_MembersInjector(Provider<T> mPresenterProvider) {
    assert mPresenterProvider != null;
    this.mPresenterProvider = mPresenterProvider;
  }

  public static <T extends IBasePresenter> MembersInjector<BaseFragment<T>> create(
      Provider<T> mPresenterProvider) {
    return new BaseFragment_MembersInjector<T>(mPresenterProvider);
  }

  @Override
  public void injectMembers(BaseFragment<T> instance) {
    if (instance == null) {
      throw new NullPointerException("Cannot inject members into a null reference");
    }
    instance.mPresenter = mPresenterProvider.get();
  }

  public static <T extends IBasePresenter> void injectMPresenter(
      BaseFragment<T> instance, Provider<T> mPresenterProvider) {
      // 这里赋值....
    instance.mPresenter = mPresenterProvider.get();
  }
}

简单的几个注解,Dagger为我们生成了大量的代码。 那就开始顺着流程去看看代码。

 DaggerPhotoNewsComponent.builder()
      // 这里是因为我会用到Application 中的一些单例成员变量
                .applicationComponent(getAppComponent())
                .photoNewsModule(new PhotoNewsModule(this))
                .build()
                .inject(this);

这里不会对父类BaseFragment中的@Inject去说明,他比当前类中使用更复杂,还会用到Application中的Module的Provides。

我们看 DaggerPhotoNewsComponent.builder().

—->得到一个静态类 Builder

.photoNewsModule(new PhotoNewsModule(this))

—->静态类 Builder 中PhotoNewsModule 进行赋值 (这里PhotoNewsModule 就是我们自己写的,调用了构造传入了当前This(为一个View))

.build()

—->会执行new DaggerPhotoNewsComponent(this),并会调用初始化方法initialize(builder),在这个初始化方法中,会将Provide 生成的注解类执行
PhotoNewsModule_ProvideAdapterFactory.create(builder.photoNewsModule)方法。(里面传入的photoNewsModule)
也会执行当前Fragment生成注解类的 PhotoNewsFragment_MembersInjector.create(providePresenterProvider, provideAdapterProvider)

.inject(this);

photoNewsFragmentMembersInjector.injectMembers(fragment); 调用到这里 ,我们知道This是当前这个view,对里面的成员变量mAdapter进行了赋值,这个值就是生成的Module生成注解类,里面持有我们自己写的module,自己写的这个提供adapter的构造。

 @Override
  public void injectMembers(PhotoNewsFragment instance) {
    if (instance == null) {
      throw new NullPointerException("Cannot inject members into a null reference");
    }
    com.dl7.mvp.module.base.BaseFragment_MembersInjector.injectMPresenter(
        instance, mPresenterProvider);
    instance.mAdapter = mAdapterProvider.get();
  }

Dagger的目的只是为了解耦进行构造,让代码看起来更加优雅。 当其他方式使用的时候,@Inject 里面持有了View的应用,就可以回调了


点击这里复制本文地址 以上内容由权冠洲的博客整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!

支持Ctrl+Enter提交

联系我们| 本站介绍| 留言建议 | 交换友链 | 域名展示
本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除

权冠洲的博客 © All Rights Reserved.  Copyright quanguanzhou.top All Rights Reserved
苏公网安备 32030302000848号   苏ICP备20033101号-1

联系我们