注入一个 Class与杜松子酒
有没有办法在 gin 中注入类类型 Class
?我似乎无法让它工作,例如:
class GenericFoo<T> {
private final Class<T> klass;
@Inject
public GenericFoo(Class<T> klass) {
this.klass = klass;
}
}
class Bar { }
在某处注入一个实例:
..
@Inject
GenericFoo<Bar> instance;
..
以及一个包含以下内容的 GinModule:
bind(new TypeLiteral<Class<Bar>>() {}).to(Bar.class);
谢谢
Is there a way to inject a class type Class<T>
in gin? I can't seem to get it working, for example:
class GenericFoo<T> {
private final Class<T> klass;
@Inject
public GenericFoo(Class<T> klass) {
this.klass = klass;
}
}
class Bar { }
with an instance injected somewhere:
..
@Inject
GenericFoo<Bar> instance;
..
and a GinModule containing something along the lines of:
bind(new TypeLiteral<Class<Bar>>() {}).to(Bar.class);
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是不可能的。客户端禁止反射,因此用于依赖注入的 GIN 使用延迟绑定。这意味着在编译过程中,GWT 生成的目标实现在您的情况下是未知的。
It's not possible. Reflection is forbidden on the client side, so GIN for dependency injection is using deffered binding. It means that during the compilation, GWT generates target implementations which are unknow in your case.
如果这是常规的 Guice(而不是 Gin),您可以这样做:
但是 Gin 不支持
.toInstance(...)
绑定。相反,您应该能够使用Provider
或@Provides
方法,例如:If this were regular Guice (as opposed to Gin), you could do:
But Gin doesn't support
.toInstance(...)
bindings. Instead, you should be able to use aProvider
or an@Provides
method, like: