如何使用Dagger2注入改造

时间:2018-06-27 12:23:39

标签: android dagger-2

我正在尝试使用Dagger2实现MVP模式。虽然我成功地为应用程序,活动和片段做了di(我不确定片段是否做得很好)。实际上,在阅读指南之后,我仍然不了解它的工作原理。

我创建了RetrofitModiule:

@Module
public class RetrofitModule {
     String mBaseUrl;
...
@Provides
@Singleton
Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
    return new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(gson))
            .baseUrl(mBaseUrl)
            .client(okHttpClient)
            .build();
}

然后我在MyApplicationComponent中声明模块:

@Singleton
@Component(
        modules = {
                MyApplicationModule.class,
                RetrofitModule.class
        }
)
public interface MyApplicationComponent {
     void inject(MyApplication myApplication);
     Retrofit provideRetrofit();
}

实际上,我不明白为什么我必须在这里使用Inject;因为实际上没有什么可以注入MyApplication中:

public class MyApplication extends Application {
    private MyApplicationComponent mMyApplicationComponent;
    ...
    @Override
    public void onCreate() {
        super.onCreate();
        mMyApplicationComponent = DaggerMyApplicationComponent.builder()
            .retrofitModule(new RetrofitModule("https://androidtutorialpoint.com"))
            .build();
        mMyApplicationComponent.inject(this);
    }
}

我仅在注入LoaderActivity的LoaderActivityPresenterImpl中使用Retrofit;

@ActivityScoped
public class LoaderActivityPresenterImpl implements LoaderActivityPresenter {

private LoaderActivityView mView;
private @ActivityContext Context mContext;
private Retrofit mRetrofit;

@Inject
public LoaderActivityPresenterImpl(LoaderActivityView view, @ActivityContext Context context, Retrofit retrofit) {
    mView = view;
    mContext = context;
    mRetrofit = retrofit;
}
}

LoaderActivity:

public class LoaderActivity extends BaseActivity implements LoaderActivityView{

@Inject LoaderActivityPresenter mPresenter;
private LoaderActivityComponent mLoaderActivityComponent;

@Override
protected void setupActivityComponent(MyApplicationComponent myApplicationComponent) {
    mLoaderActivityComponent = DaggerLoaderActivityComponent.builder()
            .myApplicationComponent(myApplicationComponent)
            .loaderActivityModule(new LoaderActivityModule(this, this, myApplicationComponent.provideRetrofit()))
            .build();
    mLoaderActivityComponent.inject(this);
}

LoaderComponent:

@ActivityScoped
@Component(
    modules = LoaderActivityModule.class,
    dependencies = MyApplicationComponent.class
)
public interface LoaderActivityComponent {

void inject(LoaderActivity loaderActivity);

}

LoaderActivityModule:

@Module
public class LoaderActivityModule {

private Retrofit mRetrofit;
private LoaderActivityView mLoaderActivityView;
private @ActivityContext Context mContext;

public LoaderActivityModule(LoaderActivityView loaderActivityView, @ActivityContext Context context, Retrofit retrofit) {
    mLoaderActivityView = loaderActivityView;
    mContext = context;
    mRetrofit = retrofit;
}

@Provides
LoaderActivityView provideLoaderActivityView() {
    return mLoaderActivityView;
}

@Provides
public @ActivityContext Context provideActivityContext() {
    return mContext;
}

@Provides
public LoaderActivityPresenter LoaderActivityPresenterImpl() {
    return new LoaderActivityPresenterImpl(mLoaderActivityView, mContext, mRetrofit);
}

}

LoaderActivityComponent:

@ActivityScoped
@Component(
    modules = LoaderActivityModule.class,
    dependencies = MyApplicationComponent.class
)
public interface LoaderActivityComponent {

void inject(LoaderActivity loaderActivity);

}

我收到此错误:

java.lang.RuntimeException: Unable to create application com.xxxxx.application.MyApplication: java.lang.IllegalStateException: com.xxxxx.di.modules.MyApplicationModule must be set;

我可能会忘记展示一些课程,所以随时问我。

1 个答案:

答案 0 :(得分:0)

如错误所述,您忘记将ApplicationModule添加到组件中。 顺便提一下,我强烈建议您看看AndroidInjector,以避免手动创建此Android组件层次结构。

public class MyApplication extends Application {
private MyApplicationComponent mMyApplicationComponent;
...
@Override
public void onCreate() {
    super.onCreate();
    mMyApplicationComponent = DaggerMyApplicationComponent.builder()
        .myApplicationModule()
        .retrofitModule(new RetrofitModule("https://androidtutorialpoint.com"))
        .build();
    mMyApplicationComponent.inject(this);