Skip to the workspace
Pebellslearn by building
Meet Spring  ›  Capstone Project

Configuration — Properties, Profiles and Overrides

Configuration 14 minSettings live in application properties files, profiles add environment-specific files, and higher-priority sources such as environment variables and command-line arguments override them.

spring boot / Configuration — Properties, Profiles and Overrides

Explore each scene at your own pace.

01 · The rule

The rule

Configuration — Properties, Profiles and Overrides

library-service lends books for 14 days on your laptop, but the head librarian wants 21 days in production, and the database password must never sit in the code. Spring Boot's configuration system lets the same jar behave differently in each place without being rebuilt.

The rule

Defaults live in the file; the environment overrides; the closest source wins.

Settings live in src/main/resources/application.properties (or the YAML form, application.yml):

server.port=8080
spring.application.name=library-service
library.loan.max-days=14
library.loan.max-books=5

Keys starting with server. or spring. are read by Boot itself. Keys like library.loan.* are your own, and you bind them to a typed class:

@ConfigurationProperties(prefix = "library.loan")
public record LoanProperties(int maxDays, int maxBooks) {}

Register it with @ConfigurationPropertiesScan (or @EnableConfigurationProperties(LoanProperties.class)) and inject LoanProperties wherever it is needed. Note the relaxed binding: max-days in the file becomes maxDays in Java. For a single value you can also write @Value("${library.loan.max-days}"), but a typed class keeps related settings together.

Profiles group settings for one environment. A file named application-prod.properties is loaded only when the prod profile is active, and its values override the plain file:

# application-prod.properties
library.loan.max-days=21

Activate it with spring.profiles.active=prod, for example as an environment variable SPRING_PROFILES_ACTIVE=prod.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.