我有一个控制器。我想将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/
答案 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
}