π« Interface for components that can be configured with an Environment
.
The EnvironmentAware interface allows components to access and interact with the application's environment configuration, including profiles, property sources, and configuration metadata.
Environment Capabilities:
- Profile Management: Check active and default profiles
- Property Resolution: Access configuration properties from various sources
- Configuration Detection: Determine application configuration state
- Feature Toggling: Enable/disable features based on environment
Framework Integration:
- Called during component initialization with non-null Environment
- The environment is fully configured with all property sources
- Profiles are activated and property resolution is operational
Example Usage:
@Component
class EnvironmentAwareService implements EnvironmentAware {
late Environment _environment;
@override
void setEnvironment(Environment environment) {
_environment = environment;
}
bool isFeatureEnabled(String featureName) {
return _environment.getProperty(
'features.$featureName.enabled',
bool.fromString,
defaultValue: false
);
}
String getDataSourceUrl() {
return _environment.getRequiredProperty('datasource.url');
}
bool isDevelopment() {
return _environment.acceptsProfiles({'development'});
}
bool isProduction() {
return _environment.acceptsProfiles({'production'});
}
}
Best Practices:
- Use for environment-specific configuration, not business logic
- Provide sensible default values for optional properties
- Use
getRequiredProperty
for mandatory configuration - Consider using
@ConfigurationProperties
for structured configuration
See also:
Environment
for the environment interface- ApplicationContextAware for alternative environment access
ConfigurationProperties
for type-safe configuration binding
- Implementers
Properties
- hashCode β int
-
The hash code for this object.
no setterinherited
- runtimeType β Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) β dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
setEnvironment(
Environment environment) β void -
Sets the
Environment
that this component runs in. -
toString(
) β String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) β bool -
The equality operator.
inherited