【问题标题】:How to pass ControllerComponents in compile time injection如何在编译时注入中传递 ControllerComponents
【发布时间】:2018-03-19 13:36:58
【问题描述】:

我的控制器定义如下

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。我该怎么办?

【问题讨论】:

    标签: playframework-2.6


    【解决方案1】:

    BuiltInComponentsFromContextComponentController 的实例

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2017-03-19
      相关资源
      最近更新 更多