Reputation: 164237
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.
Upvotes: 62
Views: 56375
Reputation: 111
For Java Playframework:
In Application.conf, you can put something like that:
email="[email protected]"
some class:
import play.Play;
String email = Play.application().configuration().getString("key") // key ->email
Upvotes: 0
Reputation: 89
Starting from version 2.5 please use play.Application class which should be injected and then
application.config().getString("your.property.here")
Upvotes: 0
Reputation: 1
import play.Play;
String myVal = Play.configuration.getProperty("your.key").toString();
i use this in my app and it works
Dont forget to import play.Play. Hope it'll gives you help
Upvotes: 0
Reputation: 1794
Import this
import com.typesafe.config.Config;
and write the below lines
private Config config;
this.config = ConfigProvider.config();
String value = this.config.getString("fieldFromConfigFile");
Upvotes: 0
Reputation: 7357
In Play 1.2.x
import play.Play;
...
String version = Play.configuration.getProperty("application.version.number", "1.1.1");
where the second parameter is the default value
Upvotes: 0
Reputation: 10190
From Play 2.4 and +
it is better to use dependency injection to access Configurations:
import play.Configuration;
import javax.inject.Inject;
@Inject
private Configuration configuration;
...
String value = configuration.getString("your.key");
Upvotes: 39
Reputation: 10404
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");
Upvotes: 55
Reputation: 11841
As folks have mentioned, Play.application.configuration
no longer exists.
In Play Scala 2.3.x, to read a value from conf/application.conf
, you can do the following:
import play.api.Play.current
...
current.configuration.getString("key")
Upvotes: 1
Reputation: 553
In the play java is:
import play.Play;
...
Play.application().configuration().getString("key")
Upvotes: 3
Reputation: 21
Use as following (Tested in Play 1.2.5)
${play.configuration.getProperty('my.var')}
where my.var should be specified in application.conf file
Upvotes: 2
Reputation: 37167
As a reference to access it from the template (for play < 2)
play.configuration['your.key']
Upvotes: 1
Reputation: 2984
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.
Upvotes: 83
Reputation: 39889
Since Play 2 uses the Typesafe config library, I accessed my vars in application.conf like this :
ConfigFactory.load().getString("my.var");
Upvotes: 12