Component class

Marks a class as a bean: an object the container creates once, injects into others and closes at shutdown.

This is how most of an application's objects are declared. The generator finds the annotated classes under a directory scanned by a ComponentScan (or @SpringBootApplication), and the container calls the constructor with the other beans it asks for. You never write new for such a class or pass its dependencies by hand.

The bean name defaults to the class name in lowerCamelCase (AuthService becomes authService; URLService stays as it is). An explicit name must not be empty, blank or padded with spaces. Only classes can be beans: on a mixin, enum, extension or extension type the build fails. The scan skips abstract classes. Service, Repository and Controller work the same way and only say what the class is for; subclass Component to make your own such annotation.

@Component()
class Clock {
  DateTime now() => DateTime.now();
}

// Named explicitly because another library looks it up by name; the
// container passes the Clock bean to the constructor.
@Component('auditLog')
class AuditLog {
  AuditLog(this.clock);
  final Clock clock;
}

Spring (Java):

@Component
class Clock { ... }

@Component("auditLog")
class AuditLog {
  AuditLog(Clock clock) { ... }
}

Constructors

Component([String? name])
Declares the bean; name replaces the default bean name.
const

Properties

hashCode → int
The hash code for this object.
no setterinherited
name → String?
The bean name; null gives the class name in lowerCamelCase.
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