Lazy class

Delays the creation of a bean, or of what an injection point receives, until it is first used.

On a bean class or @Bean method, the singleton is created on the first lookup or injection instead of at startup: use it for expensive beans that many runs never need. On a configuration class it applies to all its @Bean methods.

On an injection point (constructor parameter, field, setter) a generated proxy is injected instead of the bean; it finds the bean on every call. This breaks dependency cycles and lets a bean call itself through its AOP proxy, so that its advice applies (a call through this skips it). The bean must still exist and be unique, which is checked at startup. The type must be implementable from generated code (an interface, or a class without private members; not generic) and the point must not be nullable; use ObjectProvider<T> otherwise.

@Component()
@Lazy()
class ReportGenerator { ... } // created on first getBean/injection

// Calls through _self go through the proxy, so @Cacheable applies.
@Service()
class BookService implements Books {
  BookService(@Lazy() this._self);
  final Books _self;

  List<Book> findAll(List<String> isbns) =>
      [for (final isbn in isbns) _self.find(isbn)];
}

Spring (Java):

@Component
@Lazy
class ReportGenerator { ... }

@Service
class BookService {
  @Lazy @Autowired private BookService self;
}

Constructors

Lazy([bool value = true])
Makes the bean or injection point lazy; @Lazy(false) makes a bean eager where its configuration class or scan would make it lazy.
const

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
value → bool
Whether the bean (or the injection point) is lazy.
final

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