org.springframework.beans.factory.support
Class AbstractBeanFactory

java.lang.Object
  extended byorg.springframework.beans.factory.support.AbstractBeanFactory
All Implemented Interfaces:
BeanFactory, ConfigurableBeanFactory, HierarchicalBeanFactory
Direct Known Subclasses:
AbstractAutowireCapableBeanFactory

public abstract class AbstractBeanFactory
extends java.lang.Object
implements ConfigurableBeanFactory, HierarchicalBeanFactory

Abstract superclass for BeanFactory implementations. Implements the ConfigurableBeanFactory SPI interface.

This class provides singleton/prototype determination, singleton cache, aliases, FactoryBean handling, and bean definition merging for child bean definitions. It also allows for management of a bean factory hierarchy, implementing the HierarchicalBeanFactory interface.

The main template methods to be implemented by subclasses are getBeanDefinition and createBean, retrieving a bean definition for a given bean name respectively creating a bean instance for a given bean definition.

Since:
15 April 2001
Version:
$Id: AbstractBeanFactory.java,v 1.50 2004/03/19 17:45:36 jhoeller Exp $
Author:
Rod Johnson, Juergen Hoeller
See Also:
getBeanDefinition(java.lang.String), createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition)

Field Summary
static java.lang.String FACTORY_BEAN_PREFIX
          Used to dereference a FactoryBean and distinguish it from beans created by the factory.
protected  org.apache.commons.logging.Log logger
          Logger available to subclasses
 
Constructor Summary
AbstractBeanFactory()
          Create a new AbstractBeanFactory.
AbstractBeanFactory(BeanFactory parentBeanFactory)
          Create a new AbstractBeanFactory with the given parent.
 
Method Summary
 void addBeanPostProcessor(BeanPostProcessor beanPostProcessor)
          Add a new BeanPostPrcoessor that will get applied to beans created with this factory.
protected  void addSingleton(java.lang.String beanName, java.lang.Object singletonObject)
          Add the given singleton object to the singleton cache of this factory.
 boolean containsBean(java.lang.String name)
          Does this bean factory contain a bean with the given name?
abstract  boolean containsBeanDefinition(java.lang.String beanName)
          Check if this bean factory contains a bean definition with the given name.
protected abstract  java.lang.Object createBean(java.lang.String beanName, RootBeanDefinition mergedBeanDefinition)
          Create a bean instance for the given bean definition.
protected abstract  void destroyBean(java.lang.String beanName, java.lang.Object bean)
          Destroy the given bean.
protected  void destroySingleton(java.lang.String beanName)
          Destroy the given bean.
 void destroySingletons()
          Destroy all cached singletons in this factory.
 java.lang.String[] getAliases(java.lang.String name)
          Return the aliases for the given bean name, if defined.
 java.lang.Object getBean(java.lang.String name)
          Return the bean with the given name, checking the parent bean factory if not found.
 java.lang.Object getBean(java.lang.String name, java.lang.Class requiredType)
          Return an instance (possibly shared or independent) of the given bean name.
abstract  BeanDefinition getBeanDefinition(java.lang.String beanName)
          Return the bean definition for the given bean name.
 java.util.List getBeanPostProcessors()
          Return the list of BeanPostProcessors that will get applied to beans created with this factory.
 java.util.Map getCustomEditors()
          Return the map of custom editors, with Classes as keys and PropertyEditors as values.
 java.util.Set getIgnoredDependencyTypes()
          Return the set of classes that will get ignored for autowiring.
protected  RootBeanDefinition getMergedBeanDefinition(java.lang.String beanName, BeanDefinition bd)
          Return a RootBeanDefinition, even by traversing parent if the parameter is a child definition.
 RootBeanDefinition getMergedBeanDefinition(java.lang.String beanName, boolean includingAncestors)
          Return a RootBeanDefinition, even by traversing parent if the parameter is a child definition.
protected  java.lang.Object getObjectForSharedInstance(java.lang.String name, java.lang.Object beanInstance)
          Get the object for the given shared bean, either the bean instance itself or its created object in case of a FactoryBean.
 BeanFactory getParentBeanFactory()
          Return the parent bean factory, or null if there is none.
 java.lang.String[] getSingletonNames(java.lang.Class type)
          Return the names of beans in the singleton cache that match the given object type (including subclasses).
 void ignoreDependencyType(java.lang.Class type)
          Ignore the given dependency type for autowiring.
protected  void initBeanWrapper(BeanWrapper bw)
          Initialize the given BeanWrapper with the custom editors registered with this factory.
protected  boolean isFactoryDereference(java.lang.String name)
          Return whether this name is a factory dereference (beginning with the factory dereference prefix).
 boolean isSingleton(java.lang.String name)
          Is this bean a singleton? That is, will getBean() always return the same object?
 void registerAlias(java.lang.String beanName, java.lang.String alias)
          Given a bean name, create an alias.
 void registerCustomEditor(java.lang.Class requiredType, java.beans.PropertyEditor propertyEditor)
          Register the given custom property editor for all properties of the given type.
 void registerSingleton(java.lang.String beanName, java.lang.Object singletonObject)
          Register the given existing object as singleton in the bean factory, under the given bean name.
 void setParentBeanFactory(BeanFactory parentBeanFactory)
          Set the parent of this bean factory.
protected  java.lang.String transformedBeanName(java.lang.String name)
          Return the bean name, stripping out the factory dereference prefix if necessary, and resolving aliases to canonical names.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FACTORY_BEAN_PREFIX

public static final java.lang.String FACTORY_BEAN_PREFIX
Used to dereference a FactoryBean and distinguish it from beans created by the factory. For example, if the bean named myEjb is a factory, getting &myEjb will return the factory, not the instance returned by the factory.

See Also:
Constant Field Values

logger

protected final org.apache.commons.logging.Log logger
Logger available to subclasses

Constructor Detail

AbstractBeanFactory

public AbstractBeanFactory()
Create a new AbstractBeanFactory.


AbstractBeanFactory

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

Parameters:
parentBeanFactory - parent bean factory, or null if none
See Also:
getBean(java.lang.String)
Method Detail

getBean

public java.lang.Object getBean(java.lang.String name)
                         throws BeansException
Return the bean with the given name, checking the parent bean factory if not found.

Specified by:
getBean in interface BeanFactory
Parameters:
name - name of the bean to retrieve
Returns:
the instance of the bean
Throws:
BeansException - if the bean could not be created

getBean

public java.lang.Object getBean(java.lang.String name,
                                java.lang.Class requiredType)
                         throws BeansException
Description copied from interface: BeanFactory
Return an instance (possibly shared or independent) of the given bean name. Provides a measure of type safety by throwing an exception if the bean is not of the required type.

Note that callers should retain references to returned objects. There is no guarantee that this method will be implemented to be efficient. For example, it may be synchronized, or may need to run an RDBMS query.

Will ask the parent factory if the bean cannot be found in this factory instance.

Specified by:
getBean in interface BeanFactory
Parameters:
name - name of the bean to return
requiredType - type the bean may match. Can be an interface or superclass of the actual class. For example, if the value is Object.class, this method will succeed whatever the class of the returned instance.
Returns:
the instance of the bean
Throws:
BeansException - if the bean could not be created

containsBean

public boolean containsBean(java.lang.String name)
Description copied from interface: BeanFactory
Does this bean factory contain a bean with the given name?

Will ask the parent factory if the bean cannot be found in this factory instance.

Specified by:
containsBean in interface BeanFactory
Parameters:
name - name of the bean to query
Returns:
whether a bean with the given name is defined

isSingleton

public boolean isSingleton(java.lang.String name)
                    throws NoSuchBeanDefinitionException
Description copied from interface: BeanFactory
Is this bean a singleton? That is, will getBean() always return the same object?

Will ask the parent factory if the bean cannot be found in this factory instance.

Specified by:
isSingleton in interface BeanFactory
Parameters:
name - name of the bean to query
Returns:
is this bean a singleton
Throws:
NoSuchBeanDefinitionException - if there is no bean with the given name

getAliases

public java.lang.String[] getAliases(java.lang.String name)
                              throws NoSuchBeanDefinitionException
Description copied from interface: BeanFactory
Return the aliases for the given bean name, if defined.

Will ask the parent factory if the bean cannot be found in this factory instance.

Specified by:
getAliases in interface BeanFactory
Parameters:
name - the bean name to check for aliases
Returns:
the aliases, or an empty array if none
Throws:
NoSuchBeanDefinitionException - if there's no such bean definition

getParentBeanFactory

public BeanFactory getParentBeanFactory()
Description copied from interface: HierarchicalBeanFactory
Return the parent bean factory, or null if there is none.

Specified by:
getParentBeanFactory in interface HierarchicalBeanFactory

setParentBeanFactory

public void setParentBeanFactory(BeanFactory parentBeanFactory)
Description copied from interface: ConfigurableBeanFactory
Set the parent of this bean factory.

Note that the parent shouldn't be changed: It should only be set outside a constructor if it isn't available when an object of this class is created.

Specified by:
setParentBeanFactory in interface ConfigurableBeanFactory
Parameters:
parentBeanFactory - the parent bean factory

registerCustomEditor

public void registerCustomEditor(java.lang.Class requiredType,
                                 java.beans.PropertyEditor propertyEditor)
Description copied from interface: ConfigurableBeanFactory
Register the given custom property editor for all properties of the given type. To be invoked during factory configuration.

Specified by:
registerCustomEditor in interface ConfigurableBeanFactory
Parameters:
requiredType - type of the property
propertyEditor - editor to register

getCustomEditors

public java.util.Map getCustomEditors()
Return the map of custom editors, with Classes as keys and PropertyEditors as values.


addBeanPostProcessor

public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor)
Description copied from interface: ConfigurableBeanFactory
Add a new BeanPostPrcoessor that will get applied to beans created with this factory. To be invoked during factory configuration.

Specified by:
addBeanPostProcessor in interface ConfigurableBeanFactory

getBeanPostProcessors

public java.util.List getBeanPostProcessors()
Return the list of BeanPostProcessors that will get applied to beans created with this factory.


ignoreDependencyType

public void ignoreDependencyType(java.lang.Class type)
Description copied from interface: ConfigurableBeanFactory
Ignore the given dependency type for autowiring. To be invoked during factory configuration.

This will typically be used for dependencies that are resolved in other ways, like BeanFactory through BeanFactoryAware or ApplicationContext through ApplicationContextAware.

Specified by:
ignoreDependencyType in interface ConfigurableBeanFactory
See Also:
BeanFactoryAware, ApplicationContextAware

getIgnoredDependencyTypes

public java.util.Set getIgnoredDependencyTypes()
Return the set of classes that will get ignored for autowiring.


registerAlias

public void registerAlias(java.lang.String beanName,
                          java.lang.String alias)
                   throws BeanDefinitionStoreException
Description copied from interface: ConfigurableBeanFactory
Given a bean name, create an alias. We typically use this method to support names that are illegal within XML ids (used for bean names).

Typically invoked during factory configuration, but can also be used for runtime registration of aliases. Therefore, a factory implementation should synchronize alias access.

Specified by:
registerAlias in interface ConfigurableBeanFactory
Parameters:
beanName - name of the bean
alias - alias that will behave the same as the bean name
Throws:
BeanDefinitionStoreException

registerSingleton

public void registerSingleton(java.lang.String beanName,
                              java.lang.Object singletonObject)
                       throws BeanDefinitionStoreException
Description copied from interface: ConfigurableBeanFactory
Register the given existing object as singleton in the bean factory, under the given bean name.

Typically invoked during factory configuration, but can also be used for runtime registration of singletons. Therefore, a factory implementation should synchronize singleton access; it will have to do this anyway if it supports lazy initialization of singletons.

Specified by:
registerSingleton in interface ConfigurableBeanFactory
Parameters:
beanName - name of the bean
singletonObject - the existing object
Throws:
BeanDefinitionStoreException

addSingleton

protected void addSingleton(java.lang.String beanName,
                            java.lang.Object singletonObject)
Add the given singleton object to the singleton cache of this factory.

To be called for eager registration of singletons, e.g. to be able to resolve circular references.

Parameters:
beanName - the name of the bean
singletonObject - the singleton object

destroySingletons

public void destroySingletons()
Description copied from interface: ConfigurableBeanFactory
Destroy all cached singletons in this factory. To be called on shutdown of a factory.

Specified by:
destroySingletons in interface ConfigurableBeanFactory

destroySingleton

protected void destroySingleton(java.lang.String beanName)
Destroy the given bean. Delegates to destroyBean if a corresponding singleton instance is found.

Parameters:
beanName - name of the bean
See Also:
destroyBean(java.lang.String, java.lang.Object)

transformedBeanName

protected java.lang.String transformedBeanName(java.lang.String name)
                                        throws NoSuchBeanDefinitionException
Return the bean name, stripping out the factory dereference prefix if necessary, and resolving aliases to canonical names.

Throws:
NoSuchBeanDefinitionException

isFactoryDereference

protected boolean isFactoryDereference(java.lang.String name)
Return whether this name is a factory dereference (beginning with the factory dereference prefix).


initBeanWrapper

protected void initBeanWrapper(BeanWrapper bw)
Initialize the given BeanWrapper with the custom editors registered with this factory.

Parameters:
bw - the BeanWrapper to initialize

getSingletonNames

public java.lang.String[] getSingletonNames(java.lang.Class type)
Return the names of beans in the singleton cache that match the given object type (including subclasses). Will not consider FactoryBeans as the type of their created objects is not known before instantiation.

Does not consider any hierarchy this factory may participate in.

Parameters:
type - class or interface to match, or null for all bean names
Returns:
the names of beans in the singleton cache that match the given object type (including subclasses), or an empty array if none

getObjectForSharedInstance

protected java.lang.Object getObjectForSharedInstance(java.lang.String name,
                                                      java.lang.Object beanInstance)
Get the object for the given shared bean, either the bean instance itself or its created object in case of a FactoryBean.

Parameters:
name - name that may include factory dereference prefix
beanInstance - the shared bean instance
Returns:
the singleton instance of the bean

getMergedBeanDefinition

public RootBeanDefinition getMergedBeanDefinition(java.lang.String beanName,
                                                  boolean includingAncestors)
                                           throws BeansException
Return a RootBeanDefinition, even by traversing parent if the parameter is a child definition. Will ask the parent bean factory if not found in this instance.

Returns:
a merged RootBeanDefinition with overridden properties
Throws:
BeansException

getMergedBeanDefinition

protected RootBeanDefinition getMergedBeanDefinition(java.lang.String beanName,
                                                     BeanDefinition bd)
Return a RootBeanDefinition, even by traversing parent if the parameter is a child definition.

Returns:
a merged RootBeanDefinition with overridden properties

containsBeanDefinition

public abstract boolean containsBeanDefinition(java.lang.String beanName)
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.

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:
containsBean(java.lang.String)

getBeanDefinition

public abstract BeanDefinition getBeanDefinition(java.lang.String beanName)
                                          throws BeansException
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.

Specified by:
getBeanDefinition in interface ConfigurableBeanFactory
Parameters:
beanName - name of the bean to find a definition for
Returns:
the BeanDefinition for this prototype name. Must never return null.
Throws:
NoSuchBeanDefinitionException - if the bean definition cannot be resolved
BeansException - in case of errors
See Also:
RootBeanDefinition, ChildBeanDefinition

createBean

protected abstract java.lang.Object createBean(java.lang.String beanName,
                                               RootBeanDefinition mergedBeanDefinition)
                                        throws BeansException
Create a bean instance for the given bean definition. The bean definition will already have been merged with the parent definition in case of a child definition.

All the other methods in this class invoke this method, although beans may be cached after being instantiated by this method. All bean instantiation within this class is performed by this method.

Parameters:
beanName - name of the bean
mergedBeanDefinition - the bean definition for the bean
Returns:
a new instance of the bean
Throws:
BeansException - in case of errors

destroyBean

protected abstract void destroyBean(java.lang.String beanName,
                                    java.lang.Object bean)
Destroy the given bean. Must destroy beans that depend on the given bean before the bean itself. Should not throw any exceptions.

Parameters:
beanName - name of the bean
bean - the bean instance to destroy


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