我有以下界面:
trait Subject[T] {
def fetch() :Future[Option[T]]
}
上课:
class ChildSubject@Inject()(dao:Dao) extends Subject[String]{
def fetch(): Future[Option[String]] ={
dao.find("10").map{ name => Some(name)
}
}
一个模块:
class SubjectModule extends AbstractModule with ScalaModule{
override def configure(): Unit = {
val subMulti = ScalaMultibinder.newSetBinder[Subject](binder)
subMulti.addBinding.to[ChildSubject]
}
}
我尝试注入这个Set:
@Singleton
class SomeClass @Inject()(subjects: Set[Subject]){
subjects.map{
//do somthing
}
}
我收到以下错误:
play.sbt.PlayExceptions$CompilationException: Compilation error[kinds
of the type arguments (com.test.Subject) do not conform to the expected
kinds of the type parameters (type T).
com.test.Subject's type parameters do not match type T's expected
parameters:
trait Subject has one type parameter, but type T has none]
任何想法? 谢谢!
答案 0 :(得分:0)
你需要一个new TypeLiteral<Subject<String>>()
来绑定 - 一般类型的接口,比如来自Java的Guice中的Subject<T>
。 Scala很可能在某种形式下需要相同的东西。
这样的事情可能有用:
class SubjectModule extends AbstractModule with ScalaModule{
override def configure(): Unit = {
val subMulti = ScalaMultibinder.newSetBinder[Subject[String]](binder)
subMulti.addBinding.to[ChildSubject]
}
}