在两个 Ginjector 实例中使用 @Singleton
我在 GWT 项目集中(“主项目”、“小部件项目”、“服务适配器项目”)存在循环依赖问题。主要参考widgets和service adapter。小部件参考服务适配器。问题来了。我想在几乎所有地方使用EventBus
,注入。我应该把 Ginjector 接口放在哪里?
它必须可以在每个项目中使用,并且也必须引用每个项目中的类,以便能够注入来自不同项目的类。这显然是无法编译的。
我想为小部件使用一个单独的 Ginjector,并为 EventBus
使用一个单独的 Ginjector。如果两个单独的 Ginjector 使用两个单独的 GinModules
,两者都包含 @Singleton
EventBus
绑定,则两个 getter 将返回相同的 EventBus
是否有实例?
注意:这是杜松子酒问题,而不是 Guice 问题。
I have circural dependency problems in a GWT project set ("Main project", "Widgets project", "Service adapter project"). Main references widgets and service adapters. Widgets reference service adapters. And here comes the problem. I would like to use an EventBus
practically everywhere, injected. Where should I put my Ginjector interface?
It has to be usable from every project and has to reference classes from every project too, to be able to inject classes from different projects. This is obviously uncompilable.
I thought of using a separate Ginjector for widgets and one for only the EventBus
. If two separate Ginjectors use two separate GinModules
both containing @Singleton
EventBus
bindings, the two getters will return the same EventBus
instances or not?
Note: It's a Gin, not a Guice question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能想到的最简单的方法是在单独的注入器中创建
EventBus
作为单例(或在某些类中将其设置为静态最终字段),然后在其他注入器中使用@Provides
获取对该特定EventBus
实例的访问权限。The most simple way I can think of is to create
EventBus
as singleton in separate injector (or make it static final field in some class), then use@Provides
in other injectors to gain access to that specificEventBus
instance.我为每个逻辑部分创建一个模块(通常每个项目一个或多个),然后从入口点提供一个单一的 ginjector,引用它需要的所有模块。所有(几乎)超过入口点的东西都会由 Gin 创建,因此它将能够注入字段。
GWT.create
生成的每个新 Ginjector 都将拥有自己的一组单例,因此仅创建一个根 ginjector 非常重要。可以将该注入器注入到代码库的其他部分,但如果不这样做,通常会产生更具可读性的代码。如果您需要将 EventBus 的实例或其他任何内容传递给不是由 Gin 创建的对象,有几个选项。第一个是构建您自己的 Provider 实例,并将其绑定到模块中(或在模块中创建用
@Provides
注释的方法)。第二种是在 ginjector 中创建采用单个参数并返回 void 的方法 - Gin 将能够注入该声明类型的所有字段和设置器。在下面的示例中,只有MyWidget
中用@Inject
注释的字段和 setter 及其超类将被注入 - 它也不知道要查找子类。I make one module for each logical section (one or more per project often), and then a single ginjector, made available from the entrypoint, referencing all of the modules that it needs. Everything (almost) past the entrypoint is then created by Gin, so it will be able to have fields injected.
Each new Ginjector that is
GWT.create
'd will have its own set of singletons, so it is important to only create a single root ginjector. It is possible to inject that injector to other parts of the codebase, but generally makes for more readable code if you dont do that.If you need to pass instances of
EventBus
or anything else to objects not created by Gin, there are a few options. The first is to build your own Provider instance, and bind that in the module (or create methods in the module annotated with@Provides
). The second is to create methods in the ginjector that take a single parameter and return void - Gin will be able to inject all of the fields and setters for that declared type. In the example below, only the fields and setters annotated with@Inject
inMyWidget
and its superclasses will be injected - it won't know to look for subclasses as well.