[英]Dagger 2 - Why is this a dependency cycle?
我正在嘗試將應用程序的Context
注入其他兩個對象,即AuthManager
和ApiClient
。
它們都取決於所述上下文, 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.