Custom Configuration Files - Play! Framework 2.0

I had a question about loading properties from custom configuration files. I tried two different approaches to loading my oauth.properties file, but I cannot either work, so I hope someone here can help me.

The first approach I tried was to add the file to the conf directory and refer to it like this:

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

But it just returned NULL .

The second approach I'm trying to add is:

 @include.oauthProperties = oauth.properties 

to the application.conf file, and then reference it in my controller like:

 String clientId = oauthProperties.clientId; 

However, this does not compile.

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

+7
source share
4 answers

I'm not sure if conf is part of the class path. So I would try /conf/oauth.properties or put the file in the classpath. In addition, you should use Play.application.classloader() instead of Classloader.

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

To analyze the situation, you can run the application using -Dconfig.trace=loads and analyze the configuration using Play.application().configuration().root().render() .

Hope this gives you enough advice so that you can solve your problem.

+12
source

What worked for me with the file in /conf :

 import com.typesafe.config.ConfigFactory val myConfig = ConfigFactory.load("myfile.properties").getConfig("my.config.prefix") 
+16
source

In general, to extract any file from the "/ conf" directory using Play 2.2, this can be done as follows (note that "/ conf" is in the class path, so you should not include it).

 Play.application().classloader().getResource("any_file"); 
+6
source

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

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

All Articles