简体   繁体   English

匕首2 - 为什么这是一个依赖循环?

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

I'm trying to inject the application's Context into 2 other objects, an AuthManager and an ApiClient . 我正在尝试将应用程序的Context注入其他两个对象,即AuthManagerApiClient

Both of them depends on said context, and the ApiClient depends on the AuthManager . 它们都取决于所述上下文, ApiClient依赖于AuthManager Why is this a dependency cycle, if Context doesn't have a reference to the others 2? 为什么这是一个依赖循环,如果Context没有引用其他2? can this be solved? 这可以解决吗?

EDIT: here is some code 编辑:这是一些代码

@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;
}

Your providesAuthManager method which provides an AuthManager depends on an AuthManager . providesAuthManager AuthManager方法取决于AuthManager

There's your cycle :) 这是你的周期:)

取出providesAuthManager方法并添加@Inject在AuthManager构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM