簡體   English   中英

Dagger 2 組件依賴

[英]Dagger 2 component dependencies

是否可以向模塊中注入一些東西?

我有 2 個基本模塊/組件:

@Component(modules = {OkHttpModule.class})
public interface OkHttpComponent extends AppComponent {

    OkHttpClient provideOkHttpClient();
}

@Module
public class OkHttpModule {

    @Provides
    OkHttpClient provideOkHttpClient() {

        return mHttpClient;
    }
}
@Component(modules = {GsonModule.class})
public interface GsonComponent extends AppComponent {

    Gson provideGson();
}

@Module
public class GsonModule {

    @Provides
    Gson provideGson() {

        return mGson;
    }
}

使用此模塊/組件,我想創建第三個模塊:

@Component(dependencies = {OkHttpComponent.class, GsonComponent.class}, 
           modules = {RetrofitModule.class})
public interface RetrofitComponent extends AppComponent {

    API provideAPI();
}

@Module
public class RetrofitModule {

    @Inject OkHttpClient mHttpClient;
    @Inject Gson         mGson;

    @Provides
    VeentsMeAPI provideVeentsMeAPI() {

        return mVeentsMeAPI;
    }
}

但是沒有注入mHttpClientmGson 是否可以將某些內容注入模塊? 如果是,如何?

我創建這樣的組件:

okHttpComponent = DaggerOkHttpComponent.builder()
        .okHttpModule(new OkHttpModule(this))
        .build();

gsonComponent = DaggerGsonComponent.builder()
        .gsonModule(new GsonModule())
        .build();

retrofitComponent = DaggerRetrofitComponent.builder()
        .retrofitModule(new RetrofitModule())
        .okHttpComponent(okHttpComponent)
        .gsonComponent(gsonComponent)
        .build();

在我看來,注入模塊並沒有什么意義。 根據 Dagger1 模塊的定義,所有模塊都被聲明為complete=false, library=true ,這意味着只要您包含不包含在您正在使用的組件中的模塊,您就不需要指定任何真正的魔法,或者使您的組件依賴項以這樣一種方式相互鏈接,即您可以注入的每個依賴項僅在一個組件中指定。

您將從構造函數參數中的其他模塊獲取依賴項。 您需要將模塊includesincludes列表中,或者您需要在相同范圍內的組件中指定模塊。

正確的做法是這樣的:

@Module
public class OkHttpModule {
    @Provides
    @ApplicationScope
    public OkHttpClient okHttpClient() {
        return new OkHttpClient();
    }
}

@Module
public class GsonModule {
    @Provides
    @ApplicationScope 
    public Gson gson() {
        return new Gson();
    }
}

@Module(includes={GsonModule.class, OkHttpModule.class}) //look closely, this is the magic
public class RetrofitModule {
    @Provides
    @ApplicationScope
    public VeentsMeAPI veentsMeAPI(Gson gson, OkHttpClient okHttpClient) { //dependencies!
         return RestAdapter.Builder()
            .setClient(new Client(okHttpClient))
            .setConverter(new GsonConverter(gson))
            .create(VeentsMeAPI.class);
    }
}

然后就是

@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface ApplicationScope {}

甚至可能

@ApplicationScope
public class Something {
    private final VeentsMeAPI veentsMeApi;

    @Inject
    public Something(VeentsMeAAPI veentsMeApi) {
        this.veentsMeApi = veentsMeApi;
    }
}

然后

@ApplicationScope
@Component(modules={RetrofitModule.class}) //contains all other modules/dependencies
public interface AppComponent {
    OkHttpClient okHttpClient();
    Gson gson();
    VeentsMeAPI veentsMeAPI();
    Something something();

    void inject(SomeActivity someActivity);
}

然后你會得到生成的東西,你必須像這樣一起構建它

AppComponent appComponent = DaggerAppComponent.create();

//use appComponent.inject(someActivity); with `void inject(Stuff someActivity);` methods defined in AppComponent

組件依賴與子組件相同,因此它們用於子作用域,而不是用於從同一作用域繼承依賴關系。 相同作用域的依賴應該在同一個組件中,只是不同的模塊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM