Sub topic 5
Relaxed binding: one property, several spellings
This chapter assumes you can already read application.yaml and .properties files, and that you have bound a record with @ConfigurationProperties. Here you will learn why one setting can be written several ways, which spelling to pick, and the mistakes that leave a value silently unset.
The problem relaxed binding solves
Java field names use camelCase, such as retryAttempts. Property files conventionally use kebab-case, such as retry-attempts. Shell environment variables allow only letters, digits and underscores, and are normally uppercase, so they cannot hold dots or dashes.
Spring Boot does not make you pick one style for every source. Before it compares names, it converts them to a single canonical form: lowercase, with elements separated by dots and words in an element separated by dashes. This is called relaxed binding. app.mail.retry-attempts is the canonical form of the property that binds to retryAttempts in the record MailSettings below.
Accepted spellings
All of these name the same property:
| Source | Spelling | Notes |
|---|---|---|
| `.properties` or YAML | `app.mail.retry-attempts` | Kebab-case. Recommended. |
| `.properties` or YAML | `app.mail.retryAttempts` | camelCase. |
| `.properties` or YAML | `app.mail.retry_attempts` | Underscore notation. |
| Command-line argument | `--app.mail.retry-attempts=5` | Any of the three file spellings also works. |
| Java system property | `-Dapp.mail.retry-attempts=5` | Same rules as files. |
| Environment variable | `APP_MAIL_RETRYATTEMPTS` | Uppercase, no dashes. |
For environment variables, Spring Boot's documented conversion has three steps: replace dots with underscores, remove dashes, and convert to uppercase. So app.mail.retry-attempts becomes APP_MAIL_RETRYATTEMPTS.
A runnable demonstration
This record binds the app.mail properties. Its components have no dashes or underscores, because Java names cannot contain them. The binder bridges that gap.
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.DefaultValue;
@ConfigurationProperties(prefix = "app.mail")
public record MailSettings(
@DefaultValue("localhost") String host,
@DefaultValue("25") int port,
@DefaultValue("3") int retryAttempts) {
}This application class registers the record and prints what was bound:
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableConfigurationProperties(MailSettings.class)
public class RelaxedDemoApplication {
public static void main(String[] args) {
SpringApplication.run(RelaxedDemoApplication.class, args);
}
@Bean
CommandLineRunner show(MailSettings mail) {
return args -> System.out.println(mail);
}
}Run it with each spelling and you get the same result:
java -jar app.jar --app.mail.retry-attempts=5
java -jar app.jar --app.mail.retryAttempts=5
java -jar app.jar --app.mail.retry_attempts=5
APP_MAIL_RETRYATTEMPTS=5 java -jar app.jarEach command prints MailSettings[host=localhost, port=25, retryAttempts=5].
Choosing a spelling
Use kebab-case in .properties and YAML files. It is the canonical form, it is the form shown in the Spring Boot reference documentation, and it reads the same everywhere. Use the uppercase form only where you must, which means environment variables. Avoid mixing spellings of the same key across files, because a reader searching for retry-attempts will not find retryAttempts.
Where relaxed binding is weaker: `@Value`
Relaxed binding is complete for @ConfigurationProperties. For @Value it is limited. Spring Boot applies relaxed rules only when the placeholder is written in canonical kebab-case. Compare these two:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
class RetryPolicy {
// Works with retry-attempts, retryAttempts and APP_MAIL_RETRYATTEMPTS
private final int attempts;
// Matches only a property literally named app.mail.retryAttempts
private final int attemptsStrict;
RetryPolicy(@Value("${app.mail.retry-attempts:3}") int attempts,
@Value("${app.mail.retryAttempts:3}") int attemptsStrict) {
this.attempts = attempts;
this.attemptsStrict = attemptsStrict;
}
int attempts() {
return attempts;
}
int attemptsStrict() {
return attemptsStrict;
}
}If you use @Value, write the key in kebab-case. If you use @Value("${app.mail.retryAttempts}"), an environment variable APP_MAIL_RETRYATTEMPTS will still match, but a file entry written as app.mail.retry-attempts will not.
Lists and maps
Collections follow the same idea, with a few extra spellings.
- Lists in a properties file: use indexes (
app.mail.recipients[0][email protected]) or a comma-separated value ([email protected],[email protected]). - Lists in YAML: use a normal YAML sequence.
- Lists in environment variables: use underscore-separated indexes (
[email protected]) or a comma-separated value. - Maps: each key becomes part of the property name. If a map key contains characters other than lowercase letters, digits and dashes, wrap it in brackets, as in
app.mail.headers[X-Trace-Id]=on. In YAML, quote the key as well, otherwise the brackets are dropped.
Map keys read from environment variables are lowercase, so a key such as X-Trace-Id cannot be expressed that way. Put maps like that in a file.
Common errors
1. Underscores where Spring Boot expects none. APP_MAIL_RETRY_ATTEMPTS does not bind to retry-attempts. Each underscore in an environment variable stands for a dot, so Spring Boot reads it as app.mail.retry.attempts, a nested property named attempts. Write APP_MAIL_RETRYATTEMPTS.
2. No error on a misspelled key. By default Spring Boot ignores properties it cannot match. A record keeps its @DefaultValue, or 0 for a primitive, and the application starts normally. If you suspect a typo, print the bound object, as the demo above does, or use the inspection tools covered in a later chapter.
3. Both spellings in one source. If a file contains retry-attempts and retryAttempts for the same key, do not rely on which one wins. Keep one.
4. Dashes in a shell variable name. export APP_MAIL_RETRY-ATTEMPTS=5 is rejected by most shells. Dashes are removed from environment variable names for this reason.
5. A different source, not a different spelling. If a value looks wrong, the cause may be precedence rather than naming. An environment variable beats a packaged application.yaml even when you spell the key differently. Check the precedence order before you suspect binding.
Summary of rules
- The binder compares names in canonical form: lowercase, dot-separated elements, dashes between words.
- In files, command-line arguments and system properties, kebab-case, camelCase and underscore spellings are equivalent.
- Environment variables are uppercase, with dots turned into underscores and dashes removed.
@ConfigurationPropertiesgets full relaxed binding.@Valuegets it only for kebab-case placeholders.- Unmatched properties are ignored without warning by default.
Exercise
A deployment sets the environment variable APPMAILRETRY_ATTEMPTS=7, but MailSettings.retryAttempts still shows the default value 3. Why does the value not bind, and what should the variable be called?
Show answer
Spring Boot converts each underscore in an environment variable name to a dot, so APPMAILRETRYATTEMPTS is read as app.mail.retry.attempts, a nested property that does not match app.mail.retry-attempts. Dashes must be removed rather than replaced by underscores, so the correct name is APPMAIL_RETRYATTEMPTS. The binder ignores the unmatched variable without an error, so the record keeps its default of 3.
Exercise
A class uses @Value("${app.mail.retryAttempts}") and application.yaml defines retry-attempts under app.mail. The application fails at startup with an unresolvable placeholder. How can you fix it without changing the YAML?
Show answer
@Value applies relaxed rules only to placeholders written in canonical kebab-case. Change the placeholder to @Value("${app.mail.retry-attempts}"), or better, bind the properties with @ConfigurationProperties, which accepts every spelling.