I want to add an object to the Global scope, and in order to construct it I need to pass it a path to a file. I don't want to hard code the file path in the source, and so I want to get that path from the application.conf.
The problem is that I don't know how to access these properties from the java class. I tried this:
Configuration.root().getString("file.path")
But it ends with a NullPointerException.
Am I wrong in assuming that there's a global Configuration instance that I can use? Thanks.
conf file that defines the defaults. you can add an application. conf file in the classpath (or an absolute path, or an URL). It only needs to contain the options that you override.
An application configuration file is an XML file used to control assembly binding. It can redirect an application from using one version of a side-by-side assembly to another version of the same assembly. This is called per-application configuration.
Typesafe Config allows you to store configuration into separated files and use include keyword to include configuration of those files. Here is a concrete example for myApp which relies on the configuration of moduleA and moduleB .
Try Play.application().configuration().getString("your.key")
As noted in the comment (nico_ekito), please use play.Play
and not play.api.Play
. play.api.Play
is for scala controllers (see comment by Marcus biesior Biesioroff)
Additionally, play uses https://github.com/typesafehub/config under the hood so it can also provide some insights.
Even if it seems simple, here is the scala way to get properties from configuration file :
Play 2.0 and 2.1 :
import play.api.Play.current ... Play.application.configuration.getString("your.key")
Play 2.2 and +
import play.api.Play.current ... current.configuration.getString("your.key")
Using Typesafe config
import com.typesafe.config.ConfigFactory ... ConfigFactory.load().getString("your.key");
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