我可以在Playframwork中获得defalt actor系统的实例吗?

时间:2018-03-07 05:29:53

标签: playframework-2.6

默认的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 - 

1 个答案:

答案 0 :(得分:0)

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

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

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