Quick Tip - Spring Boot Placeholders

This article shows a quick tip concerning Spring Boot *.properties files. As you know, ResourceBundle files are one of the ways besides YAML files in Spring Boot to configure applications.

As we are children of the past, we use *.properties files in our current projects. Lately, we came across an issue with Hibernate Search: we wanted to configure one property, namely spring.jpa.properties.hibernate.search.default.indexBase with a temporary path.

Goal was to define the index-base using a temporary directory as the default-setting, to keep set up work for new developers relatively small and provide a convenient bootstrapping process for new developers.

Externalized Configuration

There is a section in the Spring Boot documentation I must have a looked up a couple of times and which is very much worth noting. It is called Externalized Configuration and describes the way how Spring Boot resolves configuration properties, using Spring’s PropertySource mechanism (JavaDoc).

This is the order in which a property is looked up:

One thing came to our attention here. It mentions “Java System properties” as one of the PropertySources to be resolved against. The available standard system property values are defined in the System#getProperties JavaDoc. As Spring Boot resolves property values against the System.getProperties() that means you can use all the mentioned property values from there in our *.properties files.

Using java.io.tmpdir

One of the system property values is java.io.tmpdir. It is defined to return the default temp file path. As those system properties are available via the PropertySource mechanism, we can simply use the temporary directory like a variable in our properties file:

spring.jpa.properties.hibernate.search.default.indexBase=${java.io.tmpdir}/lucene-index/

${java.io.tmpdir} will create a lucene-index directory in the Java temporary file path, which is exactly what we tried to achieve.

Keep in mind, the list of PropertySources from above is considering many more sources then System.getProperties() only. When working on Spring Boot applications, it’s a good hint to internalize the available configuration sources.

Summary

In this article we showed how to use the standard Java temporary path found in the System property java.io.tmpdir in Spring Boot *.properties files.