在斯派蒂,有可能 implicit inj: Injector
对于班级和 Injector
在I中定义时,在没有任何附加代码的情况下传递 Module
```
class MessageService(implicit inj: Injector)
extends Injectable {
val officialGreeting =
inject [String] (identified by "greeting.official")
def getGreetMessage(name: String) =
s"$officialGreeting, $name!"
}
//Module
class UserModule extends Module {
bind [MessageService] to new MessageService
binding identifiedBy "greeting.official" to "Welcome"
}
如何做到同样,但在一个特质?
class MessageService() {
val officialGreeting =
inject [String] (identified by "greeting.official")
def getGreetMessage(name: String) =
s"$officialGreeting, $name!"
}
trait MessageServiceReportingMixin extends MessageService with Injectable {
implicit val inj: Injector
val reporter = inject[Reporter]
override def getGreetMessage(name: String) = reporter.report(super.getGreetMessage(name))
}
//Module
class UserModule extends Module {
//I WANT TO AVOID THIS
bind [MessageService] to new MessageService() with MessageServiceReportingMixin {
val inj = injector
}
//I WANT TO DO THIS INSTEAD
bind [MessageService] to new MessageService() with MessageServiceReportingMixin
binding identifiedBy "greeting.official" to "Welcome"
}
有什么问题吗 `implicit val inj: Injector = implicitly` 为了让这个工作?另外,请评论一下将报告从初级课程中分离出来的整体风格
暂无答案!
目前还没有任何答案,快来回答吧!