使用Play从java类访问application.conf属性! 2.0 [英] Accessing the application.conf properties from java class with Play! 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.
推荐答案
尝试 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)
a href =https://github.com/typesafehub/config> 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屋!