Dagger 2 Singleton组件取决于Singleton [英] Dagger 2 Singleton Component Depend On Singleton

查看:66
本文介绍了Dagger 2 Singleton组件取决于Singleton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里遇到了一个奇怪的问题,我不确定为什么不允许我在做什么。我有以下模块:

  @Module 
public final class AppModule {
private Context mContext ;

@Provides
@Singleton
@AppContext
public Context ProvideContext(){return mContext; }
}

@Module
公共最终课程NetModule {
@Provides
@Singleton
公共OkHttpClient ProvideOkHttp(){
返回新的OkHttpClient.Builder()。build();
}
}

由于各种原因,我不想拥有这两个模块在同一个组件中(主要是由于我的项目结构)。因此,我尝试创建以下组件:

  @Singleton 
@Component(modules = AppModule.class)
公共接口AppComponent {
@AppContext上下文appContext();
}

@Singleton
@Component(依赖关系= AppComponent.class,模块= NetModule.class)
公共接口NetComponent {
Retrofit Retrofit() ;
}

但是当我尝试编译它时,出现以下错误消息: / p>

错误:(12,1)错误:此@Singleton组件不能依赖于范围内的组件:
@Singleton com.myapp.service .dagger.AppComponent



我理解为什么依赖不同范围会很糟糕并且被禁止。但是为什么不允许Singleton依赖Singleton?感觉应该可行,因为我所做的只是声明同级组件。我缺少什么?

解决方案

由于您的NetComponent组件取决于您的AppComponent组件,因此它们不能具有相同的作用域。范围用于注释生命周期,并且由于NetComponent依赖于AppComponent,因此它们没有相同的生命周期。 AppComponent的寿命可能比NetComponent长,因为它是NetComponent实际构建过程的一部分。没有AppComponent,NetComponent就不会存在,反之亦然。



您可以添加自己的自定义范围,并将其应用于NetComponent和NetModule,这将对其进行修复。


I've got a strange problem here, and I'm not quite sure why what I'm doing isn't allowed. I've got the following modules:

@Module
public final class AppModule {
  private Context mContext;

  @Provides
  @Singleton
  @AppContext
  public Context provideContext() { return mContext; }
}

@Module
public final class NetModule {
  @Provides
  @Singleton
  public OkHttpClient provideOkHttp() {
    return new OkHttpClient.Builder().build();
  }
}

For various reasons, I don't want to have these two modules in the same component (basically due to my project structure). So I tried to create the following components:

@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {
  @AppContext Context appContext();
}

@Singleton
@Component(dependencies = AppComponent.class, modules = NetModule.class)
public interface NetComponent {
  Retrofit retrofit();
}

But when I try to compile this, I get the following error message:

Error:(12, 1) error: This @Singleton component cannot depend on scoped components: @Singleton com.myapp.service.dagger.AppComponent

I understand why depending on different scopes would be bad and disallowed. But why is Singleton depends-on Singleton not allowed? This feels like it should work, since all I'm doing is declaring sibling components. What am I missing?

解决方案

Because your NetComponent component depends on your AppComponent component, they cannot have the same scope. Scopes are used to annotate lifecycles, and because NetComponent depends on AppComponent, they don't have the same lifecycle. AppComponent could potentially live longer than NetComponent, because it's a part of the actual build process of NetComponent. NetComponent couldn't exist without AppComponent, but not the other way around.

You could add your own custom scope and apply that to your NetComponent and NetModule, that would fix it.

这篇关于Dagger 2 Singleton组件取决于Singleton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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