Java 使用组件依赖时单例的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28170292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problems with singletons when using component dependencies
提问by Cativail
I'm having a problem in understanding why the following code doesn't work.
我在理解为什么以下代码不起作用时遇到问题。
I have following project structure:
我有以下项目结构:
@Component(modules = CCModule.class)
public interface CComponent {
XXX getXXX();
}
where
在哪里
@Module
public class CCModule {
@Provides @Singleton
public XXX provide XXX(){
return new XXX();
}
}
and
和
@Component(dependencies = CComponent.class, modules = AAModule.class)
public interface AComponent {
YYY getYYY();
}
where
在哪里
class YYY {
@Inject
public YYY(XXX xxx) {
...
}
}
I initialize everything as:
我将一切初始化为:
CComponent c_component = Dagger_CComponent.builder().cCModule(new CCModule()).build();
AComponent a_component = Dagger_AComponent.builder()
.cComponent(c_component)
.aAModule(new AAModule())
.build();
Once compilation takes place i get the following error:
编译发生后,我收到以下错误:
Error:(11, 1) error: com.test.CComponent (unscoped) may not reference scoped bindings: @Provides @Singleton com.test.XXX com.test.CCModule.provideXXX()
错误:(11, 1) 错误:com.test.CComponent (unscoped) 不能引用作用域绑定:@Provides @Singleton com.test.XXX com.test.CCModule.provideXXX()
What I'm aiming for is to have one component inherit bindings from other components to have the same references to an objects (singletons).
我的目标是让一个组件从其他组件继承绑定,以便对对象(单例)具有相同的引用。
采纳答案by Kirill Boyarshinov
You should put @Singleton
to CComponent
class declaration.
你应该把@Singleton
以CComponent
类声明。
@Singleton
@Component(modules = CCModule.class)
public interface CComponent {
XXX getXXX();
}
Explanation is in error message: CComponent
is unscoped, @Singleton
is a scope. Dagger 2 does not allow unscoped components to use modules with scoped bindings.
However, now you will get the following error:
解释在错误消息中:CComponent
是无范围的,@Singleton
是一个范围。Dagger 2 不允许无作用域的组件使用具有作用域绑定的模块。
但是,现在您将收到以下错误:
AComponent (unscoped) cannot depend on scoped components:
@Component(dependencies = CComponent.class, modules = AModule.class)
Unscoped components cannot have scoped dependencies. So you need to make AComponent
scoped. To do this create custom AScope
annotation.
无作用域的组件不能有作用域的依赖项。所以你需要做AComponent
scoped。为此,请创建自定义AScope
注释。
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface AScope {
}
And annotate with it AComponent
:
并用它注释AComponent
:
@AScope
@Component(dependencies = CComponent.class, modules = AModule.class)
public interface AComponent {
}
These are new requirements appeared in latest snapshot release. It was discussed in corresponding issueand may still be changed.
回答by Alexander Vasiljev
Looks like a bug in the latest Dagger-2 release: https://github.com/google/dagger/issues/107
看起来像是最新版 Dagger-2 中的一个错误:https: //github.com/google/dagger/issues/107
回答by sonal balekai
Add
添加
@Singleton
@Component(modules = {NameModule.class})
public interface NameComponent {
}
for the component because dagger2 don't allow to use unscoped components with scoped modules
对于组件,因为 dagger2 不允许使用具有作用域模块的无作用域组件