Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

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:

  1. Command-line arguments such as --server.port=9000 (top)
  2. Environment variables such as LIBRARY_LOAN_MAXDAYS=28
  3. Profile files such as application-prod.properties
  4. application.properties inside 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.

What is max-days in production?Step 1 of 4
  1. Start at the bottom

    application.properties says library.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.

quick drill

Which file holds settings that apply only when the test profile is active?

quick drill

application.properties sets server.port=8080 and the app is started with --server.port=9000. Which port is used?

quick drill

Which environment variable overrides library.loan.max-books?

quick drill

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.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhich value wins?

application.properties sets library.loan.max-books=5, application-prod.properties sets it to 8, and the server has LIBRARY_LOAN_MAXBOOKS=10 in its environment. The prod profile is active and no command-line arguments are passed. What value does the app use, and how would you make it 12 for a single run without editing any file?