如何在编译时注入中传递控制器组件



我的控制器定义如下

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
  )
}

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

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
...
}

最新更新