j3d*_*j3d 5 scala playframework
我需要在加载配置文件之后但在应用程序实际启动之前读取一些配置值.
在Play 2.3.x中,我曾经使用覆盖GlobalSettings.onLoadConfig,在Play 2.4.x中已弃用.官方文件说应该使用GuiceApplicationBuilder.loadConfig.
再次,文档有点差,我无法找到更多的细节或示例......所以任何帮助将非常感激.
如果您需要在应用程序启动之前读取配置,可以使用以下方法:
modules/CustomApplicationLoader.scala:
package modules
import play.api.ApplicationLoader
import play.api.Configuration
import play.api.inject._
import play.api.inject.guice._
class CustomApplicationLoader extends GuiceApplicationLoader() {
override def builder(context: ApplicationLoader.Context): GuiceApplicationBuilder = {
println(context.initialConfiguration) // <- the configuration
initialBuilder
.in(context.environment)
.loadConfig(context.initialConfiguration)
.overrides(overrides(context): _*)
}
}
Run Code Online (Sandbox Code Playgroud)
conf/application.conf 添加了以下内容:
play.application.loader = "modules.CustomApplicationLoader"
Run Code Online (Sandbox Code Playgroud)
有了这个,我在控制台中看到以下内容(剪断太长):
Configuration(Config(SimpleConfigObject({"akka":{"actor":{"creation-timeout":"20s"...
Run Code Online (Sandbox Code Playgroud)
资料来源:文件.
如果您不需要在应用程序启动之前读取配置,则可以使用此方法:(这非常简单)Module绑定方法需要Play环境和配置才能阅读:
class HelloModule extends Module {
def bindings(environment: Environment,
configuration: Configuration) = {
println(configuration) // <- the configuration
Seq(
bind[Hello].qualifiedWith("en").to[EnglishHello],
bind[Hello].qualifiedWith("de").to[GermanHello]
)
}
}
Run Code Online (Sandbox Code Playgroud)