GWT/Gin 在构造函数中使用 @Inject 注释创建类
假设我有一个类,
public class Foo{
@Inject
public Foo(MessageBus messageBus, SomeServiceAsync service){
...
}
...
考虑到要注入构造函数参数,我对如何构造这样的类有些疑问。或者我必须以某种方式通过 Gin 获取 Foo 类的实例(无论如何注入都会发生这种情况)?
提前致谢
Let's say I have a class
public class Foo{
@Inject
public Foo(MessageBus messageBus, SomeServiceAsync service){
...
}
...
I have some doubt on how I would construct such a class, given that the constructor parameters are to be injected. Or must I somehow also get an instance of the Foo class through Gin (is that the case anyway for injection to take place)?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的假设是正确的。如果您希望注入它们的构造函数,则必须从 Gin 获取所有 Foo。要从 Gin 获取
Foo
,您需要将其注入到其他东西中,或者使用Ginjector
。通常,您只会从 Ginjector 中获取一个类的实例(或少量类的实例),并依赖 Gin 来注入它们的所有依赖项,以及它们的依赖项的依赖项,等等在。Gin 教程是一个很好的起点。
Your assumption is correct. You must get all
Foo
s from Gin if you want them to have their constuctors injected. To get aFoo
from Gin, you need to either have it injected into something else, or use aGinjector
. Usually you'll get only one class' instances (or a small number of classes' instances) from aGinjector
, and rely on Gin to inject all of their dependencies, and their dependencies' dependencies, and so on.The Gin Tutorial is a great place to start.