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.

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

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

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

More details on the project here

Written by 

Vikas is the CEO and Co-Founder of Knoldus Inc. 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). Vikas has been working in the cutting edge tech industry for 20+ years. He was an ardent fan of Java with multiple high load enterprise systems to boast of till he met Scala. His current passions include utilizing the power of Scala, Akka and Play to make Reactive and Big Data systems for niche startups and enterprises who would like to change the way software is developed. To know more, send a mail to hello@knoldus.com or visit www.knoldus.com

Leave a Reply

%d bloggers like this: