Configuration and Properties : Spring Boot : Part 2 Externalising Configuration

Subscribe to my newsletter and never miss my upcoming articles

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.

  1. Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).
  2. @TestPropertySource annotations on your tests.
  3. @SpringBootTest#properties annotation attribute on your tests.
  4. Command line arguments.
  5. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property)
  6. ServletConfig init parameters.
  7. ServletContext init parameters.
  8. JNDI attributes from java:comp/env.
  9. Java System properties (System.getProperties()).
    1. OS environment variables.
    2. A RandomValuePropertySource that only has properties in random.*.
    3. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants)
    4. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants)
    5. Application properties outside of your packaged jar (application.properties and YAML variants).
    6. Application properties packaged inside your jar (application.properties and YAML variants).
    7. @PropertySource annotations on your @Configuration classes.
    8. 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.

 
Share this