简体   繁体   中英

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

I am upgrading to Playframework 2.4 and I have this currently:

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]
    ...
    .
  }
}

I am not getting a compile error:

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

My routes file has this for dependancy injected routes:

GET /abc         @controller.HomeController.index

What do I have to change, I hope I can re-use my ServicesModule but it is using the sse-guice library .

For what you show in your question, you have to change a small amount of things:

  1. Start to use injected routes so that you can have DI in your controllers. Just add the following line to your build.sbt file:

    routesGenerator := InjectedRoutesGenerator

This will also requires that you change your routes file to remove @ from routes declarations. So, your route will be:

GET /abc         controller.HomeController.index
  1. Provide custom binding to your ServiceModule using play.modules.enabled configuration. To do that, just add the following line to your application.conf :

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

You may need to change your ServiceModule to extends AbstractModule instead of ScalaModule :

import com.google.inject.AbstractModule

class ServicesModule extends AbstractModule {
  def configure() {
    bind[userService].to[UserServiceImpl]
  }
}
  1. Remove your GlobalSettings in favor of new way to do things.

Also, I recommend that you read the Migration Guide . It is full with information about how you can adapt your code.

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