簡體   English   中英

匕首2 - 為什么這是一個依賴循環?

[英]Dagger 2 - Why is this a dependency cycle?

我正在嘗試將應用程序的Context注入其他兩個對象,即AuthManagerApiClient

它們都取決於所述上下文, ApiClient依賴於AuthManager 為什么這是一個依賴循環,如果Context沒有引用其他2? 這可以解決嗎?

編輯:這是一些代碼

@Module
public class AppModule {

    private final Application application;

    public AppModule(Application application) {
        this.application = application;
    }

    @Provides @Singleton
    Context provideApplicationContext() {
         return this.application;
    }
}


@Module
public class NetworkModule {

    @Provides @Singleton
    public AuthManager providesAuthManager(AuthManager manager) {
        return manager;
    }

    @Provides @Singleton
    public ApiClient providesApiClient(ApiClientFactory factory) {
        return factory.create();
    }
}

@Singleton
@Component(modules = {AppModule.class, NetworkModule.class})
public interface ApplicationComponent {
    void inject(BaseActivity activity);

    // Exported for child-components
    Context context();
    ApiClient apiClient();
    AuthManager authManager();
}
@Provides @Singleton
public AuthManager providesAuthManager(AuthManager manager) {
    return manager;
}

providesAuthManager AuthManager方法取決於AuthManager

這是你的周期:)

取出providesAuthManager方法並添加@Inject在AuthManager構造函數。

暫無
暫無

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

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