Table of contents
1.
Introduction
2.
Configuration File
3.
Specifying an Alternative Configuration File
4.
Configuration Availability
4.1.
Using from your Controller
4.2.
Using with Akka
4.3.
Using with the run Command
5.
Frequently Asked Questions
5.1.
What is the use of the play framework?
5.2.
What is debugging?
5.3.
What is MVC?
5.4.
What is HTTP?
5.5.
What does SBT stand for?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Configuration File Syntax and Features

Author Rajat Agrawal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Play Framework is a free, open-source web application based on the model-view-controller (MVC) architectural paradigm. It was created in Scala and is compatible with Java and other programming languages compiled to JVM bytecode. It is built on Akka and delivers predictable and low resource usage (CPU, memory, threads) for highly scalable applications.

Play

In this blog, we will learn about Configuration file syntax and features in the Play framework.

Configuration File

The configuration file of a Play application must be defined in conf/application.conf.

The application.conf file is just one configuration source; there are a few others.

  • Any reference.conf files located on the classpath are loaded by default. A reference.conf file with default settings is typically included with Play JARs. Settings in the application.conf will override settings in reference.conf files.
  • System properties may be used to set settings as well. System properties override application.conf settings.

The config should be used idiomatically by having all configuration keys defined in reference.conf or application.conf, respectively. The key is often null to denote "no value" if it lacks an appropriate default value.

Specifying an Alternative Configuration File

The classpath is used to load the default application.conf during runtime. To force an alternative config source, utilize the following system properties:

  • config.resource defines a resource name that includes the extension, such as application.conf, rather than just application.
  • config.file gives a filesystem path, which should, once again, include the extension and not be a basename.

These system attributes define a substitute for application.conf rather than an addition to it. If you still wish to utilize some of the variables from the application.conf file, include it in your other .conf file by including "application" at the start of that file. After including the application.conf settings in your new.conf file, you may specify any settings you want to override.

Configuration Availability

Configuration can be available in different ways, some of which are mentioned below.

Configuration Availability

Using from your Controller

Due to Dependency Injection, your controller (or component) will have access to the configuration, allowing you to utilize either the default settings or a custom one (in Scala or Java).

package controllers

import com.typesafe.config.Config;
import play.mvc.Controller;

import javax.inject.Inject;

public class MyController extends Controller {

  private final Config config;

  @Inject
  public MyController(Config config) {
    this.config = config;
  }
}
You can also try this code with Online Java Compiler
Run Code

Using with Akka

The configuration file specified for your Play application will be used by Akka. Indicating that anything in Akka can be configured in the application.conf file. Instead of reading its settings from the Akka setting, Akka in Play reads them from the play.akka setting.

Using with the run Command

When using the run command to launch your program, there are a few configuration-specific considerations to keep in mind.

  • Additional settings for the run command can be configured in your build.sbt. When your application is deployed, these settings won't be applied.
PlayKeys.devSettings += "play.server.http.port" -> "8080"
You can also try this code with Online Java Compiler
Run Code
  • In run mode, Play's HTTP server component launches before the compilation of the program. This implies that it cannot read the application when the HTTP server begins.conf file. The application.conf file cannot be used to modify HTTP server settings when using the run command. In its place, you must either utilize system properties or the stated devSettings option.
> run -Dhttp.port=1234
You can also try this code with Online Java Compiler
Run Code


As you can see from these server settings, if a port or address is not provided earlier, such as through PlayKeys.devSettings, it will fall back to the config values PLAY_HTTP_PORTPLAY_HTTPS_PORT, and PLAY_HTTP_ADDRESS. Since these configuration keys are replacements, you may also create them using environment variables. For example, while using Bash under Linux:

export PLAY_HTTP_PORT=9001
export PLAY_HTTPS_PORT=9002
export PLAY_HTTP_ADDRESS=127.0.0.1
You can also try this code with Online Java Compiler
Run Code


Additionally, if you need to alter the settings of Akka for development mode, there is a dedicated namespace (the mode used with run command). Prefixing your setup with play.akka.dev-mode in PlayKeys.devSettings is required, as in:

PlayKeys.devSettings += "play.akka.dev-mode.akka.cluster.log-info" -> "off"
You can also try this code with Online Java Compiler
Run Code


This is especially helpful if the Akka ActorSystem used in development mode and the ActorSystem utilized by the application itself clash in some way.

Frequently Asked Questions

What is the use of the play framework?

Play Framework is a free, open-source web application based on the model-view-controller (MVC) architectural paradigm. It is built on Akka and delivers predictable and low resource usage (CPU, memory, threads) for highly scalable applications.

What is debugging?

It is the process of identifying and removing computer hardware or software errors.

What is MVC?

MVC is an architectural paradigm that divides an application into three basic logical components: the model, the view, and the controller.

What is HTTP?

HTTP stands for Hypertext Transfer Protocol is an application-layer protocol for transmitting hypermedia documents, such as HTML. It was designed for communication between web browsers and web servers.

What does SBT stand for?

SBT stands for System Build Tools.

Conclusion

In this article, we have extensively discussed Configuration files syntax and features in the play framework. If you want to learn more, check out our articles on What Is Web2Py?What is Sinatra?Why To Use Web2py?Postbacks and Internationalization in web2pyThird Party Modules In Web2pyTasks In Web2py, and  XML in Web2py.

Do upvote our blog to help other ninjas grow.

Happy Coding!

Thank You
Live masterclass