Accessing the application.conf properties from java class with Play! 2.0
Solution 1
Try Play.application().configuration().getString("your.key")
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)
Additionally, play uses https://github.com/typesafehub/config under the hood so it can also provide some insights.
Solution 2
Even if it seems simple, here is the scala way to get properties from configuration file :
Play 2.0 and 2.1 :
import play.api.Play.current
...
Play.application.configuration.getString("your.key")
Play 2.2 and +
import play.api.Play.current
...
current.configuration.getString("your.key")
Using Typesafe config
import com.typesafe.config.ConfigFactory
...
ConfigFactory.load().getString("your.key");
Solution 3
From Play 2.4 and +
it is better to use dependency injection to access Configurations:
import play.Configuration;
import javax.inject.Inject;
@Inject
private Configuration configuration;
...
String value = configuration.getString("your.key");
Solution 4
Since Play 2 uses the Typesafe config library, I accessed my vars in application.conf like this :
ConfigFactory.load().getString("my.var");
Solution 5
In the play java is:
import play.Play;
...
Play.application().configuration().getString("key")
Nitzan Tomer
fugazi.io fugazi.io is a web based terminal application for executing local and remote commands which uses user-defined commands syntax. I've been working on it for a while now as a side project, and we're looking for help and feedback, so feel free to contact us (terminal.fugazi.io AT gmail.com). Check out the github repo. Also: software engineer web developer geek aikidoka kendoka yogi juggler (retired) skateboarder (occasional) snowboarder (24/7) smart-ass
Updated on July 08, 2022Comments
-
Nitzan Tomer almost 2 years
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.
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")
But it ends with a NullPointerException.
Am I wrong in assuming that there's a global Configuration instance that I can use? Thanks.
-
Nitzan Tomer almost 12 yearsThanks, I'm using java though and not scala
-
Cyril N. almost 12 years
Play.application()
doesn't exist anymore :/ -
ndeverge almost 12 yearsBe careful with the import, do you use
play.Play
and notplay.api.Play
? -
nightograph almost 12 yearsas Cyril N mentioned Play.application() does not exist!
-
Nasir almost 12 years@nightograph can you elaborate? I just created a brand new 2.0 java project, added the following lines 'String key = Play.application().configuration().getString("key"); return ok(index.render(key));' and added a key in conf file. I get the text value in the conf file.
-
biesior almost 12 yearsmy
Play.application().configuration().getString("key")
is still available too. @nightograph and Cyril, could you explain ? -
nightograph almost 12 years@MarcusbiesiorBiesioroff It was my mistake, I had play.api.Play imported instead of play.Play! sorry for the confusion
-
biesior almost 12 years@nightograph, that's good news I was afraid that's something worse :) be careful, every class starting with
play.api
is for using in Scala controllers. -
nightograph almost 12 years@MarcusbiesiorBiesioroff ohh!!! that's great tip Marcus. thanks so much! I was so confused why the framework would have 2 Play objects. Better naming would had been nicer Play.scala and play.java for example
-
Nasir almost 12 yearsI modified the answer, so hopefully, it will not cause confusion in the future :) - Kudos to nico_ekito who clarified it first
-
cdmckay over 10 yearsWhy can't you use
current
? -
Jeroen Kransen about 10 years
Play.current.configuration.getString("your.key")
seems to be the way in Play 2.2.2 -
vdshb almost 10 yearsYep! This variant is way better than others. It works in filters too (before Global object initialized).
-
redent84 over 9 yearsBeware that this will load the default configuration file, but if you loaded other files afterwards (for example, application.dev.conf) in Global, it won't be taken into account. Found out the hard way :(
-
gabby over 9 yearsi was looking for that +1
-
Ihor Patychenko about 8 yearsfor exactly what I was looking for +1
-
Lasitha Yapa over 7 yearsPlay.application() is now deprecated. So update to the answer would be nice.
-
Nasir over 7 years@LasithaYapa I would check i.am.michiel's reply below and see if it helps. I haven't used Play in years now.
-
Cid over 7 yearsIf you are using latest version this may not work. Check Saeed's response
-
pcejrowski about 7 yearsThis approach is deprecated, use dependency injection instead.
-
Karlth almost 7 yearsStill works in 2.5. I have no idea why a single config read suddenly needs dependency injection. :/
-
Spenhouet over 6 yearsSince Play 2.6.0 Configuration is deprecated and you need to inject com.typesafe.config.Config instead.
-
Spenhouet over 6 yearsSince 2.6.0 com.typesafe.config.Config should be used via dependency injection.
-
user3504158 about 5 yearsStarting from version 2.5 this approach is deprecated. Please use play.Application class which should be injected and then application.config().getString("your.property.here")