BeanFactory class abstract interface

The read side of the container: it hands out beans by type or by name and answers questions about them without creating them.

Inside beans, constructor injection is the normal way to get other beans. You ask a BeanFactory directly when injection cannot help: in main after the context has started, in a test, or in a class that only learns at run time which bean it needs. The application context is a BeanFactory, and a bean can have one injected.

Lookups by type (getBean, findBean) can be narrowed by a qualifier: a bean name, an alias or a @Qualifier value. Factories form a hierarchy: what is not found here is looked up in the parent.

// main: start the context, then take the entry points out of it.
final context = ApplicationContext.run([appBeans]);
final mailer = context.getBean<Mailer>();
final smtp = context.getBean<Transport>(qualifier: 'smtp');
final audit = context.findBean<Audit>(); // null when absent
final raw = context.getBeanByName('mailer') as Mailer;

Spring (Java):

Mailer mailer = context.getBean(Mailer.class);
Transport smtp = context.getBean("smtp", Transport.class);
Audit audit = context.getBeanProvider(Audit.class).getIfAvailable();
Mailer raw = (Mailer) context.getBean("mailer");

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

containsBean(String name) → bool
Whether a bean or alias named name exists here or in a parent (&name only for a factory bean). Creates nothing.
containsBeanOfType(Type type, {String? qualifier, bool includeAncestors = true}) → bool
Whether injecting type would find a bean, without creating any.
findBean<T extends Object>({String? qualifier}) → T?
Like getBean, but returns null when no bean matches, for a collaborator the caller can do without. Still throws NoUniqueBeanDefinitionException when several match and none wins. Spring: getBeanProvider(T).getIfAvailable().
getAliases(String name) → List<String>
The other names of the bean named name: its aliases, plus its bean name when name is itself an alias. Empty when there are none or no bean has that name.
getBean<T extends Object>({String? qualifier}) → T
The bean of type T, created if it does not exist yet. With qualifier, only the bean with that name, alias or @Qualifier value.
getBeanByName(String name) → Object
The bean registered under name or one of its aliases, created if needed; looks in the parent when the name is not known here.
getBeanProvider<T extends Object>({String? qualifier}) → ObjectProvider<T>
A handle on the beans of T that creates nothing until it is used.
getType(String name) → Type?
The type of the bean named name, without creating it: the declared class (or what a SmartInstantiationAwareBeanPostProcessor predicts), the product type for a factory bean (&name: the factory's class), the runtime type of an object added with registerSingleton. null when no bean has that name.
isPrototype(String name) → bool
Whether every lookup of the bean named name creates a new instance. For a FactoryBean, whether its product is created per lookup (this creates the factory); &name asks about the factory itself. A bean in a custom scope is neither a singleton nor a prototype. Throws NoSuchBeanDefinitionException for an unknown name.
isSingleton(String name) → bool
Whether the bean named name is one shared instance. For a FactoryBean, whether its product is shared (this creates the factory); &name asks about the factory itself. Throws NoSuchBeanDefinitionException for an unknown name.
isTypeMatch(String name, Type type) → bool
Whether the bean named name can be used as a type, judged from the types its definition declares, without creating it.
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

Constants

factoryBeanPrefix → const String
Put before a bean name (&client) to get a FactoryBean itself instead of the object it produces.