YamlPropertySourceLoader class final

The reader for YAML config files (.yml, .yaml), used for application.yml and its profile variants.

The environment knows only flat keys (server.port), so the nested YAML tree is flattened. The config data loader uses it by default, so applications only write the files; flatten is useful on its own to turn a decoded JSON or YAML tree into properties.

Every YAML document (documents are separated by ---) becomes one property source, flattened like Spring's YamlProcessor:

  • nested maps become dotted keys: server: {port: 8080} is server.port;
  • list items are indexed: hosts[0], servers[1].name;
  • ~, null and empty values are '', and so are empty lists and maps (app.tags: []);
  • keys that are not strings are put in brackets: 1: a is [1], and "[a.b]" keys keep their dots (app.map[a.b]);
  • scalars keep their YAML type (8080 is an int, true a bool), strings (including | and > block scalars) stay as written;
  • merge keys (<<: *defaults) copy the entries of the referenced mappings that the mapping does not define itself;
  • a document that is just a scalar is the property document; empty documents are skipped.

Duplicate keys are an error, as in Spring Boot, and so is invalid YAML; both fail the start with the line and column.

# application.yml
spring:
  profiles:
    active: dev
app:
  hosts: [a.example, b.example]
---
spring.config.activate.on-profile: dev
app.debug: true
final sources = const YamlPropertySourceLoader().load(
  'application.yml',
  'app:\n  hosts: [a, b]\n',
);
sources.single.getProperty('app.hosts[1]'); // 'b'

Spring (Java):

List<PropertySource<?>> sources = new YamlPropertySourceLoader()
    .load("application.yml", new ClassPathResource("application.yml"));

Differences from Spring Boot: YAML 1.2 (package:yaml) instead of SnakeYAML's YAML 1.1, so on, off, yes and no stay strings (converting them to bool or an enum still works) and 0777 is the number 777.

Inheritance

Constructors

YamlPropertySourceLoader()
Creates the loader. It holds no state, so a const one can be shared.
const

Properties

encoding → Encoding
The character encoding the files are decoded with before load; UTF-8 unless overridden.
no setterinherited
fileExtensions → List<String>
The file extensions this loader reads, without the dot (json). When several loaders claim one, the first in the list wins.
no setteroverride
hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

encodingFor(Encoding hint) → Encoding
The encoding of a file whose location names one with a Spring Boot 4.1 file hint (x.properties[encoding=iso-8859-1], x.conf[.yml] [encoding=utf-8]): encoding unless overridden, so the hint is ignored, as Boot's YAML loader ignores it (the default of Boot's PropertySourceLoader.load(name, resource, encoding)). PropertiesPropertySourceLoader decodes with hint.
inherited
load(String name, String content) → List<AbstractPropertySource<Object>>
Parses content into one source per non-empty document. Throws a FormatException (a YamlException with the line and column) for invalid YAML or a duplicate key, and for nesting too deep to read (thousands of levels, which overflow the stack; SnakeYAML stops at 50).
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

flatten(Object document) → Map<String, Object>
Turns one parsed tree of maps, lists and scalars into flat property keys and values, by the rules listed on the class. It accepts the result of jsonDecode as well as a YAML document.