java.lang.Object
   ↳ org.springframework.core.SimpleAliasRegistry
     ↳ org.springframework.beans.factory.support.DefaultSingletonBeanRegistry
       ↳ org.springframework.beans.factory.support.FactoryBeanRegistrySupport
         ↳ org.springframework.beans.factory.support.AbstractBeanFactory
           ↳ org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
             ↳ org.springframework.beans.factory.support.DefaultListableBeanFactory
Known Direct Subclasses

Class Overview

Default implementation of the ListableBeanFactory and BeanDefinitionRegistry interfaces: a full-fledged bean factory based on bean definition objects.

Typical usage is registering all bean definitions first (possibly read from a bean definition file), before accessing beans. Bean definition lookup is therefore an inexpensive operation in a local bean definition table, operating on pre-built bean definition metadata objects.

Can be used as a standalone bean factory, or as a superclass for custom bean factories. Note that readers for specific bean definition formats are typically implemented separately rather than as bean factory subclasses: see for example PropertiesBeanDefinitionReader and XmlBeanDefinitionReader.

For an alternative implementation of the ListableBeanFactory interface, have a look at StaticListableBeanFactory, which manages existing bean instances rather than creating new ones based on bean definitions.

Summary

[Expand]
Inherited Constants
From interface org.springframework.beans.factory.BeanFactory
From interface org.springframework.beans.factory.config.AutowireCapableBeanFactory
From interface org.springframework.beans.factory.config.ConfigurableBeanFactory
[Expand]
Inherited Fields
From class org.springframework.beans.factory.support.DefaultSingletonBeanRegistry
Public Constructors
DefaultListableBeanFactory()
Create a new DefaultListableBeanFactory.
DefaultListableBeanFactory(BeanFactory parentBeanFactory)
Create a new DefaultListableBeanFactory with the given parent.
Public Methods
boolean containsBeanDefinition(String beanName)
Check if this bean factory contains a bean definition with the given name.
void copyConfigurationFrom(ConfigurableBeanFactory otherFactory)
Copy all relevant configuration from the given other factory.
<A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
Find a Annotation of annotationType on the specified bean, traversing its interfaces and super classes if no annotation can be found on the given class itself, as well as checking its raw bean class if not found on the exposed bean reference (e.g.
void freezeConfiguration()
Freeze all bean definitions, signalling that the registered bean definitions will not be modified or post-processed any further.
AutowireCandidateResolver getAutowireCandidateResolver()
Return the autowire candidate resolver for this BeanFactory (never null).
<T> T getBean(Class<T> requiredType)
Return the bean instance that uniquely matches the given object type, if any.
BeanDefinition getBeanDefinition(String beanName)
Return the bean definition for the given bean name.
int getBeanDefinitionCount()
Return the number of beans defined in the factory.
String[] getBeanDefinitionNames()
Return the names of all beans defined in this factory.
String[] getBeanNamesForType(Class type, boolean includeNonSingletons, boolean allowEagerInit)
String[] getBeanNamesForType(Class type)
<T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
Return the bean instances that match the given object type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans.
<T> Map<String, T> getBeansOfType(Class<T> type)
Return the bean instances that match the given object type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans.
Map<StringObject> getBeansWithAnnotation(Class<? extends Annotation> annotationType)
Find all beans whose Class has the supplied Annotation type.
boolean isAutowireCandidate(String beanName, DependencyDescriptor descriptor)
Determine whether the specified bean qualifies as an autowire candidate, to be injected into other beans which declare a dependency of matching type.
boolean isConfigurationFrozen()
Return whether this factory's bean definitions are frozen, i.e.
void preInstantiateSingletons()
Ensure that all non-lazy-init singletons are instantiated, also considering FactoryBeans.
void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
Register a new bean definition with this registry.
void registerResolvableDependency(Class<?> dependencyType, Object autowiredValue)
Register a special dependency type with corresponding autowired value.
void removeBeanDefinition(String beanName)
Remove the BeanDefinition for the given name.
Object resolveDependency(DependencyDescriptor descriptor, String beanName, Set<String> autowiredBeanNames, TypeConverter typeConverter)
Resolve the specified dependency against the beans defined in this factory.
void setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding)
Set whether it should be allowed to override bean definitions by registering a different definition with the same name, automatically replacing the former.
void setAllowEagerClassLoading(boolean allowEagerClassLoading)
Set whether the factory is allowed to eagerly load bean classes even for bean definitions that are marked as "lazy-init".
void setAutowireCandidateResolver(AutowireCandidateResolver autowireCandidateResolver)
Set a custom autowire candidate resolver for this BeanFactory to use when deciding whether a bean definition should be considered as a candidate for autowiring.
void setSerializationId(String serializationId)
Specify an id for serialization purposes, allowing this BeanFactory to be deserialized from this id back into the BeanFactory object, if needed.
String toString()
Protected Methods
boolean allowAliasOverriding()
Only allows alias overriding if bean definition overriding is allowed.
String determinePrimaryCandidate(Map<StringObject> candidateBeans, DependencyDescriptor descriptor)
Determine the primary autowire candidate in the given set of beans.
Object doResolveDependency(DependencyDescriptor descriptor, Class<?> type, String beanName, Set<String> autowiredBeanNames, TypeConverter typeConverter)
Map<StringObject> findAutowireCandidates(String beanName, Class requiredType, DependencyDescriptor descriptor)
Find bean instances that match the required type.
boolean isAutowireCandidate(String beanName, RootBeanDefinition mbd, DependencyDescriptor descriptor)
Determine whether the specified bean definition qualifies as an autowire candidate, to be injected into other beans which declare a dependency of matching type.
boolean isBeanEligibleForMetadataCaching(String beanName)
Considers all beans as eligible for metdata caching if the factory's configuration has been marked as frozen.
boolean isPrimary(String beanName, Object beanInstance)
Return whether the bean definition for the given bean name has been marked as a primary bean.
boolean matchesBeanName(String beanName, String candidateName)
Determine whether the given candidate name matches the bean name or the aliases stored in this bean definition.
void resetBeanDefinition(String beanName)
Reset all bean definition caches for the given bean, including the caches of beans that are derived from it.
Object writeReplace()
[Expand]
Inherited Methods
From class org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
From class org.springframework.beans.factory.support.AbstractBeanFactory
From class org.springframework.beans.factory.support.FactoryBeanRegistrySupport
From class org.springframework.beans.factory.support.DefaultSingletonBeanRegistry
From class org.springframework.core.SimpleAliasRegistry
From class java.lang.Object
From interface org.springframework.beans.factory.BeanFactory
From interface org.springframework.beans.factory.HierarchicalBeanFactory
From interface org.springframework.beans.factory.ListableBeanFactory
From interface org.springframework.beans.factory.config.AutowireCapableBeanFactory
From interface org.springframework.beans.factory.config.ConfigurableBeanFactory
From interface org.springframework.beans.factory.config.ConfigurableListableBeanFactory
From interface org.springframework.beans.factory.config.SingletonBeanRegistry
From interface org.springframework.beans.factory.support.BeanDefinitionRegistry
From interface org.springframework.core.AliasRegistry

Public Constructors

public DefaultListableBeanFactory ()

Also: SpringBeans

Create a new DefaultListableBeanFactory.

public DefaultListableBeanFactory (BeanFactory parentBeanFactory)

Also: SpringBeans

Create a new DefaultListableBeanFactory with the given parent.

Parameters
parentBeanFactory the parent BeanFactory

Public Methods

public boolean containsBeanDefinition (String beanName)

Also: SpringBeans

Check if this bean factory contains a bean definition with the given name. Does not consider any hierarchy this factory may participate in. Invoked by containsBean when no cached singleton instance is found.

Depending on the nature of the concrete bean factory implementation, this operation might be expensive (for example, because of directory lookups in external registries). However, for listable bean factories, this usually just amounts to a local hash lookup: The operation is therefore part of the public interface there. The same implementation can serve for both this template method and the public interface method in that case.

Parameters
beanName the name of the bean to look for
Returns
  • if this bean factory contains a bean definition with the given name

public void copyConfigurationFrom (ConfigurableBeanFactory otherFactory)

Also: SpringBeans

Copy all relevant configuration from the given other factory.

Should include all standard configuration settings as well as BeanPostProcessors, Scopes, and factory-specific internal settings. Should not include any metadata of actual bean definitions, such as BeanDefinition objects and bean name aliases.

Parameters
otherFactory the other BeanFactory to copy from

public A findAnnotationOnBean (String beanName, Class<A> annotationType)

Also: SpringBeans

Find a Annotation of annotationType on the specified bean, traversing its interfaces and super classes if no annotation can be found on the given class itself, as well as checking its raw bean class if not found on the exposed bean reference (e.g. in case of a proxy).

Parameters
beanName the name of the bean to look for annotations on
annotationType the annotation class to look for
Returns
  • the annotation of the given type found, or null

public void freezeConfiguration ()

Also: SpringBeans

Freeze all bean definitions, signalling that the registered bean definitions will not be modified or post-processed any further.

This allows the factory to aggressively cache bean definition metadata.

public AutowireCandidateResolver getAutowireCandidateResolver ()

Also: SpringBeans

Return the autowire candidate resolver for this BeanFactory (never null).

public T getBean (Class<T> requiredType)

Also: SpringBeans

Return the bean instance that uniquely matches the given object type, if any.

Parameters
requiredType type the bean must match; can be an interface or superclass. null is disallowed.

This method goes into ListableBeanFactory by-type lookup territory but may also be translated into a conventional by-name lookup based on the name of the given type. For more extensive retrieval operations across sets of beans, use ListableBeanFactory and/or BeanFactoryUtils.

Returns
  • an instance of the single bean matching the required type

public BeanDefinition getBeanDefinition (String beanName)

Also: SpringBeans

Return the bean definition for the given bean name. Subclasses should normally implement caching, as this method is invoked by this class every time bean definition metadata is needed.

Depending on the nature of the concrete bean factory implementation, this operation might be expensive (for example, because of directory lookups in external registries). However, for listable bean factories, this usually just amounts to a local hash lookup: The operation is therefore part of the public interface there. The same implementation can serve for both this template method and the public interface method in that case.

Parameters
beanName the name of the bean to find a definition for
Returns
  • the BeanDefinition for this prototype name (never null)

public int getBeanDefinitionCount ()

Also: SpringBeans

Return the number of beans defined in the factory.

Does not consider any hierarchy this factory may participate in, and ignores any singleton beans that have been registered by other means than bean definitions.

Returns
  • the number of beans defined in the factory

public String[] getBeanDefinitionNames ()

Also: SpringBeans

Return the names of all beans defined in this factory.

Does not consider any hierarchy this factory may participate in, and ignores any singleton beans that have been registered by other means than bean definitions.

Returns
  • the names of all beans defined in this factory, or an empty array if none defined

public String[] getBeanNamesForType (Class type, boolean includeNonSingletons, boolean allowEagerInit)

Also: SpringBeans

public String[] getBeanNamesForType (Class type)

Also: SpringBeans

public Map<String, T> getBeansOfType (Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)

Also: SpringBeans

Return the bean instances that match the given object type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans.

NOTE: This method introspects top-level beans only. It does not check nested beans which might match the specified type as well.

Does consider objects created by FactoryBeans if the "allowEagerInit" flag is set, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type. If "allowEagerInit" is not set, only raw FactoryBeans will be checked (which doesn't require initialization of each FactoryBean).

Does not consider any hierarchy this factory may participate in. Use BeanFactoryUtils' beansOfTypeIncludingAncestors to include beans in ancestor factories too.

Note: Does not ignore singleton beans that have been registered by other means than bean definitions.

The Map returned by this method should always return bean names and corresponding bean instances in the order of definition in the backend configuration, as far as possible.

Parameters
type the class or interface to match, or null for all concrete beans
includeNonSingletons whether to include prototype or scoped beans too or just singletons (also applies to FactoryBeans)
allowEagerInit whether to initialize lazy-init singletons and objects created by FactoryBeans (or by factory methods with a "factory-bean" reference) for the type check. Note that FactoryBeans need to be eagerly initialized to determine their type: So be aware that passing in "true" for this flag will initialize FactoryBeans and "factory-bean" references.
Returns
  • a Map with the matching beans, containing the bean names as keys and the corresponding bean instances as values

public Map<String, T> getBeansOfType (Class<T> type)

Also: SpringBeans

Return the bean instances that match the given object type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans.

NOTE: This method introspects top-level beans only. It does not check nested beans which might match the specified type as well.

Does consider objects created by FactoryBeans, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type.

Does not consider any hierarchy this factory may participate in. Use BeanFactoryUtils' beansOfTypeIncludingAncestors to include beans in ancestor factories too.

Note: Does not ignore singleton beans that have been registered by other means than bean definitions.

This version of getBeansOfType matches all kinds of beans, be it singletons, prototypes, or FactoryBeans. In most implementations, the result will be the same as for getBeansOfType(type, true, true).

The Map returned by this method should always return bean names and corresponding bean instances in the order of definition in the backend configuration, as far as possible.

Parameters
type the class or interface to match, or null for all concrete beans
Returns
  • a Map with the matching beans, containing the bean names as keys and the corresponding bean instances as values

public Map<StringObject> getBeansWithAnnotation (Class<? extends Annotation> annotationType)

Also: SpringBeans

Find all beans whose Class has the supplied Annotation type.

Parameters
annotationType the type of annotation to look for
Returns
  • a Map with the matching beans, containing the bean names as keys and the corresponding bean instances as values

public boolean isAutowireCandidate (String beanName, DependencyDescriptor descriptor)

Also: SpringBeans

Determine whether the specified bean qualifies as an autowire candidate, to be injected into other beans which declare a dependency of matching type.

This method checks ancestor factories as well.

Parameters
beanName the name of the bean to check
descriptor the descriptor of the dependency to resolve
Returns
  • whether the bean should be considered as autowire candidate

public boolean isConfigurationFrozen ()

Also: SpringBeans

Return whether this factory's bean definitions are frozen, i.e. are not supposed to be modified or post-processed any further.

Returns
  • true if the factory's configuration is considered frozen

public void preInstantiateSingletons ()

Also: SpringBeans

Ensure that all non-lazy-init singletons are instantiated, also considering FactoryBeans. Typically invoked at the end of factory setup, if desired.

public void registerBeanDefinition (String beanName, BeanDefinition beanDefinition)

Also: SpringBeans

Register a new bean definition with this registry. Must support RootBeanDefinition and ChildBeanDefinition.

Parameters
beanName the name of the bean instance to register
beanDefinition definition of the bean instance to register

public void registerResolvableDependency (Class<?> dependencyType, Object autowiredValue)

Also: SpringBeans

Register a special dependency type with corresponding autowired value.

This is intended for factory/context references that are supposed to be autowirable but are not defined as beans in the factory: e.g. a dependency of type ApplicationContext resolved to the ApplicationContext instance that the bean is living in.

Note: There are no such default types registered in a plain BeanFactory, not even for the BeanFactory interface itself.

Parameters
dependencyType the dependency type to register. This will typically be a base interface such as BeanFactory, with extensions of it resolved as well if declared as an autowiring dependency (e.g. ListableBeanFactory), as long as the given value actually implements the extended interface.
autowiredValue the corresponding autowired value. This may also be an implementation of the ObjectFactory interface, which allows for lazy resolution of the actual target value.

public void removeBeanDefinition (String beanName)

Also: SpringBeans

Remove the BeanDefinition for the given name.

Parameters
beanName the name of the bean instance to register

public Object resolveDependency (DependencyDescriptor descriptor, String beanName, Set<String> autowiredBeanNames, TypeConverter typeConverter)

Also: SpringBeans

Resolve the specified dependency against the beans defined in this factory.

Parameters
descriptor the descriptor for the dependency
beanName the name of the bean which declares the present dependency
autowiredBeanNames a Set that all names of autowired beans (used for resolving the present dependency) are supposed to be added to
typeConverter the TypeConverter to use for populating arrays and collections
Returns
  • the resolved object, or null if none found

public void setAllowBeanDefinitionOverriding (boolean allowBeanDefinitionOverriding)

Also: SpringBeans

Set whether it should be allowed to override bean definitions by registering a different definition with the same name, automatically replacing the former. If not, an exception will be thrown. This also applies to overriding aliases.

Default is "true".

public void setAllowEagerClassLoading (boolean allowEagerClassLoading)

Also: SpringBeans

Set whether the factory is allowed to eagerly load bean classes even for bean definitions that are marked as "lazy-init".

Default is "true". Turn this flag off to suppress class loading for lazy-init beans unless such a bean is explicitly requested. In particular, by-type lookups will then simply ignore bean definitions without resolved class name, instead of loading the bean classes on demand just to perform a type check.

public void setAutowireCandidateResolver (AutowireCandidateResolver autowireCandidateResolver)

Also: SpringBeans

Set a custom autowire candidate resolver for this BeanFactory to use when deciding whether a bean definition should be considered as a candidate for autowiring.

public void setSerializationId (String serializationId)

Also: SpringBeans

Specify an id for serialization purposes, allowing this BeanFactory to be deserialized from this id back into the BeanFactory object, if needed.

public String toString ()

Also: SpringBeans

Protected Methods

protected boolean allowAliasOverriding ()

Also: SpringBeans

Only allows alias overriding if bean definition overriding is allowed.

protected String determinePrimaryCandidate (Map<StringObject> candidateBeans, DependencyDescriptor descriptor)

Also: SpringBeans

Determine the primary autowire candidate in the given set of beans.

Parameters
candidateBeans a Map of candidate names and candidate instances that match the required type, as returned by findAutowireCandidates(String, Class, DependencyDescriptor)
descriptor the target dependency to match against
Returns
  • the name of the primary candidate, or null if none found

protected Object doResolveDependency (DependencyDescriptor descriptor, Class<?> type, String beanName, Set<String> autowiredBeanNames, TypeConverter typeConverter)

Also: SpringBeans

protected Map<StringObject> findAutowireCandidates (String beanName, Class requiredType, DependencyDescriptor descriptor)

Also: SpringBeans

Find bean instances that match the required type. Called during autowiring for the specified bean.

Parameters
beanName the name of the bean that is about to be wired
requiredType the actual type of bean to look for (may be an array component type or collection element type)
descriptor the descriptor of the dependency to resolve
Returns
  • a Map of candidate names and candidate instances that match the required type (never null)
Throws
BeansException in case of errors

protected boolean isAutowireCandidate (String beanName, RootBeanDefinition mbd, DependencyDescriptor descriptor)

Also: SpringBeans

Determine whether the specified bean definition qualifies as an autowire candidate, to be injected into other beans which declare a dependency of matching type.

Parameters
beanName the name of the bean definition to check
mbd the merged bean definition to check
descriptor the descriptor of the dependency to resolve
Returns
  • whether the bean should be considered as autowire candidate

protected boolean isBeanEligibleForMetadataCaching (String beanName)

Also: SpringBeans

Considers all beans as eligible for metdata caching if the factory's configuration has been marked as frozen.

Parameters
beanName the name of the bean
Returns
  • true if the bean's metadata may be cached at this point already

protected boolean isPrimary (String beanName, Object beanInstance)

Also: SpringBeans

Return whether the bean definition for the given bean name has been marked as a primary bean.

Parameters
beanName the name of the bean
beanInstance the corresponding bean instance
Returns
  • whether the given bean qualifies as primary

protected boolean matchesBeanName (String beanName, String candidateName)

Also: SpringBeans

Determine whether the given candidate name matches the bean name or the aliases stored in this bean definition.

protected void resetBeanDefinition (String beanName)

Also: SpringBeans

Reset all bean definition caches for the given bean, including the caches of beans that are derived from it.

Parameters
beanName the name of the bean to reset

protected Object writeReplace ()

Also: SpringBeans