Table of contents
1.
Introduction
2.
The Scala Configuration API
2.1.
Accessing the Configuration
2.2.
ConfigLoader
2.3.
Optional Configuration Keys
3.
Frequently Asked Questions.
3.1.
Why is the Play framework easy?
3.2.
How does the play framework work?
3.3.
What is Scala?
3.4.
Is the Play framework still used?
3.5.
What is a Hocon file?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

Scala Developers - The Configuration API

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, we will discuss The Scala Configuration API but before discussing The Scala Configuration API, let's know about the Play Framework.

Play is an open-source web application framework created by Guillaume Bort that follows the model–view–controller architectural pattern. Play was written in Scala and usable from other programming languages, e.g., Java. Play uses the Typesafe config library and provides a nice Scala wrapper called Configuration with more advanced Scala features.

The main aim of Play is to optimize the developer productivity by using convention over Configuration and display of errors in the browser. Play is heavily inspired by ASP.NET MVC, Ruby on Rails, and Django and is similar to this family of frameworks. 

The Configuration API Image

Now coming to the topic "The Scala Configuration API."

The Scala Configuration API

If you're unfamiliar with Typesafe config, don't worry; below, we will discuss the configuration file syntax and features.

We know that the configuration file used by the Play is based on the Typesafe config library.

Also, the configuration file of a Play application must be defined in conf/application.conf and uses the HOCON format.

The natural way to use Config is to have all configuration keys defined in reference.conf or application.conf. And if the key does not have a reasonable default value, it is set to null, which signifies "no value."

Let's discuss how we access the Configuration.

Accessing the Configuration

Accessing the Configuration Image

We will obtain a Configuration object through Dependency Injection (Dependency injection is a design pattern that helps separate our components' behavior from dependency resolution) or by passing an instance of Configuration to our component. 

class CodingNinjas @Inject() (config: Configuration, c: ControllerComponents) extends AbstractController(c){
  def getFoo = Action{
    Ok(config.get[String]("data"))
  }
}
You can also try this code with Online Java Compiler
Run Code

 

The get method above is the most common method to get a single value in the configuration file.

// data = codingninjas
config.get[String]("foo")

// value = 8
config.get[Int]("value")

// bool = true
config.get[Boolean]("bool")

// listOfData = ["value", "bool"]
config.get[Seq[String]]("listOfData")
You can also try this code with Online Java Compiler
Run Code

 

The Configuration also supports validating against a set of valid values.

config.getAndValidate[String]("data", Set("value", "bool"))

The most common types of implicit ConfigLoader that it accept are String, Int, and even Seq[String].


Now the question that comes to your mind What is ConfigLoader?

ConfigLoader

ConfigLoader Image

You can define your ConfigLoader or easily convert Configuration into a custom type. It is a great way to bring more type safety to your configuration use, and also this is used extensively in Play internally.

case class CodingNinjas(title: String, baseUri: URI)
object CodingNinjas{
  implicit val configLoader: ConfigLoader[CodingNinjas] = new ConfigLoader[CodingNinjas]{
    def load(rootConfig: Config, path: String): CodingNinjas ={
      val config = rootConfig.getConfig(path)
      CodingNinjas(title = config.getString("title"), baseUri = new URI(config.getString("baseUri")))
    }
  }
}
You can also try this code with Online Java Compiler
Run Code


Then you can use Config.get as we did above:

config.get[CodingNinjas]("app.config")
You can also try this code with Online Java Compiler
Run Code

Now let's discuss some optional configuration keys in The Scala Configuration API.

Optional Configuration Keys

Optional Configuration Keys Image

Play's Configuration uses the getOptional[A] method to support optional configuration keys. It works like get[A], but it will return None if the key does not exist. 

We recommend setting optional keys to null in your configuration file and using get[Option[A]] instead of the above method. Still, we provide this method for convenience in case you need to interface with libraries that use a configuration in a non-standard way.

Till now, we have briefly discussed The Scala Configuration API. So, below are some FAQs related to The Scala Configuration API.

Frequently Asked Questions.

Why is the Play framework easy?

Since, Play is based on a lightweight, stateless, web-friendly architecture. Therefore, Play Framework makes it easy to build web applications with Java & Scala. 

How does the play framework work?

Play framework is a web framework that breaks away from the Servlet Specification for the JVM. Play embraces an entirely reactive programming model by using futures for asynchronous programming, work stealing for maximizing available threads, and Akka for distributing work.

What is Scala?

Scala combines object-oriented and functional programming in a high-level language. Its static types help avoid bugs in complex applications. And its JVM and JavaScript runtimes let you build high-performance systems with easy access to huge ecosystems of libraries.

Is the Play framework still used?

Yes, Play is used monthly by hundreds of thousands of Java and Scala developers. It is still highly relevant to today's application and web development and has a passionate and capable community, ensuring that it has many good years left.

What is a Hocon file?

HOCON stands for Human-Optimized Config Object Notation and is an easy-to-use configuration format typically used in suffix. It is used by Sponge and individual plugins utilizing SpongeAPI to store important data, such as configuration or player data. 

Conclusion

This article discusses The Scala Configuration API and how to access the Configuration, ConfigLoader, and Optional Configurational Keys.

After reading about The Scala Configuration API, are you not feeling excited to read/explore more articles on Data Structures and Algorithms? Don't worry; Coding Ninjas has you covered. See What is ScalaScala Programming LanguageAPI ReferenceRest APIBasics of Java, and Web Technologies to learn.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass