Dagger2依赖关系分析

前端之家收集整理的这篇文章主要介绍了Dagger2依赖关系分析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

对应的PerActivity.java注解代码

@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface PerActivity {

}


对应的Component文件

// AppComponent.java文件
@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {
  public Activity getActivity();
  void inject(App app);
  void inject(BaseActivity baseActivity);
}

// ActivityComponent.java文件
@PerActivity
@Component(dependencies = AppComponent.class,modules = ActivityModule.class)
public interface ActivityComponent {
    public Activity getActivity();
}


对应的Module文件
// AppModule.java文件
@Module
public class AppModule {
    private final Context mContext;

    public AppModule(Context context) {
        this.mContext = context;
    }

    @Provides @Singleton
    public Context provideAppContext() {
        return mContext.getApplicationContext();
    }

    @Provides @Singleton
    public Activity provideActivity() {
        return null;
    }
}

// ActivityModule.java文件
@Module
public class ActivityModule {
    private final Activity mActivity;

    public ActivityModule(Activity activity) {
        this.mActivity = activity;
    }

}


重点从ActivityComponent.java文件开始分析,它里面有个getActivity()方法。说明是需要其他提供者提供的。

那么到ActivityModule.java文件中去找,并没有发现有提供Activity的方法。那么继续到他的dependenceies中去找,看到AppComponent文件中有

getActivity()方法,而对应的AppModule中有提供Activity的方法


也就是ActivityComponent中的getActivity()--->AppComponent中的getActivity()--->AppModule中的提供Activity方法


生成的对应的源码来看:

public final class DaggerAppComponent implements AppComponent {
  private Provider<Activity> provideActivityProvider;

<pre name="code" class="java">  // ...省略
private void initialize(final Builder builder) { this.provideActivityProvider = ScopedProvider.create(AppModule_ProvideActivityFactory.create(builder.appModule)); } @Override public Activity getActivity() { return provideActivityProvider.get(); } // ...省略}
 


@Generated("dagger.internal.codegen.ComponentProcessor")
public final class AppModule_ProvideActivityFactory implements Factory<Activity> {
  private final AppModule module;

  public AppModule_ProvideActivityFactory(AppModule module) {  
    assert module != null;
    this.module = module;
  }

  @Override
  public Activity get() {  
    Activity provided = module.provideActivity();
    if (provided == null) {
      throw new NullPointerException("Cannot return null from a non-@Nullable @Provides method");
    }
    return provided;
  }

  public static Factory<Activity> create(AppModule module) {  
    return new AppModule_ProvideActivityFactory(module);
  }
}


从这里的DaggerAtivityComponent中的getActivity()方法可以看出,调用的是getActivityProvider.get()方法。而这里的

getActivityProvider是由AppComponent中的getActivity()变换生成的。也验证了@Component(dependencies = AppComponent.class...。

@Generated("dagger.internal.codegen.ComponentProcessor")
public final class DaggerActivityComponent implements ActivityComponent {
  private Provider<Activity> getActivityProvider;

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

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

  private void initialize(final Builder builder) {  
    this.getActivityProvider = new Factory<Activity>() {
      private final AppComponent appComponent = builder.appComponent;
      @Override public Activity get() {
        Activity provided = appComponent.getActivity();
        if (provided == null) {
          throw new NullPointerException("Cannot return null from a non-@Nullable component method");
        }
        return provided;
      }
    };
  }

  @Override
  public Activity getActivity() {  
    return getActivityProvider.get();
  }

  // ...省略
}


总结:

(1)在Component中如果有相关的getXXX()方法,必然需要在Module中提供XXX实例或者在其他的dependenceies中提供XXX实例。

这里假如将AppComponent中的Activity getActivity()注释掉,则会发生如下错误

Error:(13,21) 错误: android.app.Activity cannot be provided without an @Inject constructor or from an @Provides-annotated method.


这里说明的就是ActivityComponent中的getActivity()中缺少,提供Activity的方法

@PerActivity
@Component(dependencies = AppComponent.class,modules = ActivityModule.class)
public interface ActivityComponent {
    public Activity getActivity();
}
原文链接:https://www.f2er.com/javaschema/283849.html

猜你在找的设计模式相关文章