Autowired class

Asks the container to inject beans into a field, setter, method or constructor.

Constructor parameters of a bean are injected without it. Use it:

  • on a constructor, to choose it when the class has several. Several constructors may be @Autowired(required: false): the one with the most parameters whose beans exist is used;
  • on a field or setter, to assign it after construction. Fields must be public and settable (late final or non-final);
  • on a method, to call it after construction with injected arguments.

A nullable type makes the dependency optional. List<T>, Set<T> and Map<String, T> receive all beans of T (ordered); ObjectProvider<T> receives a handle that looks the bean up later.

@Service()
class Checkout {
  @Autowired()
  late final PaymentGateway gateway;

  @Autowired()
  late final List<DiscountRule> rules; // every DiscountRule bean

  @Autowired()
  void init(Clock clock) => startedAt = clock.now();

  DateTime? startedAt;
}

Spring (Java):

@Service
class Checkout {
  @Autowired PaymentGateway gateway;
  @Autowired List<DiscountRule> rules;

  @Autowired
  void init(Clock clock) { startedAt = clock.now(); }
}

Constructors

Autowired({bool required = true})
Marks the injection point; required only matters on constructors.
const

Properties

hashCode → int
The hash code for this object.
no setterinherited
required → bool
On a constructor, false makes it one of several candidates: the container calls the one with the most parameters it can satisfy. On fields, setters and methods a nullable type makes a dependency optional instead (required: false there fails the build).
final
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
toString() → String
A string representation of this object.
inherited

Operators

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