PlayFramework 2.4,Actor注入问题:没有启动应用程序

时间:2016-01-13 19:49:34

标签: scala playframework dependency-injection actor

已经问过similar question,但我觉得它没有用。我正在MyRepository注入MyActor。在启动时,我得到以下异常:

[error] - akka.actor.OneForOneStrategy - Unable to provision, see the following errors:

1) Error injecting constructor, java.lang.RuntimeException: There is no started application
  at infrastructure.repository.MyRepository.<init>(MyRepository.scala:13)
  at infrastructure.repository.MyRepository.class(MyRepository.scala:13)
  while locating infrastructure.repository.MyRepository
    for parameter 0 at service.command.MyActor.<init>(MyActor.scala:38)
  at service.command.MyActor.class(MyActor.scala:38)
  while locating service.command.MyActor

1 error
akka.actor.ActorInitializationException: exception during creation
        at akka.actor.ActorInitializationException$.apply(Actor.scala:172) ~[akka-actor_2.11-2.4.0.jar:na]
        at akka.actor.ActorCell.create(ActorCell.scala:605) ~[akka-actor_2.11-2.4.0.jar:na]
        at akka.actor.ActorCell.invokeAll$1(ActorCell.scala:460) ~[akka-actor_2.11-2.4.0.jar:na]
        at akka.actor.ActorCell.systemInvoke(ActorCell.scala:482) ~[akka-actor_2.11-2.4.0.jar:na]
        at akka.dispatch.Mailbox.processAllSystemMessages(Mailbox.scala:282) [akka-actor_2.11-2.4.0.jar:na]
        at akka.dispatch.Mailbox.run(Mailbox.scala:223) [akka-actor_2.11-2.4.0.jar:na]
        at akka.dispatch.Mailbox.exec(Mailbox.scala:234) [akka-actor_2.11-2.4.0.jar:na]
        at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [scala-library-2.11.7.jar:na]
        at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [scala-library-2.11.7.jar:na]
        at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [scala-library-2.11.7.jar:na]
        at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [scala-library-2.11.7.jar:na]
Caused by: com.google.inject.ProvisionException: Unable to provision, see the following errors:
.... [same thing]

我按照说明操作,我有一个像

这样的模块
class InjectionModule extends AbstractModule with AkkaGuiceSupport {
  def configure = {
    bindActor[MyActor]("my-actor")
  }
}

这是MyRepository的代码:

@Singleton
class MyRepository @Inject()(val reactiveMongoApi: ReactiveMongoApi) extends Repository {

  // init db connection
  override val collection = getCollection(reactiveMongoApi, "card")

  def getById(id: CardId) = get(Json.obj(Entity.JSON_KEY_ID -> id.toString))

  // .. other similar methods
}


abstract class Repository {
  val collection: JSONCollection

  val dbName = current.configuration.getString("mongodb.database.name").getOrElse("")

  protected def getCollection(reactiveMongoApi: ReactiveMongoApi, name: String) =
    reactiveMongoApi.connection.db(dbName, FailoverStrategy(initialDelay = 1.second, retries = 20)).collection[JSONCollection](name)

  protected def get(query: JsObject): Future[Either[String, Option[A]]] = {
    collection.find(query).one[A].map {
      case Some(a) => Right(Some(a))
      case None => Right(None)
    }.recover { case t => Left(t.getMessage) }
  }

  // ... similar methods
}

这就是开场白&#39;对于MyActor:

@Singleton
class MyActor @Inject()(cardViewRepository: CardViewRepository) extends Actor {
  //.. actor methods
}

有趣的是,它曾经工作过,突然之间它就抛出了这个例外。也许是因为我从2.4.3升级到2.4.6?

1 个答案:

答案 0 :(得分:1)

评论中的@Salem是正确的:“可能问题是如上所述的当前问题。尝试在MyRepository类中注入Configuration并使用in而不是current.configuration。如果你真的需要应用程序对象,我认为你可以注入它......“ 我需要在MyRepository中注入Configuration。