如何在编译时注入中传递ControllerComponents

时间:2018-03-05 20:38:16

标签: playframework-2.6

我的控制器定义如下

class UserController @Inject()(userRepo: Repository[UUID, User],cc:
    ControllerComponents)(implicit exec: ExecutionContext) extends
    AbstractController(cc)

我想将Repository[UUID, User]注入此组件。我想我必须创建一个应用程序加载器并扩展BuiltInComponentsFromContext并定义我的路由。

class AppComponents(context: Context) extends (context) with CassandraRepositoryComponents {

  lazy val applicationController = new controllers.UserController(userRepository)
  lazy val assets = new controllers.Assets(httpErrorHandler)

  override def router: Router = new Routes(
    httpErrorHandler,
    applicationController,
    assets
  )
}

我的代码未编译,因为我必须在ComponentController中创建UserController时传递lazy val applicationController = new controllers.UserController(userRepository)。但我不知道从哪里得到这个ComponentController。我该怎么办?

1 个答案:

答案 0 :(得分:0)

BuiltInComponentsFromContext的实例为ComponentController

class AppComponents (context: Context) extends BuiltInComponentsFromContext(context)
  with CassandraRepositoryComponents
  with HttpFiltersComponents
  with controllers.AssetsComponents
  with CSRFComponents{

  //TODOM - check if userRepository will be a singleton. Don't want new connection for each request.

  lazy val userRepositoryController = new controllers.UserController(userRepository, controllerComponents) //controllerComponents is defined in BuiltInComponentsFromContext

...
}