如何使用 Play 2.4 将服务注入 actor?

How to inject services into actors using the Play 2.4?

我可以毫无问题地将服务注入我的应用程序 class。但是不知怎么的,我无法给演员本身注入。

我的演员:

class PollerCrow @Inject()(
     @Named("pollService") pollService: PollService[List[ChannelSftp#LsEntry]]
     , @Named("redisStatusService") redisStatusService: StatusService
     , @Named("dynamoDBStatusService") dynamoDbStatusService: StatusService
) extends BaseCrow {
... impl and stuff ...
}

我演员的伴生对象:

object PollerCrow extends NamedActor {
  override def name: String = this.getClass.getSimpleName

  val filesToProcess = ConfigFactory.load().getString("poller.crow.files.to.process")    
  def props = Props[PollerCrow]
}

当我 运行 它时,我得到以下信息:

IllegalArgumentException: no matching constructor found on class watcher.crows.PollerCrow for arguments []

我该如何解决这个问题?

编辑:

我已经绑定了我的演员:

class ActorModule extends AbstractModule with AkkaGuiceSupport {

  override def configure() {
    bindPollerActors()
  }

  private def PollActors() = {
    bindActor[PollerCrow](PollerCrow.name)
  }
}

编辑 2:

class 的其他详细信息:

abstract class BaseCrow extends Crow with Actor with ActorLogging

class PollerCrow @Inject()(
            @Named(ServiceNames.PollService) pollService: PollService[List[ChannelSftp#LsEntry]]
          , @Named(ServiceNames.RedisStatusService) redisStatusService: StatusService
          , @Named(ServiceNames.DynamoDbStatusService) dynamoDbStatusService: StatusService
) extends BaseCrow {

  override def receive: Receive = {
    ...
  }
}

object PollerCrow extends NamedActor {
  override def name: String = this.getClass.getSimpleName

  def props = Props[PollerCrow]
}

trait NamedActor {
  def name: String
  final def uniqueGeneratedName: String = name + Random.nextInt(10000)
}

你可能会让 Guice 知道你是演员。这是干净的方法:

import com.google.inject.AbstractModule
import play.api.libs.concurrent.AkkaGuiceSupport


class ActorModule extends AbstractModule with AkkaGuiceSupport {
  override def configure(): Unit =  {
    bindActor[YourActor]("your-actor")
  }
}

@Singleton
class YourActor @Inject()(yourService: IYourService) extends Actor {

  override def receive: Receive = {
    case msg => unhandled(msg)
  }

}

application.conf

play.modules {
  enabled += "ActorModule"
}

不想麻烦的人,直接调用injector,别忘了导入Application到scope:

Play.application.injector.instanceOf[YourService]
Play.application.injector.instanceOf(BindingKey(classOf[YourService]).qualifiedWith("your-name"));