我可以将服务注入到我的Application类中而没有任何问题。但不知怎的,我无法注入演员本身。
我的演员:
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:
课程的其他详细信息:
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)
}
答案 0 :(得分:4)
您可以让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"
}
对于那些不想麻烦的人,只需直接致电注射器,不要忘记将Application
导入范围:
Play.application.injector.instanceOf[YourService]
Play.application.injector.instanceOf(BindingKey(classOf[YourService]).qualifiedWith("your-name"));