玩! 2.5: 有没有办法只在 Prod 模式下绑定 actor?

Play! 2.5: Is there a way to bind actors only in Prod mode?

我在 Play 开始时绑定演员!申请如下:

class Modules extends AbstractModule with AkkaGuiceSupport with ScalaModule {

override def configure(): Unit = {
  bindActor[MainSupervisor](MainSupervisor.name)
}

我想在未 运行 生产时停用 MainSupervisor。今天我直接在 actor 中对 Play.env 进行模式匹配,但我想将代码与此分开。

有没有办法在 class Modules Modules 中直接在 Dev 模式下不绑定 actor?

你在配置文件中绑定模块,prod和dev上可以使用不同的配置,所以你可以不在dev配置上绑定模块。

https://www.playframework.com/documentation/2.5.x/ScalaDependencyInjection#programmatic-bindings

https://www.playframework.com/documentation/2.5.x/ProductionConfiguration#specifying-an-alternate-configuration-file

您有两种方法可以实现:

以编程方式

将环境注入模块class并查询其模式

类似于

class Modules(environment: Environment, configuration: Configuration) 
      extends AbstractModule with AkkaGuiceSupport with ScalaModule {

     override def configure(): Unit = {
         if (environment.mode == Mode.Prod) {
            bindActor[MainSupervisor](MainSupervisor.name)
         }
     }

配置

为生产模式指定不同的配置文件

在prod.conf

include "application.conf"
play.modules.enabled += com.mycompany.MyModule