How to use the Lightbend Config library in a Scala or Java application

Scala problem: You want to be able to read configuration files that are written in the Lightbend “Config” file format.

Solution

Lightbend — initially named Typesafe — created a configuration file format named HOCON, which stands for, “Human-Optimized Config Object Notation.” As an example, a small HOCON configuration file looks like this:

jdbc {
    driver  = "com.mysql.jdbc.Driver"
    url = "jdbc:mysql://127.0.0.1:8889/kbhr_geo"
    username = "root"
    password = "root"
}

Assuming that you’re using SBT, name that file lightbend.conf, and place it in your project’s src/main/resources directory. Then, to read that file from Scala, add the Lightbend config project dependency to your build.sbt file:

libraryDependencies ++= Seq(
    "com.typesafe" % "config" % "1.4.0"
)

Then you can read that configuration file from your Scala code like this:

import com.typesafe.config.{Config, ConfigFactory}

object ReadConfigurationFile extends App {

    val config: Config = ConfigFactory.load("lightbend.conf")

    val driver = config.getString("jdbc.driver")
    val url = config.getString("jdbc.url")
    val username = config.getString("jdbc.username")
    val password = config.getString("jdbc.password")

    println(s"driver =   $driver")
    println(s"url =      $url")
    println(s"username = $username")
    println(s"password = $password")

}

That code results in this output:

driver =   com.mysql.jdbc.Driver
url =      jdbc:mysql://127.0.0.1:8889/kbhr_geo
username = root
password = root

Notice how the string "jdbc.driver" in your Scala source code corresponds to the driver parameter inside the jdbc “object” in the configuration file:

jdbc {
    driver = "com.mysql.jdbc.Driver"
    ...
}

With syntax like this, the configuration file is easily created and read by humans.

Discussion

Lightbend (nee Typesafe) is a company that was co-founded by Martin Odersky, the creator of Scala, and Jonas Bonér, creator of Akka. The company created the HOCON configuration file format that is used in the Akka middleware libraries and the Play Framework, and you can also use it in your own projects by following this recipe.

A main benefit of this file format is that it’s “human optimized,” meaning that it’s easy for humans to read. For more details on the many configuration options, see the Lightbend config project, which is well documented.

No configuration setting found...

If you know how to work with Scala and SBT, that code should work just fine out of the box. Note that if you get an error message like this when running your application:

No configuration setting found for key ...

it probably means:

  • Your Lightbend Config configuration file can’t be found
  • If you change any of the name of the configuration file parameters, it can also mean that the name in the configuration file doesn’t match the name in your Scala/Java source code

In summary, if you needed a little example of how to use the Lightbend Config configuration file library in your Scala/Java project, I hope this example is helpful.

... this post is sponsored by my books ...