Reading properties by key: as text or as a typed value, with or without
a default, and resolving ${...} placeholders in any text.
Environment is the property resolver an application meets; this
interface only names the reading part of it, so code that needs nothing
else (no profiles) can ask for less. On every read, placeholders in the
value are resolved (url=${host}:8080) and the text is converted to the
requested type.
// The environment is the resolver beans get.
final PropertyResolver resolver = environment;
final host = resolver.getProperty('db.host', defaultValue: 'localhost');
final port = resolver.getPropertyAs<int>('db.port', defaultValue: 5432);
final url = resolver.resolvePlaceholders(r'jdbc://${db.host}:${db.port}');
Spring (Java):
PropertyResolver resolver = environment;
String host = resolver.getProperty("db.host", "localhost");
int port = resolver.getProperty("db.port", Integer.class, 5432);
String url = resolver.resolvePlaceholders("jdbc://${db.host}:${db.port}");
Typed access is getPropertyAs<T> instead of an overloaded
getProperty(key, Class), as Dart has no overloading.
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
-
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". -
getProperty(
String key, {String? defaultValue}) → String? -
The value of the property
keyas text, placeholders resolved, ordefaultValue(nullunless given) when no source has it. -
getPropertyAs<
T> (String key, {T? defaultValue}) → T? -
The value of the property
keyconverted toT, ordefaultValuewhen no source has it. -
getRequiredProperty(
String key) → String -
The value of the property
keyas text, for settings the application cannot run without. ThrowsMissingPropertyExceptionwhen no source has it. -
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. -
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. -
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. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited