ConfigurableEnvironment class abstract interface

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 @ConfigurationProperties binding.
getter/setter pairinherited
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 setterinherited
escapeCharacter ← String?
The character that makes a placeholder prefix or separator literal (\${ is the text ${); \ unless changed. null turns 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. null turns defaults off, so the whole text is the key.
no getterinherited

Methods

acceptsProfiles(Profiles profiles) → bool
Whether the profile condition profiles holds for the active profiles, or for the default profiles while none is active.
inherited
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.
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 key as text, placeholders resolved, or defaultValue (null unless given) when no source has it.
inherited
getPropertyAs<T>(String key, {T? defaultValue}) → T?
The value of the property key converted to T, or defaultValue when no source has it.
inherited
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.
inherited
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.
inherited
matchesProfiles(List<String> profileExpressions) → bool
Whether any of profileExpressions holds for the active profiles (or the default ones while none is active).
inherited
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.
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.
inherited
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.
inherited
setActiveProfiles(List<String> profiles) → void
Makes profiles the active profiles, dropping any set before; the property spring.profiles.active is then no longer read.
setDefaultProfiles(List<String> profiles) → void
Makes profiles the profiles used while none is active, replacing ['default'].
setRequiredProperties(List<String> keys) → void
Adds keys to 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 MissingRequiredPropertiesException listing 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