The Spring Framework

org.springframework.beans.factory.support
Class DefaultListableBeanFactory

java.lang.Object
  extended by org.springframework.beans.factory.support.DefaultSingletonBeanRegistry
      extended by org.springframework.beans.factory.support.AbstractBeanFactory
          extended by org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
              extended by org.springframework.beans.factory.support.DefaultListableBeanFactory
All Implemented Interfaces:
BeanFactory, AutowireCapableBeanFactory, ConfigurableBeanFactory, ConfigurableListableBeanFactory, SingletonBeanRegistry, HierarchicalBeanFactory, ListableBeanFactory, BeanDefinitionRegistry
Direct Known Subclasses:
XmlBeanFactory

public class DefaultListableBeanFactory
extends AbstractAutowireCapableBeanFactory
implements ConfigurableListableBeanFactory, BeanDefinitionRegistry

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.

Since:
16 April 2001
Author:
Rod Johnson, Juergen Hoeller
See Also:
StaticListableBeanFactory, PropertiesBeanDefinitionReader, XmlBeanDefinitionReader

Field Summary
 
Fields inherited from class org.springframework.beans.factory.support.DefaultSingletonBeanRegistry
logger
 
Fields inherited from interface org.springframework.beans.factory.config.AutowireCapableBeanFactory
AUTOWIRE_AUTODETECT, AUTOWIRE_BY_NAME, AUTOWIRE_BY_TYPE, AUTOWIRE_CONSTRUCTOR, AUTOWIRE_NO
 
Fields inherited from interface org.springframework.beans.factory.config.ConfigurableBeanFactory
SCOPE_PROTOTYPE, SCOPE_SINGLETON
 
Constructor Summary
DefaultListableBeanFactory()
          Create a new DefaultListableBeanFactory.
DefaultListableBeanFactory(BeanFactory parentBeanFactory)
          Create a new DefaultListableBeanFactory with the given parent.
 
Method Summary
 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.
protected  Map findAutowireCandidates(String beanName, Class requiredType)
          Find bean instances that match the required type.
 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)
          Return the names of beans matching the given type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans.
 String[] getBeanNamesForType(Class type, boolean includePrototypes, boolean allowEagerInit)
          Return the names of beans matching the given type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans.
 Map getBeansOfType(Class 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 getBeansOfType(Class type, boolean includePrototypes, 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.
 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 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".
 String toString()
           
 
Methods inherited from class org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
applyBeanPostProcessorsAfterInitialization, applyBeanPostProcessorsBeforeInitialization, applyBeanPostProcessorsBeforeInstantiation, applyBeanPropertyValues, applyPropertyValues, autowire, autowireBeanProperties, autowireByName, autowireByType, autowireConstructor, checkDependencies, configureBean, createBean, createBean, createBeanInstance, determineConstructorFromBeanPostProcessors, filterPropertyDescriptorsForDependencyCheck, findMatchingBeans, getInstantiationStrategy, getTypeForFactoryBean, getTypeForFactoryMethod, ignoreDependencyInterface, ignoreDependencyType, initializeBean, initializeBean, instantiateBean, instantiateUsingFactoryMethod, invokeCustomInitMethod, invokeInitMethods, isExcludedFromDependencyCheck, populateBean, postProcessObjectFromFactoryBean, predictBeanType, removeSingleton, setAllowCircularReferences, setAllowRawInjectionDespiteWrapping, setInstantiationStrategy, unsatisfiedNonSimpleProperties
 
Methods inherited from class org.springframework.beans.factory.support.AbstractBeanFactory
addBeanPostProcessor, addPropertyEditorRegistrar, afterPrototypeCreation, beforePrototypeCreation, checkMergedBeanDefinition, clearMergedBeanDefinition, containsBean, containsLocalBean, destroyBean, destroyBean, destroyScopedBean, getAliases, getBean, getBean, getBean, getBean, getBeanClassLoader, getBeanPostProcessorCount, getBeanPostProcessors, getCustomEditors, getMergedBeanDefinition, getMergedBeanDefinition, getMergedBeanDefinition, getMergedBeanDefinition, getObjectForBeanInstance, getObjectFromFactoryBean, getParentBeanFactory, getPropertyEditorRegistrars, getRegisteredScope, getRegisteredScopeNames, getType, getTypeConverter, getTypeForFactoryBean, hasDestructionAwareBeanPostProcessors, hasInstantiationAwareBeanPostProcessors, initBeanWrapper, isAlias, isBeanClassMatch, isBeanNameInUse, isCacheBeanMetadata, isCurrentlyInCreation, isFactoryBean, isPrototype, isPrototypeCurrentlyInCreation, isSingleton, isTypeMatch, originalBeanName, registerAlias, registerCustomEditor, registerCustomEditors, registerDisposableBeanIfNecessary, registerScope, requiresDestruction, resolveBeanClass, setBeanClassLoader, setCacheBeanMetadata, setParentBeanFactory, transformedBeanName
 
Methods inherited from class org.springframework.beans.factory.support.DefaultSingletonBeanRegistry
addSingleton, afterSingletonCreation, beforeSingletonCreation, containsSingleton, destroyBean, destroySingleton, destroySingletons, getDependentBeans, getSingleton, getSingleton, getSingletonCount, getSingletonMutex, getSingletonNames, hasDependentBean, isSingletonCurrentlyInCreation, registerDependentBean, registerDisposableBean, registerSingleton
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.springframework.beans.factory.config.ConfigurableListableBeanFactory
ignoreDependencyInterface, ignoreDependencyType
 
Methods inherited from interface org.springframework.beans.factory.config.AutowireCapableBeanFactory
applyBeanPostProcessorsAfterInitialization, applyBeanPostProcessorsBeforeInitialization, applyBeanPropertyValues, autowire, autowireBeanProperties, configureBean, createBean, initializeBean
 
Methods inherited from interface org.springframework.beans.factory.config.ConfigurableBeanFactory
addBeanPostProcessor, addPropertyEditorRegistrar, destroyBean, destroyScopedBean, destroySingletons, getBeanClassLoader, getBeanPostProcessorCount, getRegisteredScope, getRegisteredScopeNames, getTypeConverter, isCacheBeanMetadata, isCurrentlyInCreation, registerAlias, registerCustomEditor, registerScope, setBeanClassLoader, setCacheBeanMetadata, setParentBeanFactory
 
Methods inherited from interface org.springframework.beans.factory.HierarchicalBeanFactory
containsLocalBean, getParentBeanFactory
 
Methods inherited from interface org.springframework.beans.factory.config.SingletonBeanRegistry
containsSingleton, getSingleton, getSingletonCount, getSingletonNames, registerSingleton
 
Methods inherited from interface org.springframework.beans.factory.support.BeanDefinitionRegistry
getAliases, registerAlias
 

Constructor Detail

DefaultListableBeanFactory

public DefaultListableBeanFactory()
Create a new DefaultListableBeanFactory.


DefaultListableBeanFactory

public DefaultListableBeanFactory(BeanFactory parentBeanFactory)
Create a new DefaultListableBeanFactory with the given parent.

Parameters:
parentBeanFactory - the parent BeanFactory
Method Detail

setAllowBeanDefinitionOverriding

public 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. If not, an exception will be thrown. Default is "true".


setAllowEagerClassLoading

public 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".

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.

See Also:
AbstractBeanDefinition.setLazyInit(boolean)

copyConfigurationFrom

public void copyConfigurationFrom(ConfigurableBeanFactory otherFactory)
Description copied from interface: ConfigurableBeanFactory
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.

Specified by:
copyConfigurationFrom in interface ConfigurableBeanFactory
Overrides:
copyConfigurationFrom in class AbstractAutowireCapableBeanFactory
Parameters:
otherFactory - the other BeanFactory to copy from

containsBeanDefinition

public boolean containsBeanDefinition(String beanName)
Description copied from class: AbstractBeanFactory
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.

Specified by:
containsBeanDefinition in interface ListableBeanFactory
Specified by:
containsBeanDefinition in interface BeanDefinitionRegistry
Specified by:
containsBeanDefinition in class AbstractBeanFactory
Parameters:
beanName - the name of the bean to look for
Returns:
if this bean factory contains a bean definition with the given name
See Also:
AbstractBeanFactory.containsBean(java.lang.String), ListableBeanFactory.containsBeanDefinition(java.lang.String)

getBeanDefinitionCount

public int getBeanDefinitionCount()
Description copied from interface: ListableBeanFactory
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.

Specified by:
getBeanDefinitionCount in interface ListableBeanFactory
Specified by:
getBeanDefinitionCount in interface BeanDefinitionRegistry
Returns:
the number of beans defined in the factory

getBeanDefinitionNames

public String[] getBeanDefinitionNames()
Description copied from interface: ListableBeanFactory
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.

Specified by:
getBeanDefinitionNames in interface ListableBeanFactory
Specified by:
getBeanDefinitionNames in interface BeanDefinitionRegistry
Returns:
the names of all beans defined in this factory, or an empty array if none defined

getBeanNamesForType

public String[] getBeanNamesForType(Class type)
Description copied from interface: ListableBeanFactory
Return the names of beans matching the given 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' beanNamesForTypeIncludingAncestors 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 getBeanNamesForType matches all kinds of beans, be it singletons, prototypes, or FactoryBeans. In most implementations, the result will be the same as for getBeanNamesOfType(type, true, true).

Bean names returned by this method should always return bean names in the order of definition in the backend configuration, as far as possible.

Specified by:
getBeanNamesForType in interface ListableBeanFactory
Parameters:
type - the class or interface to match, or null for all bean names
Returns:
the names of beans (or objects created by FactoryBeans) matching the given object type (including subclasses), or an empty array if none
See Also:
FactoryBean.getObjectType(), BeanFactoryUtils.beanNamesForTypeIncludingAncestors(ListableBeanFactory, Class)

getBeanNamesForType

public String[] getBeanNamesForType(Class type,
                                    boolean includePrototypes,
                                    boolean allowEagerInit)
Description copied from interface: ListableBeanFactory
Return the names of beans matching the given 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' beanNamesForTypeIncludingAncestors to include beans in ancestor factories too.

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

Bean names returned by this method should always return bean names in the order of definition in the backend configuration, as far as possible.

Specified by:
getBeanNamesForType in interface ListableBeanFactory
Parameters:
type - the class or interface to match, or null for all bean names
includePrototypes - whether to include prototype 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:
the names of beans (or objects created by FactoryBeans) matching the given object type (including subclasses), or an empty array if none
See Also:
FactoryBean.getObjectType(), BeanFactoryUtils.beanNamesForTypeIncludingAncestors(ListableBeanFactory, Class, boolean, boolean)

getBeansOfType

public Map getBeansOfType(Class type)
                   throws BeansException
Description copied from interface: ListableBeanFactory
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. This will usually mean that either JDK 1.4 or Commons Collections needs to be available.

Specified by:
getBeansOfType in interface ListableBeanFactory
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
Throws:
BeansException - if a bean could not be created
See Also:
FactoryBean.getObjectType(), BeanFactoryUtils.beansOfTypeIncludingAncestors(ListableBeanFactory, Class)

getBeansOfType

public Map getBeansOfType(Class type,
                          boolean includePrototypes,
                          boolean allowEagerInit)
                   throws BeansException
Description copied from interface: ListableBeanFactory
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. This will usually mean that either JDK 1.4 or Commons Collections needs to be available.

Specified by:
getBeansOfType in interface ListableBeanFactory
Parameters:
type - the class or interface to match, or null for all concrete beans
includePrototypes - whether to include prototype 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
Throws:
BeansException - if a bean could not be created
See Also:
FactoryBean.getObjectType(), BeanFactoryUtils.beansOfTypeIncludingAncestors(ListableBeanFactory, Class, boolean, boolean)

preInstantiateSingletons

public void preInstantiateSingletons()
                              throws BeansException
Description copied from interface: ConfigurableListableBeanFactory
Ensure that all non-lazy-init singletons are instantiated, also considering FactoryBeans. Typically invoked at the end of factory setup, if desired.

Specified by:
preInstantiateSingletons in interface ConfigurableListableBeanFactory
Throws:
BeansException - if one of the singleton beans could not be created. Note: This may have left the factory with some beans already initialized! Call ConfigurableBeanFactory.destroySingletons() for full cleanup in this case.
See Also:
ConfigurableBeanFactory.destroySingletons()

registerBeanDefinition

public void registerBeanDefinition(String beanName,
                                   BeanDefinition beanDefinition)
                            throws BeanDefinitionStoreException
Description copied from interface: BeanDefinitionRegistry
Register a new bean definition with this registry. Must support RootBeanDefinition and ChildBeanDefinition.

Specified by:
registerBeanDefinition in interface BeanDefinitionRegistry
Parameters:
beanName - the name of the bean instance to register
beanDefinition - definition of the bean instance to register
Throws:
BeanDefinitionStoreException - if the BeanDefinition is invalid or if there is already a BeanDefinition for the specified bean name (and we are not allowed to override it)
See Also:
RootBeanDefinition, ChildBeanDefinition

getBeanDefinition

public BeanDefinition getBeanDefinition(String beanName)
                                 throws NoSuchBeanDefinitionException
Description copied from class: AbstractBeanFactory
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.

Specified by:
getBeanDefinition in interface ConfigurableListableBeanFactory
Specified by:
getBeanDefinition in interface BeanDefinitionRegistry
Specified by:
getBeanDefinition in class AbstractBeanFactory
Parameters:
beanName - the name of the bean to find a definition for
Returns:
the BeanDefinition for this prototype name (never null)
Throws:
NoSuchBeanDefinitionException - if there is no bean with the given name
See Also:
RootBeanDefinition, ChildBeanDefinition, ConfigurableListableBeanFactory.getBeanDefinition(java.lang.String)

findAutowireCandidates

protected Map findAutowireCandidates(String beanName,
                                     Class requiredType)
Description copied from class: AbstractAutowireCapableBeanFactory
Find bean instances that match the required type. Called during autowiring for the specified bean.

If a subclass cannot obtain information about bean names by type, a corresponding exception should be thrown.

Overrides:
findAutowireCandidates in class AbstractAutowireCapableBeanFactory
Parameters:
beanName - the name of the bean that is about to be wired
requiredType - the type of the autowired property or argument
Returns:
a Map of candidate names and candidate instances that match the required type (never null)
See Also:
AbstractAutowireCapableBeanFactory.autowireByType(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, org.springframework.beans.BeanWrapper, org.springframework.beans.MutablePropertyValues), AbstractAutowireCapableBeanFactory.autowireConstructor(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.reflect.Constructor)

toString

public String toString()
Overrides:
toString in class Object

The Spring Framework

Copyright © 2002-2007 The Spring Framework.