BeanDefinition<T extends Object> class final

The recipe for one bean: its name, the types it can be found by, its scope and flags, and the closures that create, inject, initialize and destroy it.

The container never creates objects by reflection; it follows these recipes. create calls the constructor with dependencies taken from the DependencyResolver, and the other closures call the annotated methods. The generator writes one definition per @Component class and @Bean method into the *.spring.dart file, so applications usually never write one. You write one by hand for a bean the generator cannot see (a class of a package you do not scan, a bean built from settings), in a BeanModule, a BeanDefinitionRegistryPostProcessor or a container used without a context. A BeanFactoryPostProcessor may change the mutable fields (scope, lazy, primary, ...) before the beans are created.

The container calls the closures in this order: create → inject (fields, setters, methods) → aware callbacks → postProcessBeforeInitialization → postConstruct → InitializingBean.afterPropertiesSet → initMethod → postProcessAfterInitialization. On destruction: DestructionAwareBeanPostProcessors → preDestroy → DisposableBean.destroy → destroyMethod.

// A mailer from a package outside the scan, registered by hand. It can
// be injected as a Mailer or a Notifier and wins over other Notifiers.
final definition = BeanDefinition<Mailer>(
  name: 'mailer',
  types: const [Mailer, Notifier],
  primary: true,
  create: (r) => Mailer(r.dependency<SmtpClient>()),
  inject: (bean, r) => bean.clock = r.optionalDependency<Clock>(),
  postConstruct: (bean) => bean.connect(),
  preDestroy: (bean) => bean.disconnect(),
);
factory.registerBeanDefinition(definition);

Spring (Java):

RootBeanDefinition definition = new RootBeanDefinition(Mailer.class,
    () -> new Mailer(factory.getBean(SmtpClient.class)));
definition.setPrimary(true);
definition.setInitMethodName("connect");
definition.setDestroyMethodName("disconnect");
factory.registerBeanDefinition("mailer", definition);

Differences from Spring: types lists every type the bean can be looked up by (Dart has no runtime subtype checks on Type), and the definition is generic in the bean class.

Constructors

BeanDefinition({required String name, required T? create(DependencyResolver resolver), void inject(T bean, DependencyResolver resolver)?, FutureOr<void> postConstruct(T bean)?, FutureOr<void> initMethod(T bean)?, FutureOr<void> preDestroy(T bean)?, FutureOr<void> destroyMethod(T bean)?, T scopedProxy(T target())?, List<Type>? types, String scope = ScopeType.singleton, bool lazy = false, bool primary = false, bool fallback = false, bool autowireCandidate = true, bool defaultCandidate = true, int? order, String? description, String? source, String? factoryBeanName, String? factoryMethodName, String? initMethodName, String? destroyMethodName, Set<String> qualifiers = const {}, List<String> aliases = const [], List<String> dependsOn = const [], List<Dependency> dependencies = const [], List<Condition> conditions = const [], List<EventListenerMethod<T>> eventListeners = const [], List<BeanMethod<T>> annotatedMethods = const [], List<Object> annotations = const [], List<Type>? factoryObjectTypes, AopProxyFactory? aopProxy, AspectMetadata<T>? aspect})
A definition named name whose instances create makes. The closures, all optional except create:

Properties

aliases → List<String>
More names for the bean; each also works as a qualifier.
final
annotatedMethods → List<BeanMethod<T>>
Methods carrying an annotation marked with @BeanMethodAnnotation() (e.g. @Scheduled), with all their annotations and a closure that calls them; written by the generator.
final
annotations → List<Object>
The annotations of the bean class (for a @Bean method: of the method, then of the returned class), written by the generator for findAnnotationOnBean and getBeansWithAnnotation. Spring reads them by reflection.
final
aopProxy → AopProxyFactory?
Creates the generated AOP proxy of the bean, when the generator could write one; used when an aspect applies to the bean.
final
aspect → AspectMetadata<T>?
The advice and pointcuts of the bean, when its class is an @Aspect.
final
autowireCandidate ↔ bool
Whether the bean can be injected at all (@Bean(autowireCandidate:)), even through a qualifier. When false it is never injected, but lookups by name, getBean<T>(qualifier: name), and getBean<T>() when it is the only bean of T still return it.
getter/setter pair
beanType → Type
The bean's class, the type argument T.
no setter
conditions → List<Condition>
The bean is registered only when all of these match (@Profile, @Conditional...).
final
defaultCandidate ↔ bool
Whether the bean is injected by its plain type (Spring 6.2's @Bean(defaultCandidate: false)). When false it is injected only where a qualifier selects it (bean name, alias or @Qualifier), and @ConditionalOnBean / @ConditionalOnMissingBean ignore it.
getter/setter pair
dependencies → List<Dependency>
The bean's injection points, which validateBeanGraph checks before anything is created; empty in a hand-written definition that does not list them.
final
dependsOn → List<String>
The names of the beans that must be created before this one and destroyed after it (@DependsOn), for an order that no injection point expresses.
final
description ↔ String?
A human-readable description (@Description), for tools and logs.
getter/setter pair
destroyMethodName → String?
Name of the custom destroy method (@Bean(destroyMethod:)), or inferMethod when it was inferred. An inferred method, or one named destroy, is not called for a DisposableBean (its destroy() runs once).
final
eventListeners → List<EventListenerMethod<T>>
The bean's @EventListener methods (and its ApplicationListener implementation), which the application context subscribes to events.
final
factoryBeanName → String?
For a @Bean instance method: the name of the configuration bean that declares it, like Spring's getFactoryBeanName(). That bean is not given this one, in a single injection point or a collection, unless nothing else matches (Spring's self-reference rule: creating this bean needs the configuration first).
final
factoryMethodName → String?
For a @Bean method: the method's name, like Spring's getFactoryMethodName(). Its presence also means order came from the method.
final
factoryObjectTypes → List<Type>?
For a FactoryBean: its product type and the product's supertypes. Lookups by these types (and by name) return the product. null for any other bean.
final
fallback ↔ bool
Whether this bean is used only when no other candidate matches (@Fallback()).
getter/setter pair
hasDestroyCallbacks → bool
Whether the definition has destroy callbacks of its own (preDestroy or destroyMethod).
no setter
hashCode → int
The hash code for this object.
no setterinherited
hasScopedProxy → bool
Whether lookups and injection get a scoped proxy instead of the instance (see createScopedProxy); never for singletons.
no setter
initMethodName → String?
Name of the custom init method (@Bean(initMethod:)); an init method named afterPropertiesSet is not called again for an InitializingBean.
final
isFactoryBean → bool
Whether the bean is a FactoryBean (it has factoryObjectTypes).
no setter
isPrototype → bool
Whether scope is ScopeType.prototype.
no setter
isSingleton → bool
Whether scope is ScopeType.singleton.
no setter
lazy ↔ bool
Whether a singleton is created on first use instead of at startup (@Lazy()). Defaults to false.
getter/setter pair
name → String
The bean's name, unique within its factory. The generator uses the class name in lower camel case, or the @Bean method's name.
final
order ↔ int?
The @Order value. For a @Bean definition (factoryMethodName set) it is the @Order of the method and beats Ordered.order, like in Spring; otherwise it is the class's @Order, used only when the bean is not Ordered itself.
getter/setter pair
primary ↔ bool
Whether this bean wins when several beans match a type (@Primary()).
getter/setter pair
qualifiers → Set<String>
The bean's @Qualifier values: extra names that select it at a qualified injection point or lookup, besides its name and aliases.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
scope ↔ String
The scope name: ScopeType.singleton (the default), ScopeType.prototype or the name of a registered custom scope. A BeanFactoryPostProcessor may change it.
getter/setter pair
source → String?
Where the definition comes from (e.g. package:x/y.dart#Foo). Registering a definition with the same name and the same source again is ignored, so a module imported twice does no harm.
final
types → List<Type>
The types the bean can be looked up and injected by: its class and every supertype it should be found as. Dart cannot ask at run time whether one Type is a subtype of another, so each one must be listed. Defaults to [T].
final

Methods

createScopedProxy(Object target()) → Object
Creates the scoped proxy: an object of the bean's type that forwards every call to the instance target returns at that moment. The container calls it once per bean.
instantiate(DependencyResolver resolver) → Object
Calls create and returns the new instance, or a NullBean when create returned null. The container calls it; other code looks beans up instead.
invokeDestroyMethod(Object bean) → FutureOr<void>
Runs the custom destroy method; a Future when it is asynchronous.
invokeInitMethod(Object bean) → FutureOr<void>
Runs the custom init method; a Future when it is asynchronous.
invokePostConstruct(Object bean) → FutureOr<void>
Runs @PostConstruct methods; a Future when they are asynchronous.
invokePreDestroy(Object bean) → FutureOr<void>
Runs @PreDestroy methods; a Future when they are asynchronous.
isProductType(Type type) → bool
Whether lookups by type return the product of a factory bean.
isQualifiedBy(String qualifier) → bool
Whether qualifier selects this bean: it is the bean's name, one of its aliases or one of its qualifiers.
matchesType(Type type) → bool
Whether lookups by type match this definition. Every bean is an Object.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
populate(Object bean, DependencyResolver resolver) → void
Runs the inject callback on bean: field, setter and method injection.
toString() → String
A string representation of this object.
override

Operators

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

Constants

inferMethod → const String
The destroyMethodName of a definition whose destroy method was not given but found by its name (close, shutdown or dispose), like Spring's AbstractBeanDefinition.INFER_METHOD. An inferred method is not called for a DisposableBean, whose destroy() runs instead.