0

我有一个控制器。我想将 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 - 我想我也没有正确使用 Routes。我可以看到 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/

4

1 回答 1

0

回答 1 -BuiltInComponentsFromContext有一个实例ControllerComponents

对 2 的回答 -assets在 中定义BuiltInComponentsFromContext

对 3 的回答 - 路由文件期望所有控制器都传递给它。就我而言。我没有通过所有的控制器。

完整代码

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


}
于 2018-03-19T05:42:18.963 回答