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.
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");
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");
由于 Play 2 使用了 Typesafe 配置库,因此我在 application.conf 中访问了我的变量,如下所示:
ConfigFactory.load().getString("my.var");
In the play java is:
import play.Play;
...
Play.application().configuration().getString("key")
Use as following (Tested in Play 1.2.5)
${play.configuration.getProperty('my.var')}
where my.var should be specified in application.conf file
作为从模板访问它的参考(用于播放 < 2)
play.configuration['your.key']
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")
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
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");
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
从版本 2.5 开始,请使用应该注入的 play.Application 类,然后使用application.config().getString("your.property.here")
For Java Playframework:
In Application.conf , you can put something like that:
email="example@gmail.com.pe"
some class:
import play.Play;
String email = Play.application().configuration().getString("key") // key ->email
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.