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.
systemEnvironmentreplaces 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
@ConfigurationPropertiesbinding.getter/setter pairoverride -
defaultProfiles
→ List<
String> -
The profiles that count as active while activeProfiles is empty:
['default']unless changed withsetDefaultProfilesor the propertyspring.profiles.default. This is whyapplication-default.ymland@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.nullturns 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.nullturns defaults off, so the whole text is the key.no getteroverride
Methods
-
acceptsProfiles(
Profiles profiles) → bool -
Whether the profile condition
profilesholds for the active profiles, or for the default profiles while none is active.override -
addActiveProfile(
String profile) → void -
Adds
profileto the active profiles, keeping the others. When none was set yet,spring.profiles.activeis 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
keyas text, placeholders resolved, ordefaultValue(nullunless given) when no source has it.override -
getPropertyAs<
T> (String key, {T? defaultValue}) → T? -
The value of the property
keyconverted toT, ordefaultValuewhen no source has it.override -
getRequiredProperty(
String key) → String -
The value of the property
keyas text, for settings the application cannot run without. ThrowsMissingPropertyExceptionwhen no source has it.override -
getRequiredPropertyAs<
T> (String key) → T -
The value of the property
keyconverted toT. ThrowsMissingPropertyExceptionwhen no source has it (or it converts tonull), and aConversionExceptionwhen it cannot be converted.override -
isProfileActive(
String profile) → bool -
Whether the single profile
profileis active, or is a default profile while none is active. For expressions use matchesProfiles. -
matchesProfiles(
List< String> profileExpressions) → bool -
Whether any of
profileExpressionsholds for the active profiles (or the default ones while none is active).override -
merge(
ConfigurableEnvironment parent) → void -
Takes over what
parenthas: 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 replacedefault.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}intextwith 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}intextlike resolvePlaceholders, but throwsPlaceholderResolutionExceptionfor 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
profilesthe active profiles, dropping any set before; the propertyspring.profiles.activeis then no longer read.override -
setDefaultProfiles(
List< String> profiles) → void -
Makes
profilesthe profiles used while none is active, replacing['default'].override -
setRequiredProperties(
List< String> keys) → void -
Adds
keysto 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
MissingRequiredPropertiesExceptionlisting 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.