Guice:如何将同一类的带注释的单例绑定到不同的实例?

发布于 2024-11-30 13:10:38 字数 849 浏览 0 评论 0原文

我想为每个注释绑定一个唯一的单例实例,如下所示,例如 Client1.a != Client1.bClient1.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山人契 2024-12-07 13:10:39

感谢 Google Groups Guice 论坛上的 Sam Berlin。这不起作用的原因是 OneOfEachAnnotation 已经用 @Singleton 进行了注释。删除 @Singleton 注释可以解决该问题。

Sam 解释了原因:

这些语句

bind(OneOfEachAnnotation.class).annotatedWith(A.class).
 to(OneOfEachAnnotation.class).in(Singleton.class);
bind(OneOfEachAnnotation.class).annotatedWith(B.class).
 to(OneOfEachAnnotation.class).in(Singleton.class);

:“创建从 @A OneOfEachAnnotationOneOfEachAnnotation 的绑定,并使 @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:

bind(OneOfEachAnnotation.class).annotatedWith(A.class).
 to(OneOfEachAnnotation.class).in(Singleton.class);
bind(OneOfEachAnnotation.class).annotatedWith(B.class).
 to(OneOfEachAnnotation.class).in(Singleton.class);

are saying, "Create a binding from @A OneOfEachAnnotation to OneOfEachAnnotation and make the @A OneOfEachAnnotation a singleton." Same thing with @B.

Ignoring the @Singleton on OneOfEachAnnotation 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 unannotated OneOfEach, sees it needs to be constructed, and constructs it. The second time the binding is already provisioned, so Scopes.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文