我能够毫无问题地将服务注入(inject)到我的应用程序类中。但不知怎的,我无法注入(inject) Actor 本身。
我的 Actor :
class PollerCrow @Inject()(
@Named("pollService") pollService: PollService[List[ChannelSftp#LsEntry]]
, @Named("redisStatusService") redisStatusService: StatusService
, @Named("dynamoDBStatusService") dynamoDbStatusService: StatusService
) extends BaseCrow {
... impl and stuff ...
}
我的 Actor 的伴生对象:
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 []
我该如何解决这个问题?
编辑:
我已经绑定(bind)了我的 Actor :
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
了解您的 Actor 。这是干净的方法:
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"
}
对于那些不想麻烦的人,只需直接调用注入(inject)器即可,并且不要忘记将 Application
导入到范围:
Play.application.injector.instanceOf[YourService]
Play.application.injector.instanceOf(BindingKey(classOf[YourService]).qualifiedWith("your-name"));
关于scala - 如何使用 Play 2.4 将服务注入(inject)到 Actor 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32746041/