Spring Boot : override properties – Example

If you want to see how to override properties with an external properties file, this page is for you

This example is a part of  Spring Boot with all examples

Full Code with Spring Boot 2.2.2.RELEASE:

16_spring-boot-override-property.zip

What you need for run this:

  • Maven 3
  • Java 8

What the code will do:

  • Show how use external properties file

Explain:

This is the file structure:

In the application.properties, it has these lines:

server.servlet.context-path=/test
server.port=8081

Assume that we have build the jar file and want to change the port when deploy on production to 8888. So, we create the override.properties like this:

server.port=8888

Try:

run this in console:

mvn clean package

Then go to the target folder and run this command:

java -jar spring-boot-16-1.0-SNAPSHOT.jar

Then try to access this url:

http://localhost:8081/test/api/count

Now, you are using only the application.properties.

Next, we are going to use both application.properties and override.properties.

Stop the process and run this command:

java -jar spring-boot-16-1.0-SNAPSHOT.jar --spring.config.location=classpath:application.properties,../override.properties

Then try to access this url:

http://localhost:8888/test/api/count