

Kumar Pallav
Configuration and Properties : Spring Boot : Part 2 – Externalizing Configuration
• Kumar Pallav
Externalizing Configuration
Spring boot allows you to externalize your configuration properties. Above we saw, one way to do so using application.properties. There are various way to tell your application about properties required by your application i.e. it is not only limited to defining in application.properties .
Below are the ways you can define your properties. It is in order of preference which means , if same property is defined via two different ways , the one with higher precedence will be taken and the one with lower precedence value will be ignored.
- 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).
The above list is taken from spring documentation present at https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
Tough it is good to know that there are several ways to define the properties which will be used for our application , it is hardly a chance that you will use all of them in our application . We will normally use them on need basis.
In order to verify the order of precedence , we can test it. In above list the number 4th (command line) and 15th (application.properties inside jar) are marked as bold. As per precedence order command line takes higher precedence over application.properties inside our jar file.
We already have application.properties with server.port =8081 , let’s add a command line argument of same before starting application and verify if our server starts at different port.
Currently our server starts at 8081 , say we want application to pick 8082 and ignore the value provided by application.properties (or application.yml) . We can provide command line arugument directly by invoking jar file made by build with command parameter like
java -jar user-service-0.0.1-SNAPSHOT.jar --server.port=8082
Since we are developing via an IDE , we can use IDE to add it for us , in STS or Eclipse you can right click on project , go to Run As -> Run Configuration
Inside Run Configuration , open Arguments tab and add –server.port =8082 as shown below.
You can now see our server now starting at 8082.
Now since we have already externalised configuration , we would see some other popular usage of configuration.