【问题标题】:EvolutionsComponents in compile-time dependency injection play project编译时依赖注入 play 项目中的 EvolutionsComponents
【发布时间】:2023-03-20 17:42:01
【问题描述】:

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

我是 Scala 的新手。

【问题讨论】:

    标签: scala playframework-2.5 playframework-evolutions


    【解决方案1】:

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

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

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

    【讨论】:

    • 这行得通:class AppComponents(context: Context) 使用 EhCacheComponents 扩展 BuiltInComponentsFromContext(context) 使用 EvolutionsComponents 使用 DBComponents 使用 HikariCPComponents,但我自己如何得到它?比如这个 HikariCPComponents 也是需要的?
    • “我自己搞定”是什么意思?你想得到什么?另外,您使用什么库进行 JDBC 访问?光滑?
    • 我自己尝试对依赖关系进行逆向工程来得出这个结论。我在看 EvolutionsModule.scala - trait EvolutionsComponents。但是我没有连接 EvolutionsComponents 和 DBComponents (play-jdbc-api_2.11-2.5.6.jar)。再说一遍,我怎么知道我需要包含 libraryDependencies (jdbc),它带来了 play-jdbc_2.11-2.5.6.jar - 其中还有这个 HikariCPComponents 也需要实现。 Cloude 只是以某种方式查看代码或进化文档,我得到了相同的结论?在 Play 文档中,他们对此只字未提。
    • 同意,Play 文档可能会更好。我很高兴你明白了!通常,我对这类问题的策略是查看开源项目的代码示例。
    【解决方案2】:

    我需要扩展所有这些。更多阅读蛋糕图案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,
    

    ....

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-05
      • 2020-03-14
      • 2011-09-29
      • 1970-01-01
      • 2012-07-16
      • 2019-03-26
      • 1970-01-01
      相关资源
      最近更新 更多