QuickKnol: Reading Configuration Files in Scala

There are several ways of reading configuration files in Scala including the java properties way with the deprecated Configgy and quite some more. One way which is easy and comes in handy is with the Typesafe Config project which is also used in Akka.

Let us quickly see how to set it up,

You would need to include the following dependency in your build.sbt or Build.scala.

libraryDependencies += "org.skife.com.typesafe.config" % "typesafe-config" % "0.3.0"

Once you have this, setting up the logger is straight forward. The snippet in our case is

 /** Loads all key/value pairs from the application configuration file. */
 var conf = ConfigFactory.load

  /** Seed locations for which URLs are valid, example ca, nj etc*/
  lazy val seedLocations = conf.getString("supplier.yelp.seed.locations")

The ConfigFactory.load by default looks for the following files in order

*system properties
*application.conf (all resources on classpath with this name)
*application.json (all resources on classpath with this name)
*application.properties (all resources on classpath with this name)
*reference.conf (all resources on classpath with this name)

You could also load it with a different file using ConfigFactory.load(file-name), like when you would want to execute tests.

and the configuration file would look like this

# these are the valid URLs for a given supplier site
supplier {
    yelp {
    help {


As you would notice, we can access the subsections. Let us see it through a test

import com.typesafe.config.ConfigFactory
import org.junit.runner.RunWith
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner

 * Checks that properties mentioned as a part of application.conf
 * are read properly.
class ConfigurationTester extends FunSuite {

  test("check that supplier values are being read properly") {
    val conf = ConfigFactory.load("test-application.conf")
    assert(true === conf.hasPath("supplier.yelp.seed.pages"))
    assert(true === conf.hasPath("supplier.yelp.seed.locations"))
    assert(false === conf.hasPath("supplier.yelp.event.list.pages"))
    assert(true === conf.hasPath("supplier.help.seed.pages"))
    assert(true === conf.hasPath("supplier.help.seed.locations"))

More details on the project here


About Vikas Hazrati

Vikas is the Founding Partner @ Knoldus which is a group of software industry veterans who have joined hands to add value to the art of software development. Knoldus does niche Reactive and Big Data product development on Scala, Spark and Functional Java. Knoldus has a strong focus on software craftsmanship which ensures high-quality software development. It partners with the best in the industry like Lightbend (Scala Ecosystem), Databricks (Spark Ecosystem), Confluent (Kafka) and Datastax (Cassandra). To know more, send a mail to hello@knoldus.com or visit www.knoldus.com
This entry was posted in Scala and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s