Guice:如何将同一类的带注释的单例绑定到不同的实例?
我想为每个注释绑定一个唯一的单例实例,如下所示,例如 Client1.a != Client1.b
和 Client1.a == Client2.a
。
class X {}
@Singleton class OneOfEachAnnotation {
@Inject OneOfEachAnnotation(X x) { }
}
class Client1 {
@Inject Client(@A OneOfEachAnnotation a, @B OneOfEachAnnotation b) {}
}
class Client2 {
@Inject Client(@A OneOfEachAnnotation a, @B OneOfEachAnnotation b) {}
}
这个答案似乎声称我可以像这样完成绑定,但是当我这样做时,Client1.a == Client1.b
。
bind(OneOfEachAnnotation.class).annotatedWith(A.class).to(OneOfEachAnnotation.class).in(Singleton.class);
bind(OneOfEachAnnotation.class).annotatedWith(B.class).to(OneOfEachAnnotation.class).in(Singleton.class);
I would like to bind a unique singleton instance for each annotation as shown below, such that Client1.a != Client1.b
and Client1.a == Client2.a
.
class X {}
@Singleton class OneOfEachAnnotation {
@Inject OneOfEachAnnotation(X x) { }
}
class Client1 {
@Inject Client(@A OneOfEachAnnotation a, @B OneOfEachAnnotation b) {}
}
class Client2 {
@Inject Client(@A OneOfEachAnnotation a, @B OneOfEachAnnotation b) {}
}
This answer seems to claim that I can accomplish the binding like this, but when I do so, Client1.a == Client1.b
.
bind(OneOfEachAnnotation.class).annotatedWith(A.class).to(OneOfEachAnnotation.class).in(Singleton.class);
bind(OneOfEachAnnotation.class).annotatedWith(B.class).to(OneOfEachAnnotation.class).in(Singleton.class);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢 Google Groups Guice 论坛上的 Sam Berlin。这不起作用的原因是 OneOfEachAnnotation 已经用 @Singleton 进行了注释。删除 @Singleton 注释可以解决该问题。
Sam 解释了原因:
这些语句
:“创建从
@A OneOfEachAnnotation
到OneOfEachAnnotation
的绑定,并使@A OneOfEachAnnotation
成为单例。 ”与@B 相同。忽略
OneOfEachAnnotation
上的@Singleton
一秒钟(假设它不存在),那么这将具有以下行为:用户注入
OneOfEachAnnotation< /code> (未注释)多次:每次都会创建一个新的,因为该绑定没有作用域。
用户注入
@A OneOfEachAnnotation
,第一次链接到未注释的OneOfEach
,看到它需要构造,并构造它。第二次绑定已配置,因此 Scopes.SINGLETON 不会再次转到链接的绑定。(
@B
与@A
相同。)当
OneOfEachAnnotation
上有@Singleton
时,Guice 会认为无范围值也是一个单例。因此,当 @A 链接到未注释的内容时,它将创建第一个实例。当 @B 链接未注释的版本时,Guice 注意到未注释的版本(也是一个单例)已经被构造,并返回该实例。Thanks to Sam Berlin at Google Groups Guice forum. The reason this doesn't work is because the OneOfEachAnnotation is already annotated with @Singleton. Removing the @Singleton annotation fixes the problem.
Sam explained why:
The statements:
are saying, "Create a binding from
@A OneOfEachAnnotation
toOneOfEachAnnotation
and make the@A OneOfEachAnnotation
a singleton." Same thing with@B
.Ignoring the
@Singleton
onOneOfEachAnnotation
for a second (assuming it didn't exist), then this would have the behavior of:User injects
OneOfEachAnnotation
(unannotated) multiple times: a new one is created each time, because that binding is unscoped.User injects
@A OneOfEachAnnotation
, the first time it links down to the unannotatedOneOfEach
, sees it needs to be constructed, and constructs it. The second time the binding is already provisioned, soScopes.SINGLETON
doesn't go to the linked binding again.(Same thing with
@B
as@A
.)When
OneOfEachAnnotation
has@Singleton
on it, then Guice thinks that the unscoped value is also a singleton. So when@A
links to the unannotated, it will create the first instance. When@B
links down the unannotated, Guice notices that the unannotated version, also a Singleton, has already been constructed, and returns that instance.