使用 Play 从 java 类访问 application.conf 属性!2.0 [英] Accessing the application.conf properties from java class with Play! 2.0

查看:10
本文介绍了使用 Play 从 java 类访问 application.conf 属性!2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向全局范围添加一个对象,为了构造它,我需要将一个路径传递给它.我不想在源代码中对文件路径进行硬编码,因此我想从 application.conf 中获取该路径.

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.

问题是我不知道如何从 java 类访问这些属性.我试过这个:

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

但它以 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")

如评论 (nico_ekito) 中所述,请使用 play.Play 而不是 play.api.Play.play.api.Play 适用于 Scala 控制器(参见 Marcus biesior Biesioroff 的评论)

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)

此外,play 在底层使用 https://github.com/typesafehub/config 所以它也可以提供一些见解.

Additionally, play uses https://github.com/typesafehub/config under the hood so it can also provide some insights.

这篇关于使用 Play 从 java 类访问 application.conf 属性!2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆