是否有可能创建一个在构造函数中具有隐含的akka actor? 具有:
class Actor(parameter: Long)(implicit service:Service)
和
class Service
我可以使用上下文来创建这样的演员吗?
implicit val service:Service = new Service()
val someLong = 3
context.actorOf(FromConfig.props(Props(classOf[Actor], someLong)), "actor")
提及服务无法传递给构造函数,因为可以接收多个不同的actor类,这些类使用范围内的不同含义。
答案 0 :(得分:13)
在Actor的Companion对象中定义Props,适合注入依赖项。
class SomeActor(parameter: Long)(implicit service:Service) extends Actor {
def receive = {
case message => // Do your stuff
}
}
object SomeActor {
def props(parameter: Long)(implicit service:Service) = Props(new SomeActor(parameter))
}
implicit val service:Service = new Service()
val someLong = 3
val ref = context.actorOf(SomeActor.props(someLong)), "actor")
您可以在此处阅读有关依赖注入的更多信息: http://doc.akka.io/docs/akka/snapshot/scala/actors.html#Dependency_Injection http://letitcrash.com/post/55958814293/akka-dependency-injection