Sub topic 6
Overriding with environment variables and command-line arguments
This chapter assumes you can build a Spring Boot app, read YAML, and know that Spring Boot binds properties to beans. You will learn how to change a value at launch time without editing any file in the jar.
Why override at launch
A jar is built once and runs in many places. The database host, mail server, and active profile differ between laptop, test, and production. Spring Boot lets the environment around the jar supply those values. Command-line arguments and environment variables rank above application.yaml inside the jar, so they win when the same key appears in both.
Three ways to override
# Activate a profile on the command line
java -jar app.jar --spring.profiles.active=prod
# Override a single property with a command-line argument
java -jar app.jar --app.mail.port=2525
# Override with environment variables (relaxed binding)
SPRING_PROFILES_ACTIVE=prod APP_MAIL_HOST=smtp.internal.example java -jar app.jarA command-line argument starts with -- and uses the property name as written, such as --app.mail.port=2525. Spring Boot reads these before any file.
Environment variables cannot contain dots or hyphens in most shells. Spring Boot uses relaxed binding to map them back to property names.
Naming rules for environment variables
- Replace each dot with an underscore.
- Replace each hyphen with nothing (remove it).
- Use uppercase.
| Property in YAML | Environment variable |
|---|---|
| `app.mail.host` | `APP_MAIL_HOST` |
| `app.mail.retry-attempts` | `APP_MAIL_RETRYATTEMPTS` |
| `spring.profiles.active` | `SPRING_PROFILES_ACTIVE` |
The hyphen rule is a frequent source of mistakes. APP_MAIL_RETRY_ATTEMPTS does not match app.mail.retry-attempts, because the extra underscore reads as another dot-level. Use APP_MAIL_RETRYATTEMPTS.
Which source wins
If you set both APP_MAIL_PORT=587 and --app.mail.port=2525, the port is 2525. The first source in the list that defines the key wins. Other keys are still read from the lower sources, so overrides only replace the keys you name.
Effect on bound properties
Overrides apply to @ConfigurationProperties classes like MailProperties without code changes. If app.mail.port is validated with @Min(1) @Max(65535), a bad override such as --app.mail.port=99999 stops startup with a binding error. This makes a mistyped launch value visible immediately.
Common errors
- The override seems ignored. Check the spelling. A mismatch is silent because an unknown key is simply an extra property that nothing reads.
- Arguments after the jar name. Put
--key=valueafter-jar app.jar. Placed before-jar, the JVM treats them as its own options. - Spaces in values. Quote the value in your shell, for example
--app.mail.sender="ops [email protected]". - Containers. Pass variables with the container runtime, then use the uppercase form shown above.
Exercise
Your YAML defines app.mail.retry-attempts: 3. Write the environment variable that sets it to 5, and say what happens if you also pass --app.mail.retry-attempts=7.
Show answer
Set APP_MAIL_RETRYATTEMPTS=5. With the command-line argument also present, the value is 7, because command-line arguments take precedence over environment variables.
Exercise
A teammate sets APP_MAIL_RETRY_ATTEMPTS=5, but the app still uses 3. Why?
Show answer
The hyphen in retry-attempts must be dropped in the variable name, so it should be APP_MAIL_RETRYATTEMPTS. With the extra underscore, Spring Boot looks for a different key and ignores it.