GIN 是否支持子注入器之类的东西?
我有一个包含子应用程序的应用程序。我想隔离 GIN 注入,以便每个子应用程序都可以拥有相同核心共享类的单独实例。我还希望注入器将一些核心模块中的类提供给所有子应用程序,以便可以共享单例实例。例如,
GIN Modules:
Core - shared
MetadataCache - one per sub-application
UserProvider - one per sub-application
在 Guice 中,我可以使用 createChildInjector 来完成此操作,但我在 GIN 中看不到明显的等效项。
我可以在 GIN 中实现类似的功能吗?
I have one application that contains sub-applications. I would like to segregate the GIN injection so that each sub-application can have separate instances of the same core shared classes. I also want the injector to supply classes from some core modules to all sub-applications, so that singleton instances can be shared. e.g.
GIN Modules:
Core - shared
MetadataCache - one per sub-application
UserProvider - one per sub-application
In Guice I can do this using createChildInjector
, but I can't see an obvious equivalent in GIN.
Can I achieve something similar in GIN?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
感谢 @Abderrazakk 提供的链接,我解决了这个问题,但由于该链接不是很容易找到说明,我想我也应该在这里添加一个示例解决方案:
私有 GIN 模块允许您进行单级分层注入,其中在私有模块内注册的类型仅对该模块内创建的其他实例可见。在任何非私有模块中注册的类型仍然可供所有人使用。
示例
让我们有一些要注入(和注入)的示例类型:
创建两个像这样的私有模块(使用 ThingOwner2 作为第二个模块):
和一个共享模块:
现在在我们的注入器中注册这两个模块:
最后我们可以看到 ThingOwner1 和 ThingOwner2 实例在共享模块中具有相同的 SharedThing 实例,但在其私有注册中具有不同的 Thing 实例:
I solved this thanks to the link given by @Abderrazakk, but as the link isn't very forthcoming with instructions I thought I'd add a sample solution here too:
Private GIN modules allow you to have a single level of hierarchical injection, where types registered inside a private module are only visible to other instances created within that module. Types registered inside any non-private modules are still available to all.
Example
Let's have some sample types to inject (and inject into):
Create two private modules like this (using ThingOwner2 for the second one):
And a shared module:
Now register the two modules in our injector:
Finally we can look and see that both ThingOwner1 and ThingOwner2 instances have the same SharedThing instance from the shared module, but different Thing instances from their private registrations:
这是 SOF http://code.google.com/p/google- gin/wiki/PrivateModulesDesignDoc。
希望它能帮助你。
Here it is on SOF http://code.google.com/p/google-gin/wiki/PrivateModulesDesignDoc.
Hope it helps you out.