播放2.4添加模块而不是插件 [英] Play 2.4 Adding Module instead of Plugin

查看:61
本文介绍了播放2.4添加模块而不是插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要迁移到Play 2.4的Play 2.3应用程序,并且我正在处理有关将插件迁移到模块的警告之一.我在这里关注此文档:

I have a Play 2.3 app that I'm migrating to Play 2.4 and I'm working on one of the warnings which is about migrating the plugin's to modules. I'm following this documentation here:

https://www.playframework.com/documentation/2.4.x/PluginsToModules

我的应用程序中现在有几个问题,其中有一些Akka演员.到目前为止,这是我对插件所做的:

I have now a couple of questions in my application that has some Akka actors. So far here is what I did with the plugins:

  class MyPlugin extends Plugin {

    // I get the current Application
    val app = play.api.Play.current

    // On start of the plugin, I call my initializers
    def onStart: Unit = {
      super.onStart()
      doInitialize(app)
    }
  }

现在,在我的doInitialize例程中,我对所需的不同服务进行了初始化.稍后,我将这些服务作为特征公开,并在需要服务引用的地方混合使用此特征.例如,我的控制器如下所示:

Now in my doInitialize routine, I do the initialization of the different services that I need. I later expose these services as a trait and I mix this trait on the places where I need the service references. So for example., my controller looks like:

object Application extends Controller with MyServices {

  ....
}

MyServices所在的地方

where MyServices is

trait MyServices {

  def myActorRef: ActorRef
  ...
  ...
}

现在应如何将此设置迁移到使用模块而对整个应用程序影响不大?从插件迁移到模块时,还不清楚应将onStart方法的内容放在何处!

How should this setup now be migrated to using Modules with little impact to the overall application? It is also not clear as to where I should put my onStart method contents when I migrate from Plugin to Module!

我尝试了以下操作:

sealed trait MyComp
class MyCompImpl @Inject() (lifecycle: ApplicationLifecycle) extends MyComp {
  val application = MyConfig(play.api.Play.current)

  // initialize upon start
  Initializer.doInitialize(application)

  // destroy upon stop
  lifecycle.addStopHook(() => {
    Future.successful { Initializer.destroyConfig(application) }
  })
}

我的模块定义如下:

class MyModule extends Module {

  override def bindings(environment: play.api.Environment, configuration: Configuration): Seq[Binding[_]] = {
    logger.info(s"registering bindings")
    Seq(
      bind[MyComp].to[MyCompImpl].eagerly()
    )
  }
}

这现在失败,原因如下:

This now fails with the reason:

CreationException:无法创建注射器,请参见以下错误:

CreationException: Unable to create injector, see the following errors:

1) Error injecting constructor, java.lang.RuntimeException: There is no started application
  at com.config.MyCompImpl.<init>(MyModule.scala:25)
  while locating com.config.MyCompImpl

每次将Play应用程序升级到新版本时,***都变得越来越痛苦!

It is becoming a pain in the *** with each upgrade to a Play application's to newer versions!

推荐答案

我设法解决了以下问题:

I managed to solve this as below:

sealed trait MyComp
class MyCompImpl @Inject() (app: Application, lifecycle: ApplicationLifecycle) extends MyComp {
  val application = Myconfig(app)

  // initialize upon start
  Initializer.doInitialize(application)

  // destroy upon stop
  lifecycle.addStopHook(() => {
    Future.successful { Initializer.destroyConfig(application) }
  })
}

这篇关于播放2.4添加模块而不是插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆