Sub topic 1
Why Spring Boot externalizes configuration
Externalized configuration means keeping settings such as hostnames, ports, and feature flags outside your compiled code. Spring Boot reads them at startup from files, environment variables, and command-line arguments. This chapter explains the problem it solves and the model the rest of the tutorial builds on.
Prerequisites
This chapter assumes you have:
- Basic Java, including classes and records.
- A simple Spring Boot application built with Maven or Gradle.
- Familiarity with Spring beans and dependency injection.
- Some ability to read YAML or
.propertiessyntax.
The problem with hard-coded settings
Suppose a mail sender has the host localhost written into a Java class. That works on your laptop. In production you need smtp.example.com, so you edit the code, rebuild, and redeploy. Every environment (development, test, production) needs different values, so you would end up with one build per environment.
Three problems follow:
- Rebuilds for every change. A port change should not need a compile.
- Different artifacts per environment. The jar you tested is not the jar you ship.
- Secrets in source control. Passwords written in code end up in version history.
Externalized configuration fixes this. You build one jar and supply the environment-specific values when you run it.
How Spring Boot does it
Spring Boot gathers values from several property sources, each a named collection of key-value pairs. Examples include application.yaml inside the jar, environment variables, and command-line arguments. All sources are merged into one view. If the same key appears in more than one source, the source with higher precedence wins.
Your code does not care where a value came from. It asks the Environment or receives a bound value, and Spring Boot resolves the winner.
A first example
A default configuration file sets values for local development:
app:
mail:
host: localhost
port: 1025Running the same jar in production needs no rebuild. You override one value at launch:
java -jar app.jar --app.mail.port=2525The command-line argument has higher precedence than the file packaged in the jar, so the port becomes 2525. The host stays localhost because nothing overrides it.
What you will build on
The remaining chapters follow this model:
- File formats for the defaults.
- The precedence order of the sources.
- Binding values to typed classes and validating them.
- Profiles, which switch groups of settings and beans per environment.
- Testing and inspecting the result.
Keep one rule in mind: code describes behavior, and configuration describes the environment.
Exercise
A teammate says, "To point the app at the production mail server, we should change the host in MailService.java and rebuild." Give two reasons externalized configuration is a better approach.
Show answer
Any two of these: the same tested jar can run in every environment instead of building one per environment; changing a value needs no recompile or redeploy of code; environment-specific values and secrets stay out of source code and version history; values can be overridden at launch with files, environment variables, or command-line arguments.