所以我想将PlayFramework应用程序从版本2.4.3迁移到2.5.6。我使用的是Squeryl和akka-quartz-scheduler,Squeryl需要手动设置会话,akka-quartz-scheduler作为自己的实体运行,因为其他模块都不依赖于它,尽管它依赖于其他模块。所以,以前出现了Global
-object来处理他们在启动时:迁移播放框架2.5 - 从Global.onStart移动到依赖注入
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()
}
}
这已经工作过。不过,在2.5.6上,我试图完全从Global.scala
转移。我不确定这是否是最好的方法,但从documentation看起来好像。所以我正在尝试创建Singleton类,并在应用程序加载之前急切地加载它们,如here作为onStart
的替代。因此,像指示上eager bindings -page我:
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
}
的CustomModule
继承AbstractModule
及其configure()
- 方法永远不会被调用。据说在Guice documentation中,“或者,play将扫描实现AbstractModule的类的类路径”。文档可能不是最新的,但似乎是它的工作方式。
如果例如所有使用Squeryl的类我使用依赖注入来加载SquerylInitialization
它的工作原理,但我不确定这是不是很好的方法来完成它,因为它需要大量的类,并且存在根据CustomScheduler
几乎没有任何类别。
所以基本上的问题是:
为什么没有
CustomModule
调用configure()
- 方法和渴望 加载的类,以及应该如何修复?这是加载这种功能的标准方式,还是应该使用其他方式?
你是否在'application.conf'上启用了'CustomModule',如[这里]所述(https://www.playframework.com/documentation/2.5.x/ScalaDependencyInjection#Programmatic-bindings)(查找'play。 modules.enabled')?另外,我不认为你已经链接的Guice模块已经是必需了。 – marcospereira
文档说:“如果您将此模块**模块** **并将其放入**根包**中,它将自动注册到播放。”否则,您必须在'application.conf'手动注册它。 –