【问题标题】:Custom Router in Playframework 2.4Playframework 2.4 中的自定义路由器
【发布时间】:2015-08-04 03:49:46
【问题描述】:

我使用的是 Play 2.4。我想用我自己的类替换默认路由器,使用新的动态依赖注入播放功能。这样做的步骤是什么?

【问题讨论】:

    标签: scala playframework url-routing playframework-2.4


    【解决方案1】:

    一种可能的解决方案是创建一个新的 Guice 模块来绑定您的新路由器:

    class RouterModule extends AbstractModule {
    
      override def configure(): Unit = {
        bind(classOf[Router]).to(classOf[CustomRouter])
      }
    }
    

    然后定义一个新的应用程序加载器,它将使用新创建的模块覆盖默认配置的路由器:

    class MyApplicationLoader extends GuiceApplicationLoader with GuiceableModuleConversions {
    
      override protected def overrides(context: Context): Seq[GuiceableModule] = {
        Seq(fromGuiceModule(new RouterModule)) ++ super.overrides(context)
      }
    }
    

    并在 application.conf 中使用新创建的应用程序加载器,而不是默认的:

    play.application.loader = "de.zalando.store.pdp.modules.MyApplicationLoader"
    

    【讨论】:

    • Perplayframework.com/documentation/2.4.x/ScalaRouting: 是否还需要在sbt fil:; routesGenerator := InjectedRoutesGenerator?你能指出我是如何编写自定义路线的文档吗?我链接的文档没有。
    • 恕我直言,您可以通过玩“组件”来替换注入的依赖项 (playframework.com/documentation/2.4.x/…)。这样你只需要编写新的 ApplicationLoader,在 application.conf 中引用它。无需编写 guice 模块。如果你想替换 MessageApi
    猜你喜欢
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多