PropertyPlaceholderHelper class

The engine that replaces ${name} and ${name:default} placeholders in text, looking each name up with a function you give.

The environment uses it to resolve placeholders in property values and @Value. Use it directly to fill placeholders from something that is not the environment, such as a map of template variables or a database row; to resolve against the configuration, call environment.resolvePlaceholders instead.

  • Defaults may contain placeholders: ${a:${b:c}}.
  • Keys may contain placeholders: ${prefix.${suffix}}.
  • A found value is resolved again, so values may refer to other values; a loop is detected and reported.
  • ${db:url} is first looked up as the key db:url, then as db with the default url (Spring 6.2).
  • The escape character (\ by default) before the prefix or the separator makes it literal: \${not.a.placeholder} gives ${not.a.placeholder}.
  • A ${ without a closing } is kept as text.
// Fills a URL template from values that are not properties.
final helper = PropertyPlaceholderHelper();
final values = {'host': 'localhost', 'port': '8080'};
helper.replacePlaceholders(
  r'http://${host}:${port:80}/${path:}',
  (key) => values[key],
); // 'http://localhost:8080/'

Spring (Java):

var helper = new PropertyPlaceholderHelper("${", "}", ":", '\\', false);
Map<String, String> values = Map.of("host", "localhost", "port", "8080");
String url = helper.replacePlaceholders(
    "http://${host}:${port:80}/${path:}", values::get);

Constructors

PropertyPlaceholderHelper({String prefix = r'${', String suffix = '}', String? valueSeparator = ':', String? escapeCharacter = r'\', bool ignoreUnresolvablePlaceholders = false})
Creates a helper for the given syntax. The defaults give Spring's ${key:default} with \ as the escape character.

Properties

escapeCharacter → String?
The character that makes a following prefix or valueSeparator literal, \ by default; null turns escaping off.
final
hashCode → int
The hash code for this object.
no setterinherited
ignoreUnresolvablePlaceholders → bool
Whether a placeholder that cannot be resolved stays in the text (true) instead of throwing (false).
final
prefix → String
The text that opens a placeholder, ${ by default.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
suffix → String
The text that closes a placeholder, } by default.
final
valueSeparator → String?
The text between a key and its default, : by default; null turns defaults off.
final

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
replacePlaceholders(String value, String? resolver(String key)) → String
Returns value with every placeholder replaced, looking keys up with resolver.
toString() → String
A string representation of this object.
inherited

Operators

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