Environment class abstract interface

The application's configuration as code sees it: every property from every source (config files, command line, environment variables), and the active profiles.

Most beans never need it: @Value and @ConfigurationProperties read properties for them, and @Profile switches beans on profiles. Take the environment when the key is only known at run time, when a decision depends on the active profiles in code, or to resolve ${...} placeholders in text. It is a bean, so a constructor parameter of type Environment receives it (EnvironmentAware works too). It is read-only; ConfigurableEnvironment is the side that changes it.

@Service()
class Banner {
  Banner(this.environment);
  final Environment environment;

  // The key depends on the language, so `@Value` cannot name it.
  String text(String language) =>
      environment.getProperty('shop.banner.$language') ??
      environment.resolvePlaceholders(r'Welcome to ${shop.title}');

  bool get showDebugInfo => environment.matchesProfiles(['dev & !test']);
}

Spring (Java):

@Service
class Banner {
  private final Environment environment;
  Banner(Environment environment) { this.environment = environment; }

  String text(String language) {
    String text = environment.getProperty("shop.banner." + language);
    return text != null ? text
        : environment.resolvePlaceholders("Welcome to ${shop.title}");
  }

  boolean showDebugInfo() {
    return environment.matchesProfiles("dev & !test");
  }
}
Implemented types

Properties

activeProfiles → List<String>
The profiles switched on for this run, such as ['prod', 'eu']; empty when none is.
no setter
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 setter
hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

acceptsProfiles(Profiles profiles) → bool
Whether the profile condition profiles holds for the active profiles, or for the default profiles while none is active.
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).
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
toString() → String
A string representation of this object.
inherited

Operators

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