编译时依赖注入播放项目中的 EvolutionsComponents

EvolutionsComponents in compile-time dependency injection play project

我正在尝试了解如何 运行 使用编译时 DI 进行改进。

import play.api.ApplicationLoader.Context
import play.api.cache.EhCacheComponents
import play.api.mvc.EssentialFilter
import play.api.routing.Router
import play.api._
import play.api.db.evolutions.{ DynamicEvolutions, EvolutionsComponents}
import play.filters.gzip.GzipFilter
import router.Routes

class AppLoader extends ApplicationLoader  {
  override def load(context: Context): Application = {
    LoggerConfigurator(context.environment.classLoader).foreach(_.configure(context.environment))
    new AppComponents(context).application
  }


}

class AppComponents(context: Context) extends BuiltInComponentsFromContext(context) with EhCacheComponents with EvolutionsComponents {

  lazy val applicationController = new controllers.Application(defaultCacheApi)
  lazy val usersController = new controllers.Users(defaultCacheApi)
  lazy val assets = new controllers.Assets(httpErrorHandler)

  applicationEvolutions

  // Routes is a generated class
  override def router: Router = new Routes(httpErrorHandler, applicationController, usersController, assets)

  val gzipFilter = new GzipFilter(shouldGzip =
    (request, response) => {
      val contentType = response.header.headers.get("Content-Type")
      contentType.exists(_.startsWith("text/html")) || request.path.endsWith("jsroutes.js")
    })

  override lazy val httpFilters: Seq[EssentialFilter] = Seq(gzipFilter)


}

但我总是出错 错误:(19, 7) class AppComponents 需要是抽象的,因为类型为 => play.api.db.DBApi 的特征 EvolutionsComponents 中的方法 dbApi 未定义 class AppComponents(context: Context) 使用 EhCacheComponents 和 EvolutionsComponents 扩展 BuiltInComponentsFromContext(context)

我是 Scala 的新手。

dbApi 来自 DBComponents 特征,因此您的 AppComponents class 也需要扩展 DBComponents。您还需要为连接池扩展 HikariCPComponents

class AppComponents(context: Context) extends BuiltInComponentsFromContext(context)
  with EhCacheComponents
  with EvolutionsComponents
  with DBComponents
  with HikariCPComponents {

请务必将 evolutionsjdbc 依赖项添加到您的 build.sbt 文件中。

我需要扩展所有这些。更多阅读蛋糕图案Play documentation

class AppComponents(context: Context) extends BuiltInComponentsFromContext(context) with EhCacheComponents with EvolutionsComponents with DBComponents with HikariCPComponents{

并在 build.sbt

中添加 jdbc 支持
libraryDependencies ++= Seq(
 filters,
 evolutions,
 jdbc,
 cache,

.....