Binder class final

Reads a group of properties from the environment as one typed value: a list, a map, a Duration or a whole settings object.

environment.getProperty returns one string. Some settings are not one string: a list of hosts written as app.hosts[0], app.hosts[1], a map of headers, or a nested object. The binder collects the properties under a name and converts them to the requested type, with the same rules as @ConfigurationProperties (which uses it). Applications use @ConfigurationProperties classes; use the binder directly in code that runs before beans exist, such as an EnvironmentPostProcessor or a library's setup code, or to read one structured value without a class.

The rules:

  • Relaxed names: app.max-size, app.maxSize, app.max_size and the environment variable APP_MAXSIZE are one property; the first property source (in the environment's order) that has it wins.
  • Lists and sets: a comma list (app.hosts=a,b) or indexed keys (app.hosts[0]=a, YAML lists, APP_HOSTS_0_=a), also of nested objects (app.servers[0].port=80). The first property source that has the list supplies all of it: a list is replaced, not merged element by element.
  • Maps: entries from every source (app.headers.x-a=1, app.headers[x.y]=2 for keys with dots), the higher source winning per key, also of nested objects (app.backends.eu.url=...). Keys keep the form they are written in.
  • Nested objects: @Introspected classes, set through their setters (JavaBean binding) or created through their constructor when it has parameters (constructor binding, DefaultValue).
// In an EnvironmentPostProcessor, before any bean exists.
final hosts = Binder.get(environment)
    .bind('app.hosts', const ListType<String>(ClassType<String>()))
    .orElse(const []);

A value that cannot be converted throws a ConfigurationPropertiesBindException naming the property.

Spring (Java):

List<String> hosts = Binder.get(environment)
    .bind("app.hosts", Bindable.listOf(String.class))
    .orElse(List.of());

Constructors

Binder(ConfigurableEnvironment environment, {BeanIntrospector? introspector, ConversionService? conversionService})
A binder that reads environment.

Properties

conversionService → ConversionService
Converts property values (strings, mostly) to the target types.
final
environment → ConfigurableEnvironment
The environment whose property sources are read, in their order of precedence.
final
hashCode → int
The hash code for this object.
no setterinherited
introspector → BeanIntrospector
Describes the classes of nested objects: their setters or constructor parameters.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

bind<T>(String name, TypeInfo<T> target, {BindHandler handler = BindHandler.defaultHandler}) → BindResult<T>
Reads the properties at name as a value of type target.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

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

Static Methods

get(ConfigurableEnvironment environment, {BeanIntrospector? introspector}) → Binder
A binder that reads environment, with the environment's conversion service; the same as the constructor, under Spring Boot's name.