Type conversion

Type conversion turns a value of one type into another, mostly the text of a config file into the type a bean wants.

Why use it

Settings are text: server.port=8080, timeout=10s, max-upload=10MB. Beans want an int, a Duration, a DataSize, an enum or a list. A conversion service does this for @Value, Environment.getPropertyAs<T> and @ConfigurationProperties binding, so you rarely call it yourself.

You deal with conversion directly in two cases: to teach the framework a type of your own (a conversionService bean), or to convert values in your own code.

Example

# lib/resources/application.properties
shop.timeout=30s
shop.max-upload=10MB
shop.ports=8080,8081
import 'package:spring_core_dart/spring_core_dart.dart';

@Service()
class Uploads {
  Uploads(
    @Value(r'${shop.timeout}') this.timeout, // Duration(seconds: 30)
    @Value(r'${shop.max-upload}') this.maxUpload, // 10485760 bytes
    @Value(r'${shop.ports}') this.ports, // [8080, 8081]
  );

  final Duration timeout;
  final DataSize maxUpload;
  final List<int> ports;
}

Pages

See also