0
0
Spring Bootframework~5 mins

Configuration precedence order in Spring Boot

Choose your learning style9 modes available
Introduction

Configuration precedence order tells you which settings take priority when multiple sources provide values. It helps your app use the right settings without confusion.

You want to override default settings with environment-specific values.
You need to provide user-specific configuration that should override global settings.
You want to test changes locally without affecting production settings.
You want to understand why a certain configuration value is used in your app.
You want to organize configuration files and environment variables clearly.
Syntax
Spring Boot
Spring Boot loads configuration properties from multiple sources in a specific order. The order from highest to lowest priority is:

1. Command line arguments
2. Java System properties (System.getProperties())
3. OS environment variables
4. Application properties inside your packaged jar (application.properties or application.yml)
5. Default properties (specified programmatically)

You can also use profiles to load profile-specific properties like application-dev.properties.

The first source that provides a value for a property wins.

Command line arguments override everything else.

Examples
This sets server.port to 8081, overriding any other source.
Spring Boot
# Run app with command line argument overriding property
java -jar app.jar --server.port=8081
Environment variable SERVER_PORT sets the port to 8082 unless command line overrides it.
Spring Boot
export SERVER_PORT=8082
java -jar app.jar
This is the default port if no other source overrides it.
Spring Boot
application.properties:
server.port=8080
Sample Program

This Spring Boot app reads the server.port property and shows it via a web endpoint. You can test how different configuration sources affect the port value.

Spring Boot
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

@RestController
class PortController {
    @Value("${server.port:default}")
    private String port;

    @GetMapping("/port")
    public String getPort() {
        return "Server is running on port: " + port;
    }
}
OutputSuccess
Important Notes

Use --spring.config.location to specify custom config file locations.

Profile-specific files like application-dev.properties load only when that profile is active.

System properties and environment variables are useful for containerized apps.

Summary

Spring Boot loads configuration from many places in a clear priority order.

Command line arguments have the highest priority and override other sources.

Understanding this order helps you control app behavior easily.