com.interface21.beans.factory.support
Class AbstractBeanFactory

java.lang.Object
  |
  +--com.interface21.beans.factory.support.AbstractBeanFactory
All Implemented Interfaces:
BeanFactory
Direct Known Subclasses:
ListableBeanFactoryImpl

public abstract class AbstractBeanFactory
extends java.lang.Object
implements BeanFactory

Abstract superclass that makes implementing a BeanFactory very easy. This class uses the Template Method design pattern. Subclasses must implement only the getBeanDefinition(name) method. This class handles resolution of runtime bean references, FactoryBean dereferencing, and management of collection properties.

Since:
15 April 2001
Version:
$Id: AbstractBeanFactory.java,v 1.19 2003/06/07 15:58:34 johnsonr Exp $
Author:
Rod Johnson

Field Summary
protected  java.lang.String defaultParentBean
          name of default parent bean
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()
          Creates a new AbstractBeanFactory
AbstractBeanFactory(BeanFactory parentBeanFactory)
          Creates a new AbstractBeanFactory, with the given parent.
 
Method Summary
 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 a shared instance of the given bean.
protected  java.lang.Class getBeanClass(AbstractBeanDefinition bd)
          Convenience method for use by subclasses.
protected abstract  AbstractBeanDefinition getBeanDefinition(java.lang.String beanName)
          This method must be defined by concrete subclasses to implement the Template Method GoF design pattern.
 BeanFactory getParentBeanFactory()
          Returns the parent bean factory, or null if none.
 boolean isSingleton(java.lang.String name)
          Is this bean a singleton?
 void registerAlias(java.lang.String name, java.lang.String alias)
          Given a bean name, create an alias.
 
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.

logger

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

defaultParentBean

protected java.lang.String defaultParentBean
name of default parent bean
Constructor Detail

AbstractBeanFactory

public AbstractBeanFactory()
Creates a new AbstractBeanFactory

AbstractBeanFactory

public AbstractBeanFactory(BeanFactory parentBeanFactory)
Creates a new AbstractBeanFactory, with the given parent.
Parameters:
parentBeanFactory - the parent bean factory, or null if none
See Also:
getBean(java.lang.String)
Method Detail

getParentBeanFactory

public BeanFactory getParentBeanFactory()
Returns the parent bean factory, or null if none.

getBean

public final java.lang.Object getBean(java.lang.String name)
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

getBean

public final java.lang.Object getBean(java.lang.String name,
                                      java.lang.Class requiredType)
                               throws BeansException
Return a shared instance of the given bean. Analogous to getBeanInstance(name, requiredType).
Specified by:
getBean in interface BeanFactory
Parameters:
name - name of the instance to return
requiredType - type the bean must match
Returns:
a shared instance of the given bean
Throws:
BeanNotOfRequiredTypeException - if the bean not of the required type
NoSuchBeanDefinitionException - if there's no such bean definition

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?
Specified by:
isSingleton in interface BeanFactory
See Also:
BeanFactory.isSingleton(String)

getBeanClass

protected final java.lang.Class getBeanClass(AbstractBeanDefinition bd)
Convenience method for use by subclasses. Resolves class, even by traversing parent if child definition.
Parameters:
bd - the BeanDefinition we want to check. This BeanDefinition may not actually contain the class--this method may need to navigate its ancestors to find the class.
Returns:
the Class of this bean

registerAlias

public final void registerAlias(java.lang.String name,
                                java.lang.String alias)
Given a bean name, create an alias. This must respect prototype/ singleton behaviour. We typically use this method to support names that are illegal within XML ids (used for bean names).
Parameters:
name - name of the bean
alias - alias that will behave the same as the bean names

getAliases

public final java.lang.String[] getAliases(java.lang.String name)
Description copied from interface: BeanFactory
Return the aliases for the given bean name, if defined.
Specified by:
getAliases in interface BeanFactory
Following copied from interface: com.interface21.beans.factory.BeanFactory
Parameters:
name - the bean name to check for aliases
Returns:
the aliases, or an empty array if none

getBeanDefinition

protected abstract AbstractBeanDefinition getBeanDefinition(java.lang.String beanName)
                                                     throws NoSuchBeanDefinitionException
This method must be defined by concrete subclasses to implement the Template Method GoF design pattern.
Subclasses should normally implement caching, as this method is invoked by this class every time a bean is requested.
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


Rod Johnson and Spring contributors 2001-2003.