EvolutionsComponents在编译时依赖注入游戏项目中

时间:2016-10-04 18:54:12

标签: scala playframework-2.5 playframework-evolutions

我试图了解如何使用编译时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)类AppComponents需要是抽象的,因为方法dbApi在traits EvolutionsComponents中的类型=> play.api.db.DBApi未定义 class AppComponents(context:Context)使用EvolutionsComponents扩展BuiltInComponentsFromContext(context)和EhCacheComponents

我是Scala的新手。

2 个答案:

答案 0 :(得分:6)

<li><a href="<?php echo $my_module; ?>">text_my_module</a></li> 来自dbApi特征,因此您的DBComponents课程也需要扩展AppComponents。您还需要为连接池扩展DBComponents

HikariCPComponents

请务必将class AppComponents(context: Context) extends BuiltInComponentsFromContext(context) with EhCacheComponents with EvolutionsComponents with DBComponents with HikariCPComponents { evolutions个依赖关系添加到jdbc文件中。

答案 1 :(得分:0)

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

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

并在build.sbt

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

...