I need to read some configuration values just after the configuration file has been loaded but before the application actually starts.
In Play 2.3.x I used to override GlobalSettings.onLoadConfig, which is deprecated in Play 2.4.x. The official documentation says one should use GuiceApplicationBuilder.loadConfig instead.
Again, the documentation is a bit poor and I was unable to find more details or an example... so any help would be really appreciated.
If you need to read configuration before app starts, this approach can be used:
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): _*)
  }
}
conf/application.conf has the following added:
play.application.loader = "modules.CustomApplicationLoader"
With that, I see the following in console (snipped as too long):
Configuration(Config(SimpleConfigObject({"akka":{"actor":{"creation-timeout":"20s"...
Source: documentation.
If you don't need to read configuration before app starts, this approach can be used instead: (it's so embarrassingly simple) the Module bindings method takes a Play Environment and Configuration for you to read:
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]
    )
  }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With