Sai
Sai

Reputation: 1117

How to use property file in play framework in java?

I am new to play framework. I want to know how to use the property file in play framework.

My property file is,

conf/test.properties
name=kumar
framework=playframework

so now i want to use test.properties inside my controller class (Application.java).

Please let me know what are the steps i need to do.

Upvotes: 4

Views: 3218

Answers (2)

thor-tech
thor-tech

Reputation: 62

you can access to property files in play framework by doing:

val ConfigLoader = play.Play.application.configuration
  implicit val connector = ContactPoints(Seq(ConfigLoader.getString("cassandra.host"))).keySpace(ConfigLoader.getString("cassandra.keyspace"))

Upvotes: -1

jsonmurphy
jsonmurphy

Reputation: 1600

All files placed in the conf/ folder are automatically added to your classpath. You should be able to access the your file like this:

Properties prop;
try {
  prop = new Properties();
  InputStream is = this.getClass().getResourceAsStream("test.properties");
  prop.load(is);
} catch (FileNotFoundException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}

String name = prop.getProperty("name");
String framework = prop.getProperty("playframework");

Note: I haven't tested any of the above.

Update:

I've just realized that this question is a close duplicate of Load file from '/conf' directory on Cloudbees but since my solution also includes how to access the properties in the file, I'll leave it as is.

Also since your controller method will most likely be static the above might fail to compile. In the answer I referenced they suggested using the facility provided by Play!:

Play.application().resourceAsStream("test.properties")

Upvotes: 4

Related Questions