StandardEnvironment class

The environment implementation used everywhere: an empty list of property sources except the environment variables, plus profiles.

SpringApplication and ApplicationContext.run create one and fill it (command line, config files, defaults), so an application usually only receives it. Create one yourself to read configuration without starting a context, in a command-line tool or a test, or to give a context an environment prepared in advance.

It starts with one property source, a SystemEnvironmentPropertySource (empty on the web). Unlike Spring there are no JVM system properties, so command-line arguments are usually the highest source.

// A tool that reads the application's configuration by itself.
Future<void> main(List<String> args) async {
  final environment = StandardEnvironment()
    ..propertySources.addFirst(SimpleCommandLinePropertySource(args));
  await ConfigDataEnvironment(environment).processAndApply();
  final port = environment.getPropertyAs<int>('server.port');
}

Spring (Java):

var environment = new StandardEnvironment();
environment.getPropertySources()
    .addFirst(new SimpleCommandLinePropertySource(args));
Integer port = environment.getProperty("server.port", Integer.class);
// Spring Boot's SpringApplication does all of this, plus config data.
Implemented types

Constructors

StandardEnvironment({Map<String, String>? systemEnvironment})
Creates an environment whose only property source is the environment variables. systemEnvironment replaces the variables of the process, so a test can set them.

Properties

activeProfiles → List<String>
The profiles switched on for this run, such as ['prod', 'eu']; empty when none is.
no setteroverride
conversionService ↔ ConversionService
The service that converts values in PropertyResolver.getPropertyAs and in @ConfigurationProperties binding.
getter/setter pairoverride
defaultProfiles → List<String>
The profiles that count as active while activeProfiles is empty: ['default'] unless changed with setDefaultProfiles or the property spring.profiles.default. This is why application-default.yml and @Profile('default') apply to a run without profiles.
no setteroverride
escapeCharacter ← String?
The character that makes a placeholder prefix or separator literal (\${ is the text ${); \ unless changed. null turns escaping off.
no getteroverride
hashCode → int
The hash code for this object.
no setterinherited
ignoreUnresolvableNestedPlaceholders ← bool
Whether reading a property whose value contains an unresolvable placeholder keeps the placeholder as text (true) instead of throwing (false, the default).
no getteroverride
placeholderPrefix ← String
The text that opens a placeholder; ${ unless changed.
no getteroverride
placeholderSuffix ← String
The text that closes a placeholder; } unless changed.
no getteroverride
propertySources → MutablePropertySources
The ordered list of sources the environment reads properties from; for each key the first source that has it wins. Add, remove or reorder sources here.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
systemEnvironment → Map<String, String>
The environment variables of the process, as a read-only map; empty on the web.
final
valueSeparator ← String?
The text between a key and its default in ${key:default}; : unless changed. null turns defaults off, so the whole text is the key.
no getteroverride

Methods

acceptsProfiles(Profiles profiles) → bool
Whether the profile condition profiles holds for the active profiles, or for the default profiles while none is active.
override
addActiveProfile(String profile) → void
Adds profile to the active profiles, keeping the others. When none was set yet, spring.profiles.active is read first, so the profiles from the configuration are kept too.
override
containsProperty(String key) → bool
Whether some source has the property key, even with an empty value. Use it to tell "set to empty" from "not set".
override
customizePropertySources(MutablePropertySources sources) → void
Adds the sources a new environment starts with: the environment variables. Override it in a subclass that should always start with more sources.
getProperty(String key, {String? defaultValue}) → String?
The value of the property key as text, placeholders resolved, or defaultValue (null unless given) when no source has it.
override
getPropertyAs<T>(String key, {T? defaultValue}) → T?
The value of the property key converted to T, or defaultValue when no source has it.
override
getRequiredProperty(String key) → String
The value of the property key as text, for settings the application cannot run without. Throws MissingPropertyException when no source has it.
override
getRequiredPropertyAs<T>(String key) → T
The value of the property key converted to T. Throws MissingPropertyException when no source has it (or it converts to null), and a ConversionException when it cannot be converted.
override
isProfileActive(String profile) → bool
Whether the single profile profile is active, or is a default profile while none is active. For expressions use matchesProfiles.
matchesProfiles(List<String> profileExpressions) → bool
Whether any of profileExpressions holds for the active profiles (or the default ones while none is active).
override
merge(ConfigurableEnvironment parent) → void
Takes over what parent has: its property sources go at the end, below this environment's own (except those whose name is already here), its active profiles are added, and its default profiles replace default.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
resolvePlaceholders(String text) → String
Replaces every ${key} and ${key:default} in text with the property's value, for text written by someone else, such as a message template. A placeholder that cannot be resolved and has no default stays as it is.
override
resolveRequiredPlaceholders(String text) → String
Replaces every ${key} and ${key:default} in text like resolvePlaceholders, but throws PlaceholderResolutionException for a placeholder that cannot be resolved and has no default. Use it when a missing value is an error.
override
setActiveProfiles(List<String> profiles) → void
Makes profiles the active profiles, dropping any set before; the property spring.profiles.active is then no longer read.
override
setDefaultProfiles(List<String> profiles) → void
Makes profiles the profiles used while none is active, replacing ['default'].
override
setRequiredProperties(List<String> keys) → void
Adds keys to the properties validateRequiredProperties checks; keys added earlier stay.
override
toString() → String
A string representation of this object.
override
validateRequiredProperties() → void
Checks that every key given to setRequiredProperties has a value, and throws MissingRequiredPropertiesException listing all the missing ones at once. An application context calls it when it starts, before any bean is created.
override

Operators

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

Constants

activeProfilesPropertyName → const String
The property that lists the active profiles, comma-separated: spring.profiles.active.
defaultProfilesPropertyName → const String
The property that lists the default profiles, comma-separated: spring.profiles.default.
reservedDefaultProfileName → const String
The name of the default profile unless changed: default.