PropertySourceLoader class abstract

A reader for one config file format: it turns the text of a file into property sources, one per document in the file.

The config data loader picks a loader by file extension. .properties (PropertiesPropertySourceLoader) and .yml / .yaml (YamlPropertySourceLoader) are built in, which covers almost every application. Write one only for another format, such as JSON or TOML config files, and pass it to a ConfigDataEnvironment you run yourself; SpringApplication always uses the two built-in loaders.

Unlike Spring Boot, a loader gets the file's content as a string, not a Resource, because some resources can only be read asynchronously; the file is decoded with encoding first.

// `application.json` files next to the YAML ones.
class JsonPropertySourceLoader extends PropertySourceLoader {
  const JsonPropertySourceLoader();

  @override
  List<String> get fileExtensions => const ['json'];

  @override
  List<AbstractPropertySource<Object>> load(String name, String content) =>
      [
        MapPropertySource(
          name,
          YamlPropertySourceLoader.flatten(jsonDecode(content) as Object),
        ),
      ];
}

ConfigDataEnvironment(
  environment,
  loaders: const [
    PropertiesPropertySourceLoader(),
    YamlPropertySourceLoader(),
    JsonPropertySourceLoader(),
  ],
);

Spring (Java):

public class JsonPropertySourceLoader implements PropertySourceLoader {
  @Override
  public String[] getFileExtensions() {
    return new String[] {"json"};
  }

  @Override
  public List<PropertySource<?>> load(String name, Resource resource) {
    return List.of(new MapPropertySource(name, flattenJson(resource)));
  }
}
// registered in META-INF/spring.factories

Constructors

PropertySourceLoader()
Lets subclasses be const.
const

Properties

encoding → Encoding
The character encoding the files are decoded with before load; UTF-8 unless overridden.
no setter
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 setter
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.
load(String name, String content) → List<AbstractPropertySource<Object>>
Parses content, the text of one file, into one property source per document, in file order, skipping empty documents. Keys must be flat (server.port), as in a .properties file.
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