使用 Play 框架,我在设置路由器时做错了什么

With Play framework what am I doing wrong in setting up my routers

我是 Play 和 Scala(2.6 版)的新手,我不知道如何让路由以简单的方式工作。将 2.6 文档中的示例拼凑在一起,我设法创建了一个自定义应用程序加载器,据我所知,这是执行 Evolutions 迁移所必需的。我发现的例子包括一个 var router = Routes.empty BuiltInComponentsFromContext 似乎需要使用一个路由器,但是在这样做的过程中,按照我完成它的方式,我的路由现在被破坏了,现在我得到的只是 "Action Not Found" 消息。

这是我的 application.conf:

play.application.loader=MyApplicationLoader
router = my.application.Router

这是应用程序加载器

import play.api.ApplicationLoader
import play.api.ApplicationLoader.Context
import play.api.BuiltInComponentsFromContext
import play.api.db.{Database, DBComponents, HikariCPComponents}
import play.api.db.evolutions.EvolutionsComponents
import play.api.routing.Router
import play.filters.HttpFiltersComponents
//import com.softwaremill.macwire._

class MyApplicationLoader extends ApplicationLoader {
  def load(context: Context) = {
    new MyComponents(context).application
  }
}

class MyComponents(cntx: Context)
  extends BuiltInComponentsFromContext(cntx)
    with DBComponents
    with EvolutionsComponents
    with HikariCPComponents
    with HttpFiltersComponents
{
  // this will actually run the database migrations on startup
  //lazy val router = Router.empty
  val router = Router.empty
  applicationEvolutions
}

在我看来它声明:

val router = Router.empty

我实际上是在使我在 conf/routes 文件中声明的任何路由无效,我想使用 Router.load 方法,但我找不到示例如何将所需的环境和配置值传递给方法。假设我不想使用静态路由,我该怎么做?

假设您只是为了 Evolutions 而使用编译时依赖注入(因为否则您之前会遇到同样的问题),答案是您不必那样做。 Evolutions 也适用于默认的动态依赖注入。 The part of the documentation you probably basing your assumptions on actually says that if you are already using the compile-time dependency injection, here is how to modify it to make evolutions work. If you look at the source code of the EvolutionsModule you may see that ApplicationEvolutions is bound eagerly. It means that an instance of ApplicationEvolutions will be created at the start of the app during application initialization. And in the source code of the ApplicationEvolutions 本身你可以看到 start() 是从构造函数调用的。因此,如果您提供了配置,其余的应该自行完成。