PropertySourceFactory class abstract interface

Turns a file named in @PropertySource into a property source; write one to let @PropertySource read a format other than .properties.

@PropertySource(['resources:extra.properties']) adds one file's properties to the environment from a @Configuration class. As in Spring, it reads .properties files unless the annotation's factory names a class implementing this interface. The usual reason to write one is YAML. The factory class needs a constructor without arguments, so the generated code can create it. Most applications need neither: application.yml and spring.config.import already read YAML.

// Lets `@PropertySource` read YAML: all documents merged into one.
class YamlPropertySourceFactory implements PropertySourceFactory {
  const YamlPropertySourceFactory();

  @override
  AbstractPropertySource<Object> createPropertySource(
    String? name,
    EncodedResource resource,
  ) => MapPropertySource(name ?? resource.resource.description, {
    for (final document in const YamlPropertySourceLoader().load(
      resource.resource.description,
      resource.readAsStringSync(),
    ))
      ...(document as MapPropertySource).source,
  });
}

@Configuration()
@PropertySource(['resources:app.yml'], factory: YamlPropertySourceFactory)
class AppConfig {}

Spring (Java):

class YamlPropertySourceFactory implements PropertySourceFactory {
  @Override
  public PropertySource<?> createPropertySource(
      String name, EncodedResource resource) throws IOException {
    var yaml = new YamlPropertiesFactoryBean();
    yaml.setResources(resource.getResource());
    return new PropertiesPropertySource(
        name != null ? name : resource.getResource().getDescription(),
        yaml.getObject());
  }
}

@Configuration
@PropertySource(value = "resources:app.yml",
    factory = YamlPropertySourceFactory.class)
class AppConfig {}

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

createPropertySource(String? name, EncodedResource resource) → AbstractPropertySource<Object>
Reads resource synchronously, decoded with its encoding, and returns its properties as one source. Name the source name when it is given (the annotation's name), otherwise after the resource.
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