Blogpost

3 minute read

Configuration externalization and injection in Java applications

Why configuration externalization is a good idea and how to do this for your Java application.

These days, software development and the use of a decent Version Control System (VCS) like git go hand in hand.

Apart from the countless benefits of using a VCS, it is generally a good idea to exclude the configuration of an application from it. This is also called “configuration externalization” and eliminates a few problems:

  • sensitive data (eg database credentials) is not visible for those who can access your VCS repository
  • it’s not possible to accidentally or wrongfully update the configuration (eg database credentials) causing the application to malfunction
  • you don’t have to create and maintain a configuration file for each environment your application runs on
  • no new build needs to be created for even the smallest configuration change

One of our platform’s core fundamentals is to create and maintain application configurations as easy as drinking a glass of water. Simply go to your application detail page, click the “Config” tab and add/edit/delete your configuration variables. After clicking the “Save” and then the “Apply” button, these variables are exposed to your application through environment variables.

When you have a Spring Boot application and don’t externalize your configuration, you’ll have a properties file that contains your configuration. For example:

db.url=jdbc:mysql://127.0.0.1:3306/hello_world
db.username=hello
db.password=world

And you’ll most likely run your application with the command:

java -jar helloworld.jar

Luckily, “Spring Boot” supports configuration externalization out of the box! Either by placing configuration files outside the build artifact, or by specifying properties as command line arguments. And the real nice part is that this technique can be applied on existing applications without updating a single line of code! This is because the configuration keys in the existing application properties file(s) will be overridden by the ones that are passed on the command line.

Implementing this configuration externalization technique is quite simple on the platform. After creating your configuration keys and values in the interface, create a scenario to start your application. You’ll notice that all configuration keys are easily accessible by surrounding the key with double curly braces. In this example, we’ll append the database credentials individually.

#!/bin/bash
java -jar helloworld.jar --db.url="{{db_url}}" --db.username="{{db_username}}" --db.password="{{db_password}}"

If you don’t want to have your database credentials being visible in the process list, you can also create a scenario that generates a properties file and injects the configuration variables:

#!/bin/bash
CONFIG_FILENAME=application.properties

cat > {{appRoot}}/$CONFIG_FILENAME <<EOL
db.url={{db_url}}
db.username={{db_username}}
db.password={{db_password}}
EOL

You can then run your application with the following command:

java -jar helloworld.jar --spring.config.location=classpath:/$CONFIG_FILENAME

Only make sure the properties file is within your classpath ;)

Want to find out what we can do for you?