【问题标题】:Could I get the instance of the defalt actor system in Playframwork我可以在 Play Framework 中获取默认演员系统的实例吗
【发布时间】:2018-03-09 03:33:20
【问题描述】:

默认的 playframework 应用程序有一个 AsyncController。我需要ActorSystem

class AsyncController @Inject()(cc: ControllerComponents, actorSystem: ActorSystem)(implicit exec: ExecutionContext) extends AbstractController(cc) {

我正在创建一个需要编译时依赖注入的代码。我想我必须创建我的应用程序的所有控制器的实例并将其传递给Routes

lazy val router = new Routes(httpErrorHandler, homeController,userWSRoutes, countController,asyncController, assets)

我的问题是,在创建 AsyncController 的实例时,我不知道将什么作为 ActorSystem 传递。

lazy val asyncController = new controllers.AsyncController(controllerComponents, system) //what should be system.

我尝试注入(也尝试过隐式)参数,但没有成功

class AppLoader @Inject()(system: ActorSystem) extends ApplicationLoader {
  override def load(context: ApplicationLoader.Context): Application =
    new AppComponents(system, context).application
}

class AppComponents (system: ActorSystem, context: Context) extends BuiltInComponentsFromContext(context)
  with CassandraRepositoryComponents
  with HttpFiltersComponents
  with controllers.AssetsComponents{
...
lazy val asyncController = new controllers.AsyncController(controllerComponents, system) //doesn't work.
}

我收到以下错误

! @777ipfa31 - Internal server error, for (GET) [/] ->



play.api.UnexpectedException: Unexpected exception[InstantiationException: AppLoader]
    at play.core.server.DevServerStart$$anon$1.reload(DevServerStart.scala:186)
    at play.core.server.DevServerStart$$anon$1.get(DevServerStart.scala:124)
    at play.core.server.AkkaHttpServer.handleRequest(AkkaHttpServer.scala:189)
    at play.core.server.AkkaHttpServer.$anonfun$createServerBinding$1(AkkaHttpServer.scala:106)
    at akka.stream.impl.fusing.MapAsync$$anon$25.onPush(Ops.scala:1194)
    at akka.stream.impl.fusing.GraphInterpreter.processPush(GraphInterpreter.scala:519)
    at akka.stream.impl.fusing.GraphInterpreter.processEvent(GraphInterpreter.scala:482)
    at akka.stream.impl.fusing.GraphInterpreter.execute(GraphInterpreter.scala:378)
    at akka.stream.impl.fusing.GraphInterpreterShell.runBatch(ActorGraphInterpreter.scala:585)
    at akka.stream.impl.fusing.GraphInterpreterShell$AsyncInput.execute(ActorGraphInterpreter.scala:469)
Caused by: java.lang.InstantiationException: AppLoader
    at java.lang.Class.newInstance(Class.java:427)
    at play.api.ApplicationLoader$.apply(ApplicationLoader.scala:79)
    at play.core.server.DevServerStart$$anon$1.$anonfun$reload$3(DevServerStart.scala:173)
    at play.utils.Threads$.withContextClassLoader(Threads.scala:21)
    at play.core.server.DevServerStart$$anon$1.reload(DevServerStart.scala:171)
    at play.core.server.DevServerStart$$anon$1.get(DevServerStart.scala:124)
    at play.core.server.AkkaHttpServer.handleRequest(AkkaHttpServer.scala:189)
    at play.core.server.AkkaHttpServer.$anonfun$createServerBinding$1(AkkaHttpServer.scala:106)
    at akka.stream.impl.fusing.MapAsync$$anon$25.onPush(Ops.scala:1194)
    at akka.stream.impl.fusing.GraphInterpreter.processPush(GraphInterpreter.scala:519)
Caused by: java.lang.NoSuchMethodException: AppLoader.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.newInstance(Class.java:412)
    at play.api.ApplicationLoader$.apply(ApplicationLoader.scala:79)
    at play.core.server.DevServerStart$$anon$1.$anonfun$reload$3(DevServerStart.scala:173)
    at play.utils.Threads$.withContextClassLoader(Threads.scala:21)
    at play.core.server.DevServerStart$$anon$1.reload(DevServerStart.scala:171)
    at play.core.server.DevServerStart$$anon$1.get(DevServerStart.scala:124)
    at play.core.server.AkkaHttpServer.handleRequest(AkkaHttpServer.scala:189)
    at play.core.server.AkkaHttpServer.$anonfun$createServerBinding$1(AkkaHttpServer.scala:106)
    at akka.stream.impl.fusing.MapAsync$$anon$25.onPush(Ops.scala:1194)
[error] application - 

【问题讨论】:

    标签: playframework-2.6


    【解决方案1】:

    BuiltInComponentsFromContext 在其变量 actorSystem 中引用了 Actor System。

    lazy val asyncController = new controllers.AsyncController(controllerComponents, actorSystem)

    https://www.playframework.com/documentation/2.6.x/api/scala/index.html#play.api.BuiltInComponentsFromContext

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-24
      • 2020-08-10
      • 2011-10-31
      • 2011-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多