AbstractPropertySource<T extends Object> class abstract

One place property values come from, under a name: a config file, the command line, the environment variables, a map of defaults.

The environment does not hold properties itself. It holds an ordered list of property sources and asks them in turn, the first one that has a key winning; that is how the command line overrides a config file. Every source the framework adds is a subclass of this class.

An application rarely touches it. Extend it to read properties from a place the framework does not know, such as a feature flag service or a secrets vault, and add the source to the environment early, from an EnvironmentPostProcessor. When the source can list its keys, extend EnumerablePropertySource instead, so @ConfigurationProperties binding can find keys by prefix. A plain map is MapPropertySource.

Two sources are equal when their names are equal, so a MutablePropertySources holds at most one source per name. The class is called AbstractPropertySource because @PropertySource is the annotation.

// Flags come from a flag service and override the config files.
class FeatureFlagSource extends AbstractPropertySource<FlagClient> {
  FeatureFlagSource(FlagClient client) : super('featureFlags', client);

  @override
  Object? getProperty(String name) => source.valueOf(name);
}

environment.propertySources.addFirst(FeatureFlagSource(client));

Spring (Java):

class FeatureFlagSource extends PropertySource<FlagClient> {
  FeatureFlagSource(FlagClient client) { super("featureFlags", client); }

  @Override
  public Object getProperty(String name) {
    return getSource().valueOf(name);
  }
}
Implementers

Constructors

AbstractPropertySource(String name, T source)
Creates a source called name that reads from source.
const

Properties

hashCode → int
The hash code for this object.
no setteroverride
name → String
The name of this source, unique within the environment's list. It is how other code finds, replaces or orders the source, and it appears in error messages (Config resource 'resources:/application.yml').
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
source → T
The object the values are read from: a map, the command-line arguments, a client.
final

Methods

containsProperty(String name) → bool
Whether this source has the property name. By default, whether getProperty returns a value other than null.
getProperty(String name) → Object?
The value of the property name, or null when this source does not have it (the environment then asks the next source).
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
override

Operators

operator ==(Object other) → bool
Whether other is a source with the same name, whatever its values, as in Spring.
override