我的控制器定义如下
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
)
}
我的代码未编译,因为我必须在ComponentController
中创建UserController
时传递lazy val applicationController = new controllers.UserController(userRepository)
。但我不知道从哪里得到这个ComponentController
。我该怎么办?
答案 0 :(得分:0)
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
...
}