Configuration

Configuration #

Configuration of the application is done via HOCON configuration files in the src/main/resources and src/test/resources directories for production code and test code respectively.

Reference #

Below is the reference configuration file that sets the defaults for the framework.

# Namespace for all application configuration
app {
  # Set the application runtime environment (development, production or <custom>)
  env = "development"
  # The default date format used throughout the application.
  dateFormat = "yyyy-MM-dd"
  # Any other Guice modules
  modules = []
  # Static assets
  http {
    # Set the HTTP port
    port = 8080
    # Set the max body size
    maxBodySize = "10MB"
    # Set the file upload directory
    fileUploadsDir = "file-uploads"
  }
}

Providing your own configuration #

In the starter project, you can provide your own configuration in the src/main/resources/application.conf file.

Overriding configuration properties #

According to the official documentation,

Configuration properties are read from the following sources (in order of priority):

  1. System properties (-D command line arguments)
  2. application.conf files on the classpath
  3. application.json files on the classpath
  4. application.properties files on the classpath
  5. reference.conf that ships with the library

Configuration values are overridden by redefining the same value in any of the places listed above. For example, to change the app.http.port value, you can set it as a system property on the command line:

java -Dapp.http.port=8080 -jar myapp.jar

Or you can declare it in your own application.conf file:

# src/main/resources/application.conf
app.http.port = 8080

Choosing a configuration file #

When you declare your configuration in application.conf, this file will be read automatically as per the rules of precedence outlined in the previous section. However, you may want to specify different settings based on the application’s runtime environment. To do this, you set the config.resource system property to the location of your configuration file.

For example, to instruct the framework to load the settings stored in src/main/resources/production.conf, use -DConfig.resource=production.conf when starting the application from the command line.

JSON Configuration #

To configure the JSON mapper, get a hold of the ObjectMapper singleton using Components.createInstance(). All components of the framework use this singleton, so any changes you apply to it will be applied globally.

Check out the FasterXML Documentation to learn about what options are available to you.

Accessing configuration at runtime #

The Settings class holds all of the configuration details in your *.conf files. This class can be injected into your domain objects using the @Inject annotation or by using Components.createInstance(Settings).

For more information about Components.createInstance() and the @Inject annotation, see dependency injection.

Next: HTTP Client