简体   繁体   English

使用 Play 从 java 类访问 application.conf 属性! 2.0

[英]Accessing the application.conf properties from java class with Play! 2.0

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.问题是我不知道如何从 java 类访问这些属性。 I tried this:我试过这个:

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?假设有一个我可以使用的全局 Configuration 实例,我错了吗? Thanks.谢谢。

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 .如评论 (nico_ekito) 中所述,请使用play.Play而不是play.api.Play play.api.Play is for scala controllers (see comment by Marcus biesior Biesioroff) play.api.Play用于 Scala 控制器(参见 Marcus biesior Biesioroff 的评论)

Additionally, play uses https://github.com/typesafehub/config under the hood so it can also provide some insights.此外,play 在底层使用https://github.com/typesafehub/config ,因此它也可以提供一些见解。

Even if it seems simple, here is the scala way 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");

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");

由于 Play 2 使用了 Typesafe 配置库,因此我在 application.conf 中访问了我的变量,如下所示:

ConfigFactory.load().getString("my.var");

In the play java is:在剧中java是:

import play.Play;
...
Play.application().configuration().getString("key")

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 的位置

作为从模板访问它的参考(用于播放 < 2)

play.configuration['your.key']

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")

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其中第二个参数是默认值

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.不要忘记导入 play.Play。 Hope it'll gives you help希望它会给你帮助

从版本 2.5 开始,请使用应该注入的 play.Application 类,然后使用application.config().getString("your.property.here")

For Java Playframework:对于 Java Playframework:

In Application.conf , you can put something like that:Application.conf 中,您可以输入以下内容:

email="example@gmail.com.pe"电子邮件="example@gmail.com.pe"

some class:某类:

import play.Play;导入 play.Play;

String email = Play.application().configuration().getString("key") // key ->email String email = Play.application().configuration().getString("key") // key ->email

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM