我的代碼:爲什麼在嘗試將服務注入到actor中時收到此IllegalArgumentException異常?
Global.scala - 我只是在啓動時設置它,它會向演員發送一條快速消息。這裏拋出異常;我證實注入的服務已被加載。
object Global extends GlobalSettings {
override def onStart(app: Application): Unit = {
val system = app.actorSystem
system.actorOf(TempActor.props, TempActor.name) ! "hi hi"
}
}
TempActor.scala
package actors
class TempActor @Inject() (
@Named(TestServiceModuleNames.RedisService) redisService: StatusService
, @Named(TestServiceModuleNames.DynamoDbService) dynamoDbService: StatusService
) extends Actor with ActorLogging {
override def receive: Receive = {
case msg: Any =>
log.info(s"the msg => $msg")
context.system.shutdown()
}
}
object TempActor extends NamedActor {
override def name: String = this.getClass.getSimpleName
override def props: Props = Props[TempActor]
}
TestServiceModule.scala - 一個Guice的模塊中的角色需要,我已經確信,使模塊中的application.conf服務加載
package modules
class TestServiceModule extends AbstractModule with AkkaGuiceSupport {
val configs = ConfigFactory.load()
override def configure(): Unit = {
bind(classOf[StatusService]).annotatedWith(Names.named(TestServiceModuleNames.RedisService)).toInstance(new RedisStatusServiceImpl(new RedisConfig(configs.getString("redis.host"), configs.getInt("redis.port"))))
bind(classOf[StatusService]).annotatedWith(Names.named(TestServiceModuleNames.DynamoDbService)).toInstance(new DynamoDBStatusServiceImpl(Region.US_EAST_1, configs.getString("dynamo.db.endpoint"), configs.getString("dynamo.db.table.name.status")))
}
}
object TestServiceModuleNames {
final val RedisService = "RedisStatusService"
final val DynamoDbService = "DynamoDbStatusService"
}
application.conf
redis.host="localhost"
redis.port=4242
dynamo.db.endpoint="http://localhost:8000"
dynamo.db.table.name.status="status"
play.modules {
enabled += "modules.TestServiceModule"
}
play.akka.actor-system="warden"
akka {
loggers = ["akka.event.slf4j.Slf4jLogger"]
loglevel = "DEBUG"
}
我的目標是讓演員處理後端處理的遊戲應用。每個actor都會對不同的服務有一定的依賴關係,我試圖用google guice注入這些服務。
所有當我啓動應用程序,我得到的是異常跟蹤:
java.lang.IllegalArgumentException: no matching constructor found on class actors.TempActor for arguments []
我不知道如何準確地解決這個問題...
我使用播放2.4。
請把代碼的相關部分在線(即不是鏈接到github),所以問題是自包含http://meta.stackoverflow.com/questions/253915/what-to-do-with-鏈接到github-gist-in-questions – mattinbits
@mattinbits oops,認爲它會更乾淨。將更新 – iCodeLikeImDrunk