我正在将我的应用程序从play 2.3迁移到2.4。
在我的GlobalSettings中的2.3应用程序中,我不得不使用slick访问数据库以创建postgres数据库函数。
由于在2.4中不推荐使用GlobalSettings,所以可以使用热切的绑定:
https://www.playframework.com/documentation/2.4.x/ScalaDependencyInjection#Eager-bindings
就像这样:
class MyModule extends AbstractModule {
def configure() = {
db.withSession { implicit ss =>
StaticQuery.update("""CREATE OR REPLACE FUNCTION ... """).execute
}
}
}但这给了我一个错误:
java.lang.ExceptionInInitializerError:
core.includes$.<init>(includes.scala:14)
core.includes$.<clinit>(includes.scala)
Application$$anonfun$configure$1.apply(Application.scala:17)
Application$$anonfun$configure$1.apply(Application.scala:15)
scala.slick.backend.DatabaseComponent$DatabaseDef$class.withSession(DatabaseComponent.scala:34)
scala.slick.jdbc.JdbcBackend$DatabaseFactoryDef$$anon$4.withSession(JdbcBackend.scala:61)
modules.jdbc.Database$$anonfun$withSession$1.apply(Database.scala:14)
modules.jdbc.Database$$anonfun$withSession$1.apply(Database.scala:14)
Application.configure(Application.scala:15)
com.google.inject.AbstractModule.configure(AbstractModule.java:62)
com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:340)
com.google.inject.spi.Elements.getElements(Elements.java:110)
com.google.inject.util.Modules$OverrideModule.configure(Modules.java:177)
com.google.inject.AbstractModule.configure(AbstractModule.java:62)
com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:340)
com.google.inject.spi.Elements.getElements(Elements.java:110)
com.google.inject.internal.InjectorShell$Builder.build(InjectorShell.java:138)
com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:104)
com.google.inject.Guice.createInjector(Guice.java:96)
com.google.inject.Guice.createInjector(Guice.java:73)
com.google.inject.Guice.createInjector(Guice.java:62)
play.api.inject.guice.GuiceBuilder.injector(GuiceInjectorBuilder.scala:126)
play.api.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.scala:93)
play.api.inject.guice.GuiceApplicationLoader.load(GuiceApplicationLoader.scala:21)有人知道我怎么解决这个问题吗?谢谢。
发布于 2015-07-24 05:59:27
根据https://www.playframework.com/documentation/2.4.2/GlobalSettings的引文:
GlobalSettings.beforeStart和GlobalSettings.onStart:在启动时需要发生的任何事情现在都应该发生在依赖注入类的构造函数中。当依赖注入框架加载类时,类将执行它的初始化。如果您需要急切的初始化(因为您需要在应用程序实际启动之前执行一些代码),那么定义一个急切的绑定。
正如您所看到的,只有当需要在应用程序启动之前发生时才能使用急切的绑定,这在您的情况下是不适用的,因为db.withSession需要启动应用程序上下文。这就是为什么会出现异常(严格地说,您没有以适当的方式使用热切绑定)。
那么,你如何才能实现这个目标呢?答案在引文的前两句中。
首先,您必须定义如下内容:
@Singleton
class DatabaseService {
db.withSession { implicit ss =>
StaticQuery.update("""CREATE OR REPLACE FUNCTION ... """).execute
}
}然后,如果将DatabaseService注入另一个单例类(最好将它注入另一个单例类,否则代码可能会被多次调用),然后由Guice初始化后一个类,则调用DatabaseService构造函数中的代码(因为DatabaseService首先被Guice初始化为后一个类的依赖项)。
例如,您可以将其注入控制器:
@Singleton
class Application @Inject() (dbService: DatabaseService) extends Controller {
def index = Action {
Ok(views.html.index("Your new application is ready."))
}
}然后,如果有人访问索引页,您的代码将被执行。
编辑:
我找到了另一个关于这个主题的堆栈溢出帖子,请看这里:PlayFramework 2.4 run some code after application has started。它找到了在应用程序启动后运行代码的正确方法,同时仍然使用急切的绑定。:)
如果您依赖于ActorSystem,只需将actorSystem: ActorSystem注入您的类,如下所示:
@Singleton
class QuartzSchedulerService @Inject() (configuration: Configuration,
actorSystem: ActorSystem,
@Named("library.actors.ApiExecutionRecorderRouter") apiExecutionRecorderRouter: ActorRef
) {
val scheduler = QuartzSchedulerExtension(actorSystem)
scheduler.schedule("QuartzSchedulerTest", apiExecutionRecorderRouter, "Start")
}https://stackoverflow.com/questions/31454857
复制相似问题