匕首2-为什么这是一个依赖周期? [英] Dagger 2 - Why is this a dependency cycle?

查看:59
本文介绍了匕首2-为什么这是一个依赖周期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将应用程序的 Context 注入其他2个对象中,即 AuthManager ApiClient

I'm trying to inject the application's Context into 2 other objects, an AuthManager and an ApiClient.

两者都取决于上述上下文,而 ApiClient 取决于 AuthManager 。如果 Context 没有引用其他2,为什么这是一个依赖循环?

Both of them depends on said context, and the ApiClient depends on the AuthManager. Why is this a dependency cycle, if Context doesn't have a reference to the others 2? can this be solved?

编辑:这是一些代码

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

您的提供AuthManager 方法提供 AuthManager 取决于 AuthManager

有你的周期:)

这篇关于匕首2-为什么这是一个依赖周期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆