Sub topic 5
Relaxed binding: one property, several spellings
This chapter assumes you can read .properties and YAML, know how @ConfigurationProperties records work, and have seen environment variables and command-line arguments override a setting.
The problem it solves
The same setting has to be written in different places. YAML conventionally uses retry-attempts. Java code uses retryAttempts. Shell environment variables cannot contain dots or dashes. Spring Boot's relaxed binding maps all of these spellings to one property, so you don't need a separate key for each source.
Accepted spellings
Take the retryAttempts component of the MailProperties record. All of these set it:
| Source | Spelling | Notes |
|---|---|---|
| `.properties` or YAML | `app.mail.retry-attempts` | Canonical form: lowercase kebab-case. Recommended. |
| `.properties` or YAML | `app.mail.retryAttempts` | Camel case. |
| `.properties` or YAML | `app.mail.retry_attempts` | Underscore notation. |
| Environment variable | `APP_MAIL_RETRYATTEMPTS` | Uppercase, dots become underscores, dashes are removed. |
| Command line | `--app.mail.retry-attempts=5` | Same rules as a file. |
app:
mail:
retry-attempts: 3APP_MAIL_RETRYATTEMPTS=5 java -jar app.jarThe environment variable overrides the file value because environment variables rank above packaged files in the precedence order from the earlier chapter. Only the spelling differs.
Converting a name to an environment variable
Start from the canonical form and apply three steps:
- Replace each dot with an underscore.
- Remove every dash.
- Convert to uppercase.
So app.mail.retry-attempts becomes APP_MAIL_RETRYATTEMPTS. Do not write APP_MAIL_RETRY_ATTEMPTS. Boot reads each underscore as a separator between name segments, so that variable targets app.mail.retry.attempts, which does not exist. Nothing fails. The setting is silently ignored.
Where relaxed binding applies
Relaxed binding is a feature of the binder behind @ConfigurationProperties. For @Value, it works only if you write the key in canonical form:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
class RetryReporter {
private final int attempts;
RetryReporter(@Value("${app.mail.retry-attempts}") int attempts) {
this.attempts = attempts;
}
int attempts() {
return attempts;
}
}With the canonical key, APP_MAIL_RETRYATTEMPTS still resolves. If you wrote @Value("${app.mail.retryAttempts}"), the environment variable would not match, and only a file key spelled exactly retryAttempts would.
Common errors
- Dashes in variable names. Shells often reject
APP_MAIL_RETRY-ATTEMPTS. Remove the dash. - Extra underscores. An underscore between words changes the property path instead of spelling out a word boundary, as shown above.
- Mixed spellings in one file. If both
retry-attemptsandretryAttemptsappear, the later source or entry wins, and the result is hard to predict. Use one spelling.
Exercise
A record binds app.mail.sender. Which environment variable overrides it, and why does APP_MAIL_RETRY_ATTEMPTS fail to override app.mail.retry-attempts?
Show answer
Use APP_MAIL_SENDER: uppercase, with dots replaced by underscores. APP_MAIL_RETRY_ATTEMPTS fails because each underscore is treated as a segment separator, so it targets app.mail.retry.attempts. The correct name removes the dash: APP_MAIL_RETRYATTEMPTS.