Is there a good way Play2 for injection in Play Plugins with Guice

I am trying to figure out how to introduce my classes using Google Guice in play.api.Plugin. I implemented Guice to work with my controllers, and it works great.

I use:

"com.google.inject" % "guice" % "4.0-beta",
"com.tzavellas" % "sse-guice" % "0.7.1"

If a Controller instance is required, the getControllerInstance method in Global will load the appropriate implementation thanks to the injector.

Global:

object Global extends GlobalSettings {

  /**
   * Currently we only want to load a different module when test.
   */
  private lazy val injector = {
    Logger.info("Is Test: "+Play.isTest)

    Play.isTest match {
      case true => Guice.createInjector(new TestModule)
      case false => Guice.createInjector(new CommonModule)
    }
  }    

  override def onStart(app: Application) {
    Logger.info("Application has started")
  }

  override def onStop(app: Application) {
    Logger.info("Application shutdown...")
  }

  override def getControllerInstance[A](clazz: Class[A]) = {
    Logger.info("getControllerInstance")
    injector.getInstance(clazz)
  }    
}

General:

package modules

import com.tzavellas.sse.guice.ScalaModule
import services.{CallServiceImpl, CallService}

/**
 * User: jakob
 * Date: 11/5/13
 * Time: 10:04 AM
 */
class CommonModule extends ScalaModule {
  def configure() {
    bind[CallService].to[CallServiceImpl]
  }
}

class TestModule extends ScalaModule {
  def configure() {
    // Test modules!
  }
}

Controller:

@Singleton
class StatsController @Inject()(callService: CallService) extends Controller with securesocial.core.SecureSocial with ProvidesHeader  {

    def doSomething = {
        callService.call()
    }   
}

Now I would like to add the same service to my plugin, but I can’t use the Global implementation, because the plugins do not load using getControllerInstance

class CallerPlugin (application: Application) extends Plugin {

  val secondsToWait = {
    import scala.concurrent.duration._
    10 seconds
  }

  val defaultInterval = 60
  val intervalKey = "csv.job.interval"
  val csvParserEnabled = "csv.job.enabled"
  val newDir = "csv.job.new.file.path"
  val doneDir = "csv.job.done.file.path"

  var cancellable: Option[Cancellable] = None

  override def onStop() {
    cancellable.map(_.cancel())
  }

  override def onStart() {

    // do some cool injection of callService here!!!

    import scala.concurrent.duration._
    import play.api.libs.concurrent.Execution.Implicits._
    val i = current.configuration.getInt(intervalKey).getOrElse(defaultInterval)

    cancellable = if (current.configuration.getBoolean(csvParserEnabled).getOrElse(false)) {
      Some(
        Akka.system.scheduler.schedule(0 seconds, i minutes) {
            callService.call()

        })
    } else None
  }
}

I suppose there must be some way to implement the injection in the onStart method, and there probably is some nice easy way to do this, but I can't figure it out. Thanks!

+4
2

, , Guice. :

val injector = Guice.createInjector(new CommonModule)
val callService = injector.getInstance(classOf[CallService])

CallServiceImpl. Global.scala, , . Play , , , , , onStart, CallService CallerPlugin ( ). , , (, Global).

0

, , - Global.onStart().

public class Global extends GlobalSettings{

    public Injector injector = createInjector();

    private Injector createInjector(){
        return Guice.createInjector(new SomeGuiceModule());
    }

    @Override
    public void onStart(Application application) {
        CallerPlugin plugin = application.plugin(CallerPlugin.class);
        plugin.setCallService(injector.getInstance(CallService.class));
    }
}

, 10000. Global 10000, Global.onStart().

0

All Articles