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
nameexists here or in a parent (&nameonly for a factory bean). Creates nothing. -
containsBeanOfType(
Type type, {String? qualifier, bool includeAncestors = true}) → bool -
Whether injecting
typewould find a bean, without creating any. -
findBean<
T extends Object> ({String? qualifier}) → T? -
Like getBean, but returns
nullwhen no bean matches, for a collaborator the caller can do without. Still throwsNoUniqueBeanDefinitionExceptionwhen 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 whennameis 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. Withqualifier, only the bean with that name, alias or@Qualifiervalue. -
getBeanByName(
String name) → Object -
The bean registered under
nameor 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
Tthat 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 aSmartInstantiationAwareBeanPostProcessorpredicts), the product type for a factory bean (&name: the factory's class), the runtime type of an object added withregisterSingleton.nullwhen no bean has that name. -
isPrototype(
String name) → bool -
Whether every lookup of the bean named
namecreates a new instance. For aFactoryBean, whether its product is created per lookup (this creates the factory);&nameasks about the factory itself. A bean in a custom scope is neither a singleton nor a prototype. ThrowsNoSuchBeanDefinitionExceptionfor an unknown name. -
isSingleton(
String name) → bool -
Whether the bean named
nameis one shared instance. For aFactoryBean, whether its product is shared (this creates the factory);&nameasks about the factory itself. ThrowsNoSuchBeanDefinitionExceptionfor an unknown name. -
isTypeMatch(
String name, Type type) → bool -
Whether the bean named
namecan be used as atype, 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 aFactoryBeanitself instead of the object it produces.