java 自定义配置文件 - 玩!框架 2.0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10392545/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:51:37  来源:igfitidea点击:

Custom configuration files - Play! Framework 2.0

javafileplayframework-2.0custom-configuration

提问by travega

I have a question about loading properties from custom configuration files. I have tried two different approaches to loading my oauth.propertiesfile but I can't get either to work so I'm hoping someone here can help me.

我有一个关于从自定义配置文件加载属性的问题。我尝试了两种不同的方法来加载我的oauth.properties文件,但我无法使用任何一种方法,所以我希望这里有人可以帮助我。

The first approach I tried was to add the file to the conf directory and reference it thusly:

我尝试的第一种方法是将文件添加到 conf 目录并以此引用它:

String oauthPropertiesFile = ClassLoader.getSystemResource("oauth.properties").getFile();

But that just returned NULL.

但那只是回来了NULL

The second approach I tries was to add:

我尝试的第二种方法是添加:

@include.oauthProperties = oauth.properties

to the application.conffile and then reference it in my controller like:

application.conf文件,然后在我的控制器中引用它,如:

String clientId = oauthProperties.clientId;

However this doesn't compile.

但是,这不会编译。

Can anyone shed some light on what I'm doing wrong here?

任何人都可以阐明我在这里做错了什么吗?

回答by Victor Sergienko

What worked for me with a file in /conf:

什么对我有用/conf

import com.typesafe.config.ConfigFactory

val myConfig = ConfigFactory.load("myfile.properties").getConfig("my.config.prefix")

回答by niels

I'm not sure if conf is part of the classpath. So I would try /conf/oauth.propertiesor put the file into the classpath. Furthermore you should use Play.application.classloader()instead of Classloader.

我不确定 conf 是否是类路径的一部分。所以我会尝试/conf/oauth.properties或将文件放入类路径。此外,您应该使用Play.application.classloader()而不是 Classloader。

About the include: I still think you need to call Play.application().configuration().get("clientID");

关于包括:我还是觉得你需要打电话 Play.application().configuration().get("clientID");

To analyze the situation you can start the app with -Dconfig.trace=loadsand analyse the configuration with Play.application().configuration().root().render().

要分析情况,您可以使用 启动应用程序-Dconfig.trace=loads并使用 分析配置Play.application().configuration().root().render()

Hope this give you enough hints so that you can solve your problem.

希望这能给您足够的提示,以便您可以解决您的问题。

回答by Didac Montero

In a generic case, to fetch any File from the "/conf" directory, using Play 2.2 it can be done as follows (note that "/conf" is on the classpath, so you should not include it).

在一般情况下,要从“/conf”目录中获取任何文件,使用 Play 2.2 可以按如下方式完成(请注意,“/conf”位于类路径中,因此不应包含它)。

Play.application().classloader().getResource("any_file");

回答by bsmk

If you want to use scala.io.Sourceyou can also do something like

如果你想使用scala.io.Source你也可以做类似的事情

Source.fromFile(Play.getFile("path-in-your-play-project"))