简体   繁体   中英

EvolutionsComponents in compile-time dependency injection play project

Im trying to understand how to run evolutions using compile-time 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)


}

But I keep getting error Error:(19, 7) class AppComponents needs to be abstract, since method dbApi in trait EvolutionsComponents of type => play.api.db.DBApi is not defined class AppComponents(context: Context) extends BuiltInComponentsFromContext(context) with EhCacheComponents with EvolutionsComponents

I'am newbie in Scala.

dbApi comes from the DBComponents trait, so your AppComponents class needs to also extend DBComponents . You'll also need to extend HikariCPComponents for the connection pool.

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

Be sure to add the evolutions and jdbc dependencies to your build.sbt file.

I needed to extend all of this. More reading Cake Pattern Play documentation

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

and add jdbc suport inside build.sbt

libraryDependencies ++= Seq(
 filters,
 evolutions,
 jdbc,
 cache,

....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM