玩 2.4 和 Guice Module,如何重用我现在拥有的东西?

Play 2.4 and Guice Module, how can I re-use what I have now?

我正在升级到 Playframework 2.4,目前我有这个:

Global.scala:

object Global extends GlobalSettings with LazzyLogging {
  private lazy val injector = {
    Guice.createInjector(new ServiceModule)
  }
}

override def getControllerInstance[A](controllerClass: Class[A]): A = {
    injector.getInstance(controllerClass)
  }

ServiesModule.scala:

class ServicesModule extends ScalaModule {
  def configure() {
    bind[userService].to[UserServiceImpl]
    ...
    .
  }
}

我没有遇到编译错误:

Global.scala:28: method getControllerInstance overrides nothing
[error]   override def getControllerInstance[A](controllerClass: Class[A]): A = {

我的路由文件包含依赖注入路由:

GET /abc         @controller.HomeController.index

我需要更改什么,我希望我可以重新使用我的 ServicesModule 但它正在使用 sse-guice library

对于您在问题中显示的内容,您必须更改少量内容:

  1. 开始使用injected routes这样你的控制器就可以有DI了。只需将以下行添加到您的 build.sbt 文件中:

    routesGenerator := InjectedRoutesGenerator

这还需要您更改 routes 文件以从路由声明中删除 @。因此,您的路线将是:

GET /abc         controller.HomeController.index
  1. 使用 play.modules.enabled 配置为您的 ServiceModule 提供 custom binding。为此,只需将以下行添加到您的 application.conf

    play.modules.enabled += "com.acme.services.ServiceModule"

您可能需要将 ServiceModule 更改为扩展 AbstractModule 而不是 ScalaModule:

import com.google.inject.AbstractModule

class ServicesModule extends AbstractModule {
  def configure() {
    bind[userService].to[UserServiceImpl]
  }
}
  1. Remove your GlobalSettings 支持新的做事方式。

此外,我建议您阅读Migration Guide。它充满了关于如何调整代码的信息。