An Environment that can be changed: its list of property sources, its profiles and how it converts and resolves values.
Changes must happen before beans read the configuration, so this side
is for startup code. The framework fills it (config files, command
line) while the application starts; an application receives it in an
EnvironmentPostProcessor to add a property source or switch on a
profile, and in tests. Beans should take Environment instead.
// Passed to SpringApplication(environmentPostProcessors: [...]).
class RegionProfile implements EnvironmentPostProcessor {
@override
void postProcessEnvironment(
ConfigurableEnvironment environment,
SpringApplication application,
) {
// Machines in the EU data centre run with the `eu` profile.
if (environment.getProperty('REGION') == 'eu') {
environment.addActiveProfile('eu');
}
}
}
Spring (Java):
class RegionProfile implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(
ConfigurableEnvironment environment, SpringApplication application) {
if ("eu".equals(environment.getProperty("REGION"))) {
environment.addActiveProfile("eu");
}
}
}
- Implemented types
Properties
-
activeProfiles
→ List<
String> -
The profiles switched on for this run, such as
['prod', 'eu']; empty when none is.no setterinherited - conversionService ↔ ConversionService
-
The service that converts values in PropertyResolver.getPropertyAs
and in
@ConfigurationPropertiesbinding.getter/setter pairinherited -
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 setterinherited - escapeCharacter ← String?
-
The character that makes a placeholder prefix or separator literal
(
\${is the text${);\unless changed.nullturns escaping off.no getterinherited - 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 getterinherited - placeholderPrefix ← String
-
The text that opens a placeholder;
${unless changed.no getterinherited - placeholderSuffix ← String
-
The text that closes a placeholder;
}unless changed.no getterinherited - 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.
no setter
- 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.
no setter
- 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 getterinherited
Methods
-
acceptsProfiles(
Profiles profiles) → bool -
Whether the profile condition
profilesholds for the active profiles, or for the default profiles while none is active.inherited -
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. -
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".inherited -
getProperty(
String key, {String? defaultValue}) → String? -
The value of the property
keyas text, placeholders resolved, ordefaultValue(nullunless given) when no source has it.inherited -
getPropertyAs<
T> (String key, {T? defaultValue}) → T? -
The value of the property
keyconverted toT, ordefaultValuewhen no source has it.inherited -
getRequiredProperty(
String key) → String -
The value of the property
keyas text, for settings the application cannot run without. ThrowsMissingPropertyExceptionwhen no source has it.inherited -
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.inherited -
matchesProfiles(
List< String> profileExpressions) → bool -
Whether any of
profileExpressionsholds for the active profiles (or the default ones while none is active).inherited -
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. -
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.inherited -
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.inherited -
setActiveProfiles(
List< String> profiles) → void -
Makes
profilesthe active profiles, dropping any set before; the propertyspring.profiles.activeis then no longer read. -
setDefaultProfiles(
List< String> profiles) → void -
Makes
profilesthe profiles used while none is active, replacing['default']. -
setRequiredProperties(
List< String> keys) → void -
Adds
keysto the properties validateRequiredProperties checks; keys added earlier stay.inherited -
toString(
) → String -
A string representation of this object.
inherited
-
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.inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited