使用 Play 从 java 类访问 application.conf 属性!2.0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9844568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing the application.conf properties from java class with Play! 2.0
提问by Nitzan Tomer
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.
我想向全局范围添加一个对象,为了构造它,我需要将一个路径传递给它。我不想在源代码中对文件路径进行硬编码,因此我想从 application.conf 中获取该路径。
The problem is that I don't know how to access these properties from the java class. I tried this:
问题是我不知道如何从 java 类访问这些属性。我试过这个:
Configuration.root().getString("file.path")
But it ends with a NullPointerException.
但它以NullPointerException结束。
Am I wrong in assuming that there's a global Configuration instance that I can use? Thanks.
假设有一个我可以使用的全局 Configuration 实例,我错了吗?谢谢。
采纳答案by Nasir
Try Play.application().configuration().getString("your.key")
尝试 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)
如评论 (nico_ekito) 中所述,请使用play.Play
而不是play.api.Play
。play.api.Play
用于 Scala 控制器(见 Marcus biesior Biesioroff 的评论)
Additionally, play uses https://github.com/typesafehub/configunder the hood so it can also provide some insights.
此外,play在底层使用https://github.com/typesafehub/config,因此它也可以提供一些见解。
回答by i.am.michiel
Even if it seems simple, here is the scalaway to get properties from configuration file :
即使看起来很简单,以下是从配置文件中获取属性的Scala方法:
Play 2.0 and 2.1:
玩 2.0 和 2.1:
import play.api.Play.current
...
Play.application.configuration.getString("your.key")
Play 2.2 and +
玩 2.2 和 +
import play.api.Play.current
...
current.configuration.getString("your.key")
Using Typesafe config
使用类型安全配置
import com.typesafe.config.ConfigFactory
...
ConfigFactory.load().getString("your.key");
回答by Cyril N.
Since Play 2 uses the Typesafe config library, I accessed my vars in application.conf like this :
由于 Play 2 使用了 Typesafe 配置库,因此我在 application.conf 中访问了我的变量,如下所示:
ConfigFactory.load().getString("my.var");
回答by fmsf
As a reference to access it from the template (for play < 2)
作为从模板访问它的参考(用于播放 < 2)
play.configuration['your.key']
回答by Rajesh
Use as following (Tested in Play 1.2.5)
使用如下(在 Play 1.2.5 中测试)
${play.configuration.getProperty('my.var')}
where my.var should be specified in application.conf file
应在 application.conf 文件中指定 my.var 的位置
回答by Alfaville
In the play java is:
在剧中java是:
import play.Play;
...
Play.application().configuration().getString("key")
回答by cmd
As folks have mentioned, Play.application.configuration
no longer exists.
正如人们所说,Play.application.configuration
不再存在。
In Play Scala 2.3.x, to read a value from conf/application.conf
, you can do the following:
在 Play Scala 2.3.x 中,要从 读取值conf/application.conf
,您可以执行以下操作:
import play.api.Play.current
...
current.configuration.getString("key")
回答by Saeed Zarinfam
From Play 2.4 and +
it is better to use dependency injection to access Configurations:
从Play 2.4 and +
最好是使用依赖注入来访问配置:
import play.Configuration;
import javax.inject.Inject;
@Inject
private Configuration configuration;
...
String value = configuration.getString("your.key");
回答by user9869932
In Play 1.2.x
在玩 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
其中第二个参数是默认值
回答by Atul Chavan
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");