Set default values to application properties in spring boot

In this tutorial, we will see how to set the default values to the application properties in spring boot.

The application.yml in the spring boot is used to define all the properties that will be used with the application.

There are many times scenarios we want to set the default values to the properties if they are not set in the environment variables.

For example, let’s say we have this property to define the PORT at which the application will run,

server:
  port: ${SPRING_APP_PORT}

Now if the environment variable SPRING_APP_PORT is not defined, we want to have a default value, for that case, wherever in your code you are accessing the value using @Value annotation use the colon : operator and set the default value.

@Value("${server.port:8080}")
private int port;

This will use the default value 8080 if the port value is null.