【发布时间】:2018-07-23 19:09:45
【问题描述】:
在 Scala 中使用 Guice,我正在尝试重现以下 Java 代码。
Foo接口和类声明:
public interface Foo[T] {}
public class FooImpl[T] implements Foo[T] {}
Guice绑定代码:
bind(Foo.class).to(FooImpl.class);
一个使用示例是;
@Inject
public class Bar(Foo<String> foo) {}
在 scala 中,我的第一个赌注是:
bind(classOf[Foo]).to(classOf[FooImpl])
但它在抱怨“Type Foo 接受类型参数” 我如何在 scala 中实现这一点?
谢谢
【问题讨论】:
-
bind(classOf[Foo<String>]).to(classOf[FooImpl<String>])怎么样 -
bind(classOf[Foo[_]]).to(classOf[FooImpl[_]]) -
您好 Dima,感谢您的回答。看起来它正在工作,但我现在无法确认,因为我面临另一个问题。我的班级有 2 个隐式参数,看起来它们没有使用 Guice 和注入正确提供。如果它确实有效,我会通知你,以便你用它来回答我的问题。
标签: java scala dependency-injection guice