Custom configuration in Play Framework

Greg Methvin
Keep It Up
Published in
1 min readOct 10, 2013

In our Play! applications, we often want to be able to enable or disable features for specific environments. We decided to take a simple approach to this: a config file on the filesystem. Play provides the onLoadConfig method in the global which you can override to modify the config before your application starts.

Here’s an example of how we use it to customize configs for certain development and testing environments:

This just adds it to the application’s config, overriding the default values. If the config file doesn’t exist, it adds an empty config. Since Play’s Configuration wraps the Typesafe Config library, we use ConfigFactory to load the config, then wrap that in a Configuration.

You can now read the new config values as usual in your application code:

In addition to loading from a file, you can see how you could use the same strategy to load a configuration from Amazon S3 or another external source. You can use ConfigFactory.parseString to parse any string as a configuration.

We wrote this post while working on Kifi — tools that help people outsmart information overload together. Learn more.

Originally published at eng.kifi.com on October 10, 2013.

--

--