如何将ComponentController传递给控制器

时间:2018-03-05 09:59:00

标签: playframework-2.6

我有一个控制器。我想将DAO组件userRepo传递给它

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

我正在按如下方式挂钩编译时间DI:

class AppComponents(context: Context) extends BuiltInComponentsFromContext(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
  )
}

问题1 - 我的问题是我不知道该传递的内容为ControllerComponent

问题2 - missing parameter meta:AssetsMetaData

我收到以下错误lazy val assets = new controllers.Assets(httpErrorHandler)

问题3 - 我想我也没有正确使用路线。我可以看到IDE显示的是潜在错误cannot resolve constructor

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

我遵循以下教程但由于它基于Play 2.4,它可能不会在2.6中运行。如何才能使上面的代码在Play 2.6

中运行

http://manuel.kiessling.net/2016/01/17/compile-time-cassandra-injection-in-play-2-4/

1 个答案:

答案 0 :(得分:0)

答案1 - BuiltInComponentsFromContext的实例为ControllerComponents

回答2 - assets中定义的BuiltInComponentsFromContext使用。

答案3 - routes文件期望将所有控制器传递给它。就我而言。我没有通过所有控制器。

完整代码

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


  lazy val userRepositoryController = new controllers.UserController(userRepository, controllerComponents) //answer 1
  lazy val homeController = new controllers.HomeController(controllerComponents, csrfAddToken,csrfCheck) //answer 3. create all controllers and pass it to Routes below
  //AtomicCounter is application's class, not Play's class to it is instantiated explicitly in the loader.
  lazy val countController = new controllers.CountController(controllerComponents,new AtomicCounter())
  lazy val asyncController = new controllers.AsyncController(controllerComponents, actorSystem)

  lazy val userWSRoutes = new WSRouters.User.UserRouter(userRepositoryController) //I am using SIRD router, so creating them here

  lazy val router = new Routes(httpErrorHandler, homeController,userWSRoutes, countController,asyncController, assets) //answer to 2


}