Scala 性状混合蛋白中的sphiti注射剂
在SHOTHI中,类可以有Scala 性状混合蛋白中的sphiti注射剂,scala,implicit,scaldi,Scala,Implicit,Scaldi,在SHOTHI中,类可以有隐式inj:Injector,并且在SHOTHI模块中定义时,Injector在无任何附加代码的情况下传递 class MessageService(implicit inj: Injector) extends Injectable { val officialGreeting = inject [String] (identified by "greeting.official") def getGreet
隐式inj:Injector
,并且在SHOTHI模块中定义时,Injector
在无任何附加代码的情况下传递
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=implicit
是否会出现任何问题,以使其正常工作?另外,请评论将报告作为混音从主类中分离出来的总体风格