scala 如何在 Play 2.x 中检测应用程序模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14613148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 04:54:08  来源:igfitidea点击:

How to detect application mode in Play 2.x

scalaplayframeworkplayframework-2.0

提问by kes

From within a Play 2.1 application, how would I programmatically determine which mode the application is running in (i.e., Developmentvs. Production)?

在 Play 2.1 应用程序中,我将如何以编程方式确定应用程序在哪种模式下运行(即DevelopmentProduction)?

For example, it would be useful to be able to do something like this from inside a template:

例如,能够从模板内部执行以下操作会很有用:

<p>@if(__some_play_API_call__ == Dev) { <b>Development mode</b> }</p>

<p>@if(__some_play_API_call__ == Dev) { <b>Development mode</b> }</p>

In the Play 2.0 API documentation, there appears to be a modeproperty of the play.api.Applicationclass... however, I am unsure about how to get at the instanceof the currently running application.

在 Play 2.0 API 文档中,似乎有一个mode属性......但是,我不确定如何获取当前正在运行的应用程序的实例play.api.Application

回答by maxmc

You can access the current Appliction via

您可以通过以下方式访问当前应用程序

play.api.Play.current()

to find out the mode try

找出模式尝试

play.api.Play.current().mode()

or simply use

或者干脆使用

play.api.Play.isDev(play.api.Play.current())

回答by koppor

In Play 2.5.x the play.Play.isDev()method is deprecated, one has to use dependency injection:

在 Play 2.5.x 中,该play.Play.isDev()方法已弃用,必须使用依赖注入

import javax.inject.Inject;

public class Example {

    @Inject
    private play.Environment environment;

    public void myMethod() {
        if (environment.isDev()) {
          ...
        }
    }
}

Or equivalently in Scala:

或者在 Scala 中等效:

class ErrorHandler @Inject()(environment: Environment) {

  def myMethod() = {
    if (environment.isDev) {
      ...
    }
  }

}

environment.isDev()returns a Boolean, which one can easily pass to a template. No need to use implicit variables as described here.

environment.isDev()返回一个布尔值,可以轻松地将其传递给模板。无需使用此处所述的隐式变量。

回答by pme

With Play 2.5, Play 2.6and Play 2.7

使用Play 2.5Play 2.6Play 2.7

You can do it like this:

你可以这样做:

import play.Environment

class MyController @Inject()(env: Environment) {

  println(s"DevMode is ${env.isDev}")
  println(s"ProdMode is ${env.isProd}")
  println(s"TestMode is ${env.isTest}")

}

Or in Play 2.6and Play 2.7you have also the version with play.api.Environment:

或者在Play 2.6Play 2.7 中,您还有以下版本play.api.Environment

import play.api.Environment

class MyController @Inject()(env: Environment) {

  println(s"ProdMode is ${env.mode == Mode.Prod}")
  println(s"DevMode is ${env.mode == Mode.Dev}")
  println(s"TestMode is ${env.mode == Mode.Test}")
}

For both the Scala Doc states:

对于 Scala Doc 状态:

/**
 * The environment for the application.
 *
 * Captures concerns relating to the classloader and the filesystem for the application.
 */

回答by Gus

In play 2.3.X you can also check via:

在 play 2.3.X 中,您还可以通过以下方式检查:

play.Play.isProd()
play.Play.isDev()
play.Play.isTest()

回答by icl7126

In Play 2.5 using Scala there is a context.environment.modevalue of Enumerationfrom play.api.Modewith one of the values Dev, Test, Prod.
For compile time dependency injection you have contextavailable in your app loader and if you extend BuiltInComponentsFromContextthen you can use (inject) directly environment.mode

在使用 Scala 的 Play 2.5 中,有一个fromcontext.environment.mode值,其中一个值是。 对于编译时依赖注入,您可以在应用程序加载器中使用,如果您扩展,则可以直接使用 (inject)Enumerationplay.api.ModeDev, Test, Prod
contextBuiltInComponentsFromContextenvironment.mode

回答by Jonik

In Play 2.6, inject an Environmentinstance and use its modefield: one of play.api.Modeenum values.

Play 2.6 中,注入一个Environment实例并使用其mode字段:play.api.Mode枚举值之一。

import javax.inject.Inject
import play.api.Environment
import play.api.Mode.Prod
import play.api.mvc.{AbstractController, ControllerComponents}

class TestController @Inject()(cc: ControllerComponents, env: Environment)
    extends AbstractController(cc) {

  def hello = Action {
    if (env.mode == Prod) {
??????// ...
    }

    Ok(s"Hello world in ${env.mode} mode")
  }

}

At least in Play 2.6.20, the methods env.isDev, env.isProd, etc, mentioned by pme, no longer work.

至少在 Play 2.6.20中, pme 提到的方法env.isDevenv.isProd等不再有效。