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< eventListeners = const [], List<T> >BeanMethod< annotatedMethods = const [], List<T> >Object> annotations = const [], List<Type> ? factoryObjectTypes, AopProxyFactory? aopProxy, AspectMetadata<T> ? aspect}) -
A definition named
namewhose instancescreatemakes. The closures, all optional exceptcreate:
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
@Beanmethod: of the method, then of the returned class), written by the generator forfindAnnotationOnBeanandgetBeansWithAnnotation. 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. Whenfalseit is never injected, but lookups by name,getBean<T>(qualifier: name), andgetBean<T>()when it is the only bean ofTstill 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)). Whenfalseit is injected only where a qualifier selects it (bean name, alias or@Qualifier), and@ConditionalOnBean/@ConditionalOnMissingBeanignore it.getter/setter pair -
dependencies
→ List<
Dependency> -
The bean's injection points, which
validateBeanGraphchecks 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 nameddestroy, is not called for aDisposableBean(itsdestroy()runs once).final -
eventListeners
→ List<
EventListenerMethod< T> > -
The bean's
@EventListenermethods (and itsApplicationListenerimplementation), which the application context subscribes to events.final - factoryBeanName → String?
-
For a
@Beaninstance method: the name of the configuration bean that declares it, like Spring'sgetFactoryBeanName(). 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
@Beanmethod: the method's name, like Spring'sgetFactoryMethodName(). 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.nullfor 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 (
preDestroyordestroyMethod).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 namedafterPropertiesSetis not called again for anInitializingBean.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 tofalse.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
@Beanmethod's name.final - order ↔ int?
-
The
@Ordervalue. For a@Beandefinition (factoryMethodName set) it is the@Orderof the method and beatsOrdered.order, like in Spring; otherwise it is the class's@Order, used only when the bean is notOrdereditself.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
@Qualifiervalues: 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
BeanFactoryPostProcessormay 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
Typeis 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
targetreturns at that moment. The container calls it once per bean. -
instantiate(
DependencyResolver resolver) → Object -
Calls
createand returns the new instance, or a NullBean whencreatereturnednull. The container calls it; other code looks beans up instead. -
invokeDestroyMethod(
Object bean) → FutureOr< void> -
Runs the custom destroy method; a
Futurewhen it is asynchronous. -
invokeInitMethod(
Object bean) → FutureOr< void> -
Runs the custom init method; a
Futurewhen it is asynchronous. -
invokePostConstruct(
Object bean) → FutureOr< void> -
Runs
@PostConstructmethods; aFuturewhen they are asynchronous. -
invokePreDestroy(
Object bean) → FutureOr< void> -
Runs
@PreDestroymethods; aFuturewhen they are asynchronous. -
isProductType(
Type type) → bool -
Whether lookups by
typereturn the product of a factory bean. -
isQualifiedBy(
String qualifier) → bool -
Whether
qualifierselects this bean: it is the bean's name, one of its aliases or one of its qualifiers. -
matchesType(
Type type) → bool -
Whether lookups by
typematch this definition. Every bean is anObject. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
populate(
Object bean, DependencyResolver resolver) → void -
Runs the
injectcallback onbean: 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,shutdownordispose), like Spring'sAbstractBeanDefinition.INFER_METHOD. An inferred method is not called for aDisposableBean, whosedestroy()runs instead.