Do you think IIT Guwahati certified course can help you in your career?
No
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.
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 inconf/application.conf.
Theapplication.conffile is just one configuration source; there are a few others.
Any reference.conffiles located on the classpath are loaded by default. A reference.conf file with default settings is typically included with Play JARs. Settings in theapplication.confwill 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.
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
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.
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
As you can see from these server settings, if a port or address is not provided earlier, such as throughPlayKeys.devSettings, it will fall back to the config values PLAY_HTTP_PORT, PLAY_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:
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-modein PlayKeys.devSettings is required, as in:
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.