Working with YAML
Working with YAML
Spring Boot supports two configuration file formats out of the box: the classic application.properties (key=value pairs) and the more expressive application.yml (YAML). Both files are loaded automatically from the classpath root — you do not need any extra dependency or annotation to switch. Understanding the structure of YAML and the trade-offs between the two formats is an essential skill for any professional Spring Boot developer.
YAML Fundamentals in Under Five Minutes
YAML (YAML Ain't Markup Language) represents data as indented key-value pairs, lists, and maps. Spring's YAML support is provided by SnakeYAML, which is already on the classpath whenever you include spring-boot-starter.
The core rules you need to know:
- Indentation defines nesting. Use spaces, never tabs. Two spaces per level is the universal convention in Spring projects.
- Keys and values are separated by a colon followed by a space (
:). - Lists use a leading dash (
-). - Strings can be unquoted unless they contain special characters (
: { } [ ] , # & * ? | > ' " % @ `). - Boolean values are
true/false(lowercase).
Flat Properties vs. Nested YAML — Side by Side
Consider a typical database and server configuration. In application.properties every key is fully qualified:
The exact same configuration in application.yml:
Spring's PropertySourcesPlaceholderConfigurer and the Environment abstraction flatten YAML into the same dot-notation keys internally, so @Value("${server.port}") and @ConfigurationProperties(prefix = "spring.datasource") work identically regardless of which file format you chose.
Lists in YAML
In properties files, lists use indexed keys: app.allowed-origins[0]=https://example.com. In YAML the same list reads naturally:
When bound to a @ConfigurationProperties bean, Spring maps the YAML list directly to a List<String> field.
Multi-Document YAML Files
A single application.yml can contain multiple documents separated by ---. Each document can activate under a specific profile:
The first document supplies defaults for every environment. The second document overrides only the datasource and JPA keys when the production profile is active. This is one of YAML's strongest selling points: you can consolidate what would otherwise be three separate files (application.properties, application-dev.properties, application-prod.properties) into a single readable file.
application-{profile}.yml pattern (one file per profile) scales better and keeps secrets-containing files isolated.
YAML Anchors and Aliases — Reduce Repetition
YAML supports anchors (&name) and aliases (*name) that let you reuse a block without repeating it. This is a YAML-only feature with no equivalent in properties files:
<<:) in all contexts. SnakeYAML resolves anchors before handing the data to Spring's binder, so simple value anchors work reliably. Merge-key anchors (<<:) work for logging and custom properties, but avoid using them inside spring.* or server.* blocks — Spring's relaxed binding can occasionally misinterpret merged maps. Test thoroughly if you use this feature for critical configuration.
When to Prefer YAML Over Properties
Neither format is objectively superior — the choice is a matter of fit:
- Choose YAML when you have deeply nested configuration (e.g. Spring Security, Spring Cloud Gateway route definitions, complex Hikari tuning), when your team already reads YAML fluently from Kubernetes or Docker Compose, or when you want multi-document profile consolidation in a single file.
- Choose Properties when configuration is shallow and few, when the file will be processed by external tools that parse key=value (some legacy CI/CD pipelines), or when team members are unfamiliar with YAML's indentation sensitivity.
- Never mix both for the same application. If
application.ymlandapplication.propertiesboth exist, Spring Boot loads both butapplication.propertieswins in case of conflict — a silent, confusing precedence rule that causes hard-to-diagnose bugs.
Common YAML Pitfalls
YAML's flexibility hides several traps that bite developers once and are never forgotten:
- Tabs vs. spaces: A tab character in YAML is a parse error. Configure your editor to expand tabs to spaces in
.ymlfiles. - Norway Problem: Bare
NOis parsed asfalsein older YAML 1.1 (used by SnakeYAML). Always quote country codes:country: "NO". Spring Boot 2.4+ upgraded to SnakeYAML 1.28 which follows YAML 1.1 but the pitfall still exists withon,off,yes,no. - Colons in values:
url: http://localhost:8080is valid because there is a space after the colon. Butmessage: Hello: Worldbreaks parsing — quote it:message: "Hello: World". - Missing space after colon:
port:8080is treated as a string key with valuenull, not asport = 8080.
Summary
YAML brings hierarchical clarity to Spring Boot configuration. The indented block format eliminates the tedious repetition of shared prefixes, lists bind naturally, and multi-document files let you co-locate profile overrides. Spring Boot's YAML support is zero-configuration — add application.yml to src/main/resources and it is picked up automatically. Watch out for indentation errors, bare boolean-like strings, and the tab trap. In the next lesson you will learn how to keep sensitive values out of both formats entirely, using environment variables and secrets managers.