Using TypeSafe’s Config In Scala
Our application behavior is generally defined with a set of configurations. Instead of hard-coding the values in the code, we like to define them separately in a configuration store. In this post, we are going to see how we can define configurations using TypeSafe’s config.
Let’s create a SBT project. We need to specify the SBT and Scala version we intend to use. You can leave it as default if you are not sure.
In order to use TypeSafe’s config, we need to add it as SBT dependency. Let’s modify build.sbt as follows:
Typesafe config allows us to define configuration files in a number of formats. Here we are using JSON. Yes, it supports the hierarchical configurations. These configurations can be accessed using DOT notation. For using the default behavior, we are saving the file in resources folder. We are naming the file as application.json.
ConfigFactory is used to load all application configuration. Since we are using the default behavior, we can use the default load method. It returns a Config object. Just look at how it provides methods to provide configuration value for various primitive types including Boolean, Int and String. So we don’t need to load it as a string and parse it to our specific data type.
As you can notice, it loads and merges all configurations in our application.