I have a question about loading properties from custom configuration files. I have tried two different approaches to loading my oauth.properties
file but I can't get either to work so I'm hoping someone here can help me.
The first approach I tried was to add the file to the conf directory and reference it thusly:
String oauthPropertiesFile = ClassLoader.getSystemResource("oauth.properties").getFile();
But that just returned NULL
.
The second approach I tries was to add:
@include.oauthProperties = oauth.properties
to the application.conf
file and then reference it in my controller like:
String clientId = oauthProperties.clientId;
However this doesn't compile.
Can anyone shed some light on what I'm doing wrong here?
What worked for me with a file in /conf
:
import com.typesafe.config.ConfigFactory
val myConfig = ConfigFactory.load("myfile.properties").getConfig("my.config.prefix")
I'm not sure if conf is part of the classpath. So I would try /conf/oauth.properties
or put the file into the classpath. Furthermore you should use Play.application.classloader()
instead of Classloader.
About the include: I still think you need to call Play.application().configuration().get("clientID");
To analyze the situation you can start the app with -Dconfig.trace=loads
and analyse the configuration with Play.application().configuration().root().render()
.
Hope this give you enough hints so that you can solve your problem.
在一般情况下,要使用Play 2.2从“ / conf”目录中获取任何文件,可以按照以下步骤进行操作(请注意,“ / conf”在类路径上,因此您不应包括它)。
Play.application().classloader().getResource("any_file");
如果要使用scala.io.Source
还可以执行以下操作
Source.fromFile(Play.getFile("path-in-your-play-project"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.