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:
- Devtools global settings properties on your home directory (
~/.spring-boot-devtools.properties
when devtools is active). @TestPropertySource
annotations on your tests.@SpringBootTest#properties
annotation attribute on your tests.- Command line arguments.
- Properties from
SPRING_APPLICATION_JSON
(inline JSON embedded in an environment variable or system property) - ServletConfig init parameters.
- ServletContext init parameters.
- JNDI attributes from java:comp/env.
- Java System properties (
System.getProperties()
). - OS environment variables.
- A RandomValuePropertySource that only has properties in
random.*
. - Profile-specific application properties outside of your packaged jar (
application-{profile}.properties
and YAML variants) - Profile-specific application properties packaged inside your jar (
application-{profile}.properties
and YAML variants) - Application properties outside of your packaged jar (
application.properties
and YAML variants). - Application properties packaged inside your jar (
application.properties
and YAML variants). @PropertySource
annotations on your@Configuration
classes.- Default properties (specified using
SpringApplication.setDefaultProperties
).
One thing came to our attention here. It mentions “Java System properties” as one of the PropertySource
s 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 PropertySource
s 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.