mpartan
mpartan

Reputation: 1316

Migrating Play Framework 2.5 - moving from Global.onStart to Dependency Injection

So I am trying to migrate a PlayFramework application from version 2.4.3 to 2.5.6. I am using Squeryl and akka-quartz-scheduler, and Squeryl requires setting up a session manually and akka-quartz-scheduler runs as its own entity, as none of the other modules really depend on it, though it will depend on others. So previously there has been a Global-object to handle them on start up:

import org.squeryl.{Session, SessionFactory}
object Global extends GlobalSettings {
    private lazy val injector = Guice.createInjector(CustomModule)

    override def onStart(app: Application) {
        SessionFactory.concreteFactory = // Squeryl initialization http://squeryl.org/sessions-and-tx.html
        injector.getInstance(classOf[CustomScheduler]).initialize()
    }
}

This has worked before. However, on 2.5.6 I'm trying to shift away from Global.scala altogether. I'm not sure if this is the best way to do this, but from documentation it seems like it. So I'm trying to create Singleton classes, and load them eagerly before the application loads like instructed here as a replacement for onStart. So like instructed on eager bindings -page I have:

import com.google.inject._

class CustomModule extends AbstractModule {
  override def configure() = { // or without override
    println("configure called")
    bind(classOf[SquerylInitialization]).to(classOf[SquerylInitialization]).asEagerSingleton()
    bind(classOf[CustomScheduler]).to(classOf[CustomScheduler]).asEagerSingleton()
  }
}


import play.api.{Configuration, Application}
import play.api.db.{DB, DBApi}
import org.squeryl.{SessionFactory, Session}
@Singleton
class SquerylInitialization @Inject()(conf: Configuration, dbApi: DBApi) extends Logging {
     SessionFactory.concreteFactory = // Squeryl initialization http://squeryl.org/sessions-and-tx.html
}


import akka.actor.{ActorSystem, ActorRef}
@Singleton
class CustomScheduler @Inject()(system: ActorSystem) extends Logging {
    val scheduler: QuartzSchedulerExtension = QuartzSchedulerExtension(system)
    // other initialize code here
}

The CustomModule inheriting the AbstractModule and its configure()-method is never called. It is said in Guice documentation that "Alternatively, play will scan the classpath for classes that implement AbstractModule". Documentation might not be the most recent, but that seems to be the way it works.

If for instance on all classes using Squeryl I use dependency injection to load SquerylInitialization it works, but I'm not sure if that's good way to do it as it would have to be required by tons of Classes, and there is hardly any Class depending on the CustomScheduler.

So basically the questions are:

Upvotes: 2

Views: 761

Answers (1)

mpartan
mpartan

Reputation: 1316

So basically comments are correct and the documentation was just out of date, so including

play.modules.enabled += "module.CustomModule"

helped. Thought I tried that as well, but turns out I didn't. Answer just a comment so can't accept that.

Upvotes: 1

Related Questions