org.springframework.beans.factory.support
Class DefaultListableBeanFactory

java.lang.Object
  extended byorg.springframework.beans.factory.support.AbstractBeanFactory
      extended byorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
          extended byorg.springframework.beans.factory.support.DefaultListableBeanFactory
All Implemented Interfaces:
AutowireCapableBeanFactory, BeanDefinitionRegistry, BeanFactory, ConfigurableBeanFactory, ConfigurableListableBeanFactory, HierarchicalBeanFactory, ListableBeanFactory
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 definitions.

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.

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.

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:
ListableBeanFactory, StaticListableBeanFactory, PropertiesBeanDefinitionReader, XmlBeanDefinitionReader

Field Summary
 
Fields inherited from class org.springframework.beans.factory.support.AbstractBeanFactory
logger
 
Fields inherited from interface org.springframework.beans.factory.BeanFactory
FACTORY_BEAN_PREFIX
 
Fields inherited from interface org.springframework.beans.factory.config.AutowireCapableBeanFactory
AUTOWIRE_AUTODETECT, AUTOWIRE_BY_NAME, AUTOWIRE_BY_TYPE, AUTOWIRE_CONSTRUCTOR
 
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 registry contains a bean definition with the given name.
protected  List doGetBeanNamesForType(Class type, boolean includePrototypes, boolean includeFactoryBeans)
          Get a List of all bean names that match the given type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans.
protected  Map findMatchingBeans(Class requiredType)
          Find bean instances that match the required type.
 BeanDefinition getBeanDefinition(String beanName)
          Return the registered BeanDefinition for the given bean, allowing access to its property values and constructor argument value (which can be modified during bean factory post-processing).
 int getBeanDefinitionCount()
          Return the number of beans defined in the registry.
 String[] getBeanDefinitionNames()
          Return the names of all beans defined in this registry.
 String[] getBeanDefinitionNames(Class type)
          Return the names of beans matching the given type (including subclasses), judging from the bean definitions.
 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.
 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 includeFactoryBeans)
          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 if it should be allowed to override bean definitions by registering a different definition with the same name, automatically replacing the former.
 String toString()
           
 
Methods inherited from class org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
applyBeanPostProcessorsAfterInitialization, applyBeanPostProcessorsAfterInitialization, applyBeanPostProcessorsBeforeInitialization, applyBeanPostProcessorsBeforeInitialization, applyBeanPropertyValues, autowire, autowireBeanProperties, autowireByName, autowireByType, autowireConstructor, createBean, createBean, dependencyCheck, getIgnoredDependencyTypes, ignoreDependencyType, instantiateUsingFactoryMethod, invokeCustomInitMethod, invokeInitMethods, populateBean, resolveValueIfNecessary, setInstantiationStrategy, unsatisfiedObjectProperties
 
Methods inherited from class org.springframework.beans.factory.support.AbstractBeanFactory
addBeanPostProcessor, addSingleton, checkMergedBeanDefinition, containsBean, containsSingleton, createBeanWrapper, destroyBean, destroySingletons, getAliases, getBean, getBean, getBean, getBean, getBeanPostProcessors, getCustomEditors, getMergedBeanDefinition, getMergedBeanDefinition, getObjectForSharedInstance, getParentBeanFactory, getSingletonCount, getSingletonNames, getType, hasDestructionAwareBeanPostProcessors, initBeanWrapper, invokeCustomDestroyMethod, isFactoryBean, isFactoryDereference, isSingleton, registerAlias, registerCustomEditor, registerDependentBean, registerDisposableBean, registerDisposableBeanIfNecessary, registerSingleton, removeSingleton, setParentBeanFactory, transformedBeanName
 
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
ignoreDependencyType
 
Methods inherited from interface org.springframework.beans.factory.BeanFactory
containsBean, getAliases, getBean, getBean, getType, isSingleton
 
Methods inherited from interface org.springframework.beans.factory.config.AutowireCapableBeanFactory
applyBeanPostProcessorsAfterInitialization, applyBeanPostProcessorsBeforeInitialization, applyBeanPropertyValues, autowire, autowireBeanProperties
 
Methods inherited from interface org.springframework.beans.factory.config.ConfigurableBeanFactory
addBeanPostProcessor, containsSingleton, destroySingletons, registerAlias, registerCustomEditor, registerSingleton, setParentBeanFactory
 
Methods inherited from interface org.springframework.beans.factory.HierarchicalBeanFactory
getParentBeanFactory
 
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.

Method Detail

setAllowBeanDefinitionOverriding

public void setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding)
Set if 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.


containsBeanDefinition

public boolean containsBeanDefinition(String beanName)
Description copied from interface: BeanDefinitionRegistry
Check if this registry contains a bean definition with the given name.

Specified by:
containsBeanDefinition in interface ListableBeanFactory
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: BeanDefinitionRegistry
Return the number of beans defined in the registry.

Specified by:
getBeanDefinitionCount in interface ListableBeanFactory
Returns:
the number of beans defined in the factory
See Also:
BeanFactoryUtils.countBeansIncludingAncestors(org.springframework.beans.factory.ListableBeanFactory)

getBeanDefinitionNames

public String[] getBeanDefinitionNames()
Description copied from interface: BeanDefinitionRegistry
Return the names of all beans defined in this registry.

Specified by:
getBeanDefinitionNames in interface ListableBeanFactory
Returns:
the names of all beans defined in this factory, or an empty array if none defined
See Also:
BeanFactoryUtils.beanNamesIncludingAncestors(ListableBeanFactory)

getBeanDefinitionNames

public String[] getBeanDefinitionNames(Class type)
Description copied from interface: ListableBeanFactory
Return the names of beans matching the given type (including subclasses), judging from the bean definitions. Merges child bean definition with their parent before checking the type.

Does not consider objects created by FactoryBeans but rather the FactoryBean classes themselves, avoiding instantiation of any beans. Use getBeanNamesForType to match objects created by FactoryBeans.

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

Note: Ignores any singleton beans that have been registered by other means than bean definitions.

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

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.

Does consider objects created by FactoryBeans but rather the FactoryBean classes themselves, which means that FactoryBeans will get instantiated.

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.

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)

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.

Does consider objects created by FactoryBeans but rather the FactoryBean classes themselves, which means that FactoryBeans will get instantiated.

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).

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 includeFactoryBeans)
                   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.

Does consider objects created by FactoryBeans but rather the FactoryBean classes themselves, which means that FactoryBeans will get instantiated.

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.

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)
includeFactoryBeans - whether to include FactoryBeans too or just conventional 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, boolean, boolean)

doGetBeanNamesForType

protected List doGetBeanNamesForType(Class type,
                                     boolean includePrototypes,
                                     boolean includeFactoryBeans)
Get a List of all bean names that match the given type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans.

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)
includeFactoryBeans - whether to include FactoryBeans too or just conventional beans
Returns:
the names of beans (or objects created by FactoryBeans) matching the given object type (including subclasses), or an empty List if none

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.

As this is a startup method, it should destroy already created singletons if it fails, to avoid dangling resources. In other words, after invocation of that method, either all or no singletons at all should be instantiated.

Specified by:
preInstantiateSingletons in interface ConfigurableListableBeanFactory
Throws:
BeansException - if one of the singleton beans could not be created

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
See Also:
RootBeanDefinition, ChildBeanDefinition

getBeanDefinition

public BeanDefinition getBeanDefinition(String beanName)
                                 throws BeansException
Description copied from interface: ConfigurableListableBeanFactory
Return the registered BeanDefinition for the given bean, allowing access to its property values and constructor argument value (which can be modified during bean factory post-processing).

A returned BeanDefinition object should not be a copy but the original definition object as registered in the factory. This means that it should be castable to a more specific implementation type, if necessary.

Specified by:
getBeanDefinition in interface ConfigurableListableBeanFactory
Specified by:
getBeanDefinition in class AbstractBeanFactory
Parameters:
beanName - name of the bean to find a definition for
Returns:
the BeanDefinition for this prototype name. Must never return null.
Throws:
BeansException - in case of errors
See Also:
RootBeanDefinition, ChildBeanDefinition, ConfigurableListableBeanFactory.getBeanDefinition(java.lang.String)

findMatchingBeans

protected Map findMatchingBeans(Class requiredType)
Description copied from class: AbstractAutowireCapableBeanFactory
Find bean instances that match the required type. Called by autowiring. If a subclass cannot obtain information about bean names by type, a corresponding exception should be thrown.

Specified by:
findMatchingBeans in class AbstractAutowireCapableBeanFactory
Parameters:
requiredType - the type of the beans to look up
Returns:
a Map of bean names and bean instances that match the required type, or null if none found
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)

toString

public String toString()


Copyright (C) 2003-2004 The Spring Framework Project.