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=5Keys 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=21Activate it with spring.profiles.active=prod, for example as an environment variable SPRING_PROFILES_ACTIVE=prod.
The shortcut
Picture the sources as a stack, lowest priority at the bottom. When the same key appears twice, the one higher in the stack wins:
- Command-line arguments such as
--server.port=9000(top) - Environment variables such as
LIBRARY_LOAN_MAXDAYS=28 - Profile files such as
application-prod.properties application.propertiesinside the jar (bottom)
The full list has more layers, but these four answer most real questions. Environment variables use upper case with underscores in place of dots, and dashes simply disappear.
Start at the bottom
application.propertiessayslibrary.loan.max-days=14.
The classic trap: editing application.properties, redeploying, and seeing no change, because an environment variable on the server has been overriding that key all along.
Secrets such as database passwords belong in environment variables or a secrets store, never in a file committed to git.
Drills
Answer fast — trust the rule.
Which file holds settings that apply only when the test profile is active?
application.properties sets server.port=8080 and the app is started with --server.port=9000. Which port is used?
Which environment variable overrides library.loan.max-books?
A LoanProperties record has a field maxDays. Which property key binds to it with prefix library.loan?
Remember
- Defaults live in the file; the environment overrides; the closest source wins.
- Stack from top: command line, environment variables, profile files,
application.properties. - Trap: a forgotten environment variable silently beating your file edit.
