如果没有@ Provide-annotated方法,则无法提供Dagger 2

时间:2017-01-08 14:27:04

标签: java android dependency-injection dagger-2 dagger

我知道有很多类似的问题,但我仍然无法找到问题的解决方案,在这个阶段,我没有想法。我有以下设置:

  • 应用程序模块/组件:仅用于上下文和Application对象。
  • 网络模块/组件:改造客户端
  • 城市模块/组件:在MVP屏幕中注入依赖关系的模块/组件。我想在片段中注入Presenter和Interactor。
  • PlaceRequests:改造界面

这是代码的外观:

ApplicationModule.java

/blogs/

ApplicationComponent.java

@Module
public class ApplicationModule {
    private Application mApp;

    public ApplicationModule(Application app) {
        mApp = app;
    }

    @Provides
    @Singleton
    public Application provideApplication() {
        return mApp;
    }

    @Provides
    @Singleton
    Context provideApplicationContext() {
        return mApp.getApplicationContext();
    }
}

NetModule.java

@Singleton
@Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {
    Application application();
    Context getContext();
}

NetComponent.java

@Module
public class NetModule {

    String mBaseUrl;

    public NetModule(String baseUrl) {
        this.mBaseUrl = baseUrl;
    }

    @Provides
    @Singleton
    SharedPreferences providesSharedPreferences(Application application) {
        return PreferenceManager.getDefaultSharedPreferences(application);
    }

    @Provides
    @Singleton
    Cache provideOkHttpCache(Application application) {
        int cacheSize = 10 * 1024 * 1024; // 10 MiB
        return new Cache(application.getCacheDir(), cacheSize);
    }

    @Provides
    @Singleton
    OkHttpClient provideOkHttpClient(Cache cache) {
        OkHttpClient client = new OkHttpClient();
        client.setCache(cache);
        return client;
    }

    @Provides
    @Singleton
    Retrofit provideRetrofit(OkHttpClient okHttpClient) {
        return new Retrofit.Builder()
                .addConverterFactory(JacksonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(mBaseUrl)
                .client(okHttpClient)
                .build();
    }

    @Provides
    @Singleton
    PlaceRequests providePlaceRequests(Retrofit retrofit) {
        return retrofit.create(PlaceRequests.class);
    }
}

CityModule.java

@Singleton
@Component(
        dependencies = {ApplicationModule.class},
        modules = {NetModule.class}
)
public interface NetComponent {
    Application application();
    PlaceRequests getPlaceRequests();
}

CityComponent.java

@Module
public class CityModule {

    private CityMvp.View view;

    public CityModule(CityMvp.View view) {
        this.view = view;
    }

    @Provides
    public CityMvp.View provideView() {
        return view;
    }

    @Provides
    public CityMvp.Interactor provideInteractor(PlaceRequests placeRequests) {
        return new CityInteractor(placeRequests);
    }

    @Provides
    public CityPresenter providePresenter(CityMvp.View cityView, CityMvp.Interactor interactor) {
        return new CityPresenter(cityView, interactor);
    }
}

CityInteractor.java(无法注入的那个导致PlaceRequests依赖项)

@PerFragment
@Component(
        dependencies = {NetModule.class},
        modules = {CityModule.class}
)

public interface CityComponent {
    void inject(CityFragment cityFragment);
}

最后,错误跟踪:

public class CityInteractor implements CityMvp.Interactor {

    private PlaceRequests placeRequests;

    public CityInteractor(PlaceRequests placeRequests) {
        this.placeRequests = placeRequests;
    }

    @Override
    public void getPlaceDetails(String placeId, String key, Subscriber<PlaceDetails> subscriber) {
        Observable<PlaceDetails> observable = placeRequests.getPlaceDetails(placeId, key);
        observable.observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(subscriber);
    }
}

据我所知,我在NetComponent.java中暴露了PlaceRequests对象,而NetModule.java是CityComponent的依赖项。为什么我无法从CityComponent获得PlaceRequests依赖?我错过了什么?谢谢!

1 个答案:

答案 0 :(得分:6)

通过@Component(dependencies={...})指定的

Component dependencies不应该是模块,因为您拥有它们。它们应该是通过称为提供方法的零参数方法使依赖性可用的类型(通常是组件)。

将依赖项切换到组件而不是模块。

如果有帮助,您可能希望改变对组件,模块和组件依赖关系的看法。您可以将Dagger视为创建对象图,其中模块定义图的输入或绑定组件定义图的输出或使用者。这使得组件依赖性成为类型或组件包含或从另一个外部源导入,其中可能包含不同的Dagger创建的组件,但可能不包含模块 - 而不是依赖于您依赖于使用该模块的组件的模块。