这将是一个关于如何让绑定工作的问题,但是在清理我的示例以准备发布后,我已经设法使其工作 - 问题是我不知道为什么这个工程:用Scala Guice绑定一个对象到一个对象
import org.specs2.mutable._
import com.google.inject.{ Inject, Module, Binder, Guice }
import net.codingwell.scalaguice.ScalaModule
object InjectorSpec extends Specification {
val injector = Guice.createInjector(new ScalaModule() {
def configure() {
bind[Message].toInstance(MessageImpl)
bind[MessageService.type].toInstance(MessageService) // This line makes it work?
}
})
trait Message {
val body: String
}
object MessageImpl extends Message {
val body: String = "Hello!"
}
object MessageService {
@Inject
val message: Message = null
def get = message.body
}
"MessageService" should {
"Inject Message Implementation" in {
MessageService.get mustEqual "Hello!"
}
}
}
最初我只是绑定类型被注入(注入MessageImpl消息)。一路上,我拿起了服务的第二个绑定,没有注入(所以我不明白绑定是必需的)。任何人都可以解释这里发生了什么,如果这是继续进行的正确方法?
https://pbs.twimg.com/media/ A5HdRB-CIAAM3Ri.png:大* scnr * –
@StefanKunze确切! :) – matthewrk