简体   繁体   English

如何在scala Play中启动代码! 框架申请?

[英]How to execute on start code in scala Play! framework application?

I need to execute a code allowing the launch of scheduled jobs on start of the application, how can I do this? 我需要执行一个代码,允许在应用程序启动时启动预定作业,我该怎么做? Thanks. 谢谢。

Use the Global object which - if used - must be defined in the default package: 使用Global对象 - 如果使用 - 必须在默认包中定义:

object Global extends play.api.GlobalSettings {

  override def onStart(app: play.api.Application) {
    ...
  }

}

Remember that in development mode, the app only loads on the first request, so you must trigger a request to start the process. 请记住,在开发模式下,应用程序仅在第一个请求时加载,因此您必须触发启动该过程的请求。


Since Play Framework 2.6x 自Play Framework 2.6x起

The correct way to do this is to use a custom module with eager binding: 执行此操作的正确方法是使用具有急切绑定的自定义模块:

import scala.concurrent.Future
import javax.inject._
import play.api.inject.ApplicationLifecycle

// This creates an `ApplicationStart` object once at start-up and registers hook for shut-down.
@Singleton
class ApplicationStart @Inject() (lifecycle: ApplicationLifecycle) {

  // Start up code here

  // Shut-down hook
  lifecycle.addStopHook { () =>
    Future.successful(())
  }
  //...
}
import com.google.inject.AbstractModule

class StartModule extends AbstractModule {
  override def configure() = {
    bind(classOf[ApplicationStart]).asEagerSingleton()
  }
}

See https://www.playframework.com/documentation/2.6.x/ScalaDependencyInjection#Eager-bindings 请参阅https://www.playframework.com/documentation/2.6.x/ScalaDependencyInjection#Eager-bindings

I was getting a similar error. 我收到了类似的错误。 Like @Leo said, create Global object in app/ directory. 就像@Leo所说,在app /目录中创建Global对象。

Only thing I had to make sure was to change "app: Application" to "app: play.api.Application". 我唯一需要确定的是将“app:Application”更改为“app:play.api.Application”。

app: Application referred to class Application in controllers package. app:应用程序在控制器包中引用类Application。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM