org.springframework.beans.factory.support
Class AbstractAutowireCapableBeanFactory

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

public abstract class AbstractAutowireCapableBeanFactory
extends AbstractBeanFactory
implements AutowireCapableBeanFactory

Abstract BeanFactory superclass that implements default bean creation. Implements the AutowireCapableBeanFactory interface.

Provides bean creation, initialization and wiring, supporting autowiring and constructor resolution. Handles runtime bean references, managed collections, and bean destruction.

The main template method to be implemented by subclasses is findMatchingBeans, used for autowiring by type. Note that this class does not implement bean definition registry capabilities (DefaultListableBeanFactory does).

Since:
13.02.2004
Author:
Rod Johnson, Juergen Hoeller
See Also:
findMatchingBeans(java.lang.Class)

Field Summary
 
Fields inherited from class org.springframework.beans.factory.support.AbstractBeanFactory
FACTORY_BEAN_PREFIX, logger
 
Fields inherited from interface org.springframework.beans.factory.config.AutowireCapableBeanFactory
AUTOWIRE_AUTODETECT, AUTOWIRE_BY_NAME, AUTOWIRE_BY_TYPE, AUTOWIRE_CONSTRUCTOR
 
Constructor Summary
AbstractAutowireCapableBeanFactory()
           
AbstractAutowireCapableBeanFactory(BeanFactory parentBeanFactory)
           
 
Method Summary
 java.lang.Object applyBeanPostProcessorsAfterInitialization(java.lang.Object bean, java.lang.String name)
          Apply BeanPostProcessors to the given existing bean instance, invoking their postProcessAfterInitialization methods.
 java.lang.Object applyBeanPostProcessorsBeforeInitialization(java.lang.Object bean, java.lang.String name)
          Apply BeanPostProcessors to the given existing bean instance, invoking their postProcessBeforeInitialization methods.
protected  void applyPropertyValues(java.lang.String beanName, RootBeanDefinition mergedBeanDefinition, BeanWrapper bw, PropertyValues pvs)
          Apply the given property values, resolving any runtime references to other beans in this bean factory.
 java.lang.Object autowire(java.lang.Class beanClass, int autowireMode, boolean dependencyCheck)
          Create a new bean instance of the given class with the specified autowire strategy.
 void autowireBeanProperties(java.lang.Object existingBean, int autowireMode, boolean dependencyCheck)
          Autowire the bean properties of the given bean instance by name or type.
protected  void autowireByName(java.lang.String beanName, RootBeanDefinition mergedBeanDefinition, BeanWrapper bw, MutablePropertyValues pvs)
          Fills in any missing property values with references to other beans in this factory if autowire is set to "byName".
protected  void autowireByType(java.lang.String beanName, RootBeanDefinition mergedBeanDefinition, BeanWrapper bw, MutablePropertyValues pvs)
          Abstract method defining "autowire by type" (bean properties by type) behaviour.
protected  BeanWrapper autowireConstructor(java.lang.String beanName, RootBeanDefinition mergedBeanDefinition)
          "autowire constructor" (with constructor arguments by type) behaviour.
protected  java.lang.Object createBean(java.lang.String beanName, RootBeanDefinition mergedBeanDefinition)
          Create a bean instance for the given bean definition.
protected  void dependencyCheck(java.lang.String beanName, RootBeanDefinition mergedBeanDefinition, BeanWrapper bw, PropertyValues pvs)
          Perform a dependency check that all properties exposed have been set, if desired.
protected  void destroyBean(java.lang.String beanName, java.lang.Object bean)
          Destroy the given bean.
 void destroySingletons()
          Destroy all cached singletons in this factory.
protected abstract  java.util.Map findMatchingBeans(java.lang.Class requiredType)
          Find bean instances that match the required type.
protected abstract  java.lang.String[] getDependingBeanNames(java.lang.String beanName)
          Return the names of the beans that depend on the given bean.
protected  void invokeCustomDestroyMethod(java.lang.String beanName, java.lang.Object bean, java.lang.String destroyMethodName)
          Invoke the specified custom destroy method on the given bean.
protected  void invokeInitMethods(java.lang.Object bean, java.lang.String beanName, RootBeanDefinition mergedBeanDefinition)
          Give a bean a chance to react now all its properties are set, and a chance to know about its owning bean factory (this object).
protected  void populateBean(java.lang.String beanName, RootBeanDefinition mergedBeanDefinition, BeanWrapper bw)
          Populate the bean instance in the given BeanWrapper with the property values from the bean definition.
protected  java.util.List resolveManagedList(java.lang.String beanName, RootBeanDefinition mergedBeanDefinition, java.lang.String argName, ManagedList ml)
          For each element in the ManagedList, resolve reference if necessary.
protected  java.util.Map resolveManagedMap(java.lang.String beanName, RootBeanDefinition mergedBeanDefinition, java.lang.String argName, ManagedMap mm)
          For each element in the ManagedMap, resolve reference if necessary.
protected  java.util.Set resolveManagedSet(java.lang.String beanName, RootBeanDefinition mergedBeanDefinition, java.lang.String argName, ManagedSet ms)
          For each element in the ManagedList, resolve reference if necessary.
protected  java.lang.Object resolveReference(RootBeanDefinition mergedBeanDefinition, java.lang.String beanName, java.lang.String argName, RuntimeBeanReference ref)
          Resolve a reference to another bean in the factory.
protected  java.lang.Object resolveValueIfNecessary(java.lang.String beanName, RootBeanDefinition mergedBeanDefinition, java.lang.String argName, java.lang.Object value)
          Given a PropertyValue, return a value, resolving any references to other beans in the factory if necessary.
protected  java.lang.String[] unsatisfiedObjectProperties(RootBeanDefinition mergedBeanDefinition, BeanWrapper bw)
          Return an array of object-type property names that are unsatisfied.
 
Methods inherited from class org.springframework.beans.factory.support.AbstractBeanFactory
addBeanPostProcessor, addSingleton, containsBean, containsBeanDefinition, destroySingleton, getAliases, getBean, getBean, getBeanDefinition, getBeanPostProcessors, getCustomEditors, getIgnoredDependencyTypes, getMergedBeanDefinition, getMergedBeanDefinition, getObjectForSharedInstance, getParentBeanFactory, getSingletonNames, ignoreDependencyType, initBeanWrapper, isFactoryDereference, isSingleton, registerAlias, registerCustomEditor, registerSingleton, setParentBeanFactory, transformedBeanName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.springframework.beans.factory.BeanFactory
containsBean, getAliases, getBean, getBean, isSingleton
 

Constructor Detail

AbstractAutowireCapableBeanFactory

public AbstractAutowireCapableBeanFactory()

AbstractAutowireCapableBeanFactory

public AbstractAutowireCapableBeanFactory(BeanFactory parentBeanFactory)
Method Detail

autowire

public java.lang.Object autowire(java.lang.Class beanClass,
                                 int autowireMode,
                                 boolean dependencyCheck)
                          throws BeansException
Description copied from interface: AutowireCapableBeanFactory
Create a new bean instance of the given class with the specified autowire strategy. All constants defined in this interface are supported here.

Specified by:
autowire in interface AutowireCapableBeanFactory
Parameters:
beanClass - the class of the bean to instantiate
autowireMode - by name or type, using the constants in this interface
dependencyCheck - whether to perform a dependency check for objects (not applicable to autowiring a constructor, thus ignored there)
Returns:
the new bean instance
Throws:
BeansException - if instantiation respectively wiring failed
See Also:
AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT

autowireBeanProperties

public void autowireBeanProperties(java.lang.Object existingBean,
                                   int autowireMode,
                                   boolean dependencyCheck)
                            throws BeansException
Description copied from interface: AutowireCapableBeanFactory
Autowire the bean properties of the given bean instance by name or type.

Specified by:
autowireBeanProperties in interface AutowireCapableBeanFactory
Parameters:
existingBean - the existing bean instance
autowireMode - by name or type, using the constants in this interface
dependencyCheck - whether to perform a dependency check for object
Throws:
BeansException - if wiring failed
See Also:
AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE

applyBeanPostProcessorsBeforeInitialization

public java.lang.Object applyBeanPostProcessorsBeforeInitialization(java.lang.Object bean,
                                                                    java.lang.String name)
                                                             throws BeansException
Description copied from interface: AutowireCapableBeanFactory
Apply BeanPostProcessors to the given existing bean instance, invoking their postProcessBeforeInitialization methods. The returned bean instance may be a wrapper around the original.

Specified by:
applyBeanPostProcessorsBeforeInitialization in interface AutowireCapableBeanFactory
Parameters:
bean - the new bean instance
name - the name of the bean
Returns:
the bean instance to use, either the original or a wrapped one
Throws:
BeansException - if any post-processing failed
See Also:
BeanPostProcessor.postProcessBeforeInitialization(java.lang.Object, java.lang.String)

applyBeanPostProcessorsAfterInitialization

public java.lang.Object applyBeanPostProcessorsAfterInitialization(java.lang.Object bean,
                                                                   java.lang.String name)
                                                            throws BeansException
Description copied from interface: AutowireCapableBeanFactory
Apply BeanPostProcessors to the given existing bean instance, invoking their postProcessAfterInitialization methods. The returned bean instance may be a wrapper around the original.

Specified by:
applyBeanPostProcessorsAfterInitialization in interface AutowireCapableBeanFactory
Parameters:
bean - the new bean instance
name - the name of the bean
Returns:
the bean instance to use, either the original or a wrapped one
Throws:
BeansException - if any post-processing failed
See Also:
BeanPostProcessor.postProcessAfterInitialization(java.lang.Object, java.lang.String)

createBean

protected java.lang.Object createBean(java.lang.String beanName,
                                      RootBeanDefinition mergedBeanDefinition)
                               throws BeansException
Description copied from class: AbstractBeanFactory
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.

Specified by:
createBean in class AbstractBeanFactory
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

autowireConstructor

protected BeanWrapper autowireConstructor(java.lang.String beanName,
                                          RootBeanDefinition mergedBeanDefinition)
                                   throws BeansException
"autowire constructor" (with constructor arguments by type) behaviour. Also applied if explicit constructor argument values are specified, matching all remaining arguments with beans from the bean factory.

This corresponds to PicoContainer's "Type 3 IoC" paradigm: In this mode, a Spring bean factory is able to host components that expect constructor-based dependency resolution.

Parameters:
beanName - name of the bean to autowire by type
mergedBeanDefinition - bean definition to update through autowiring
Returns:
BeanWrapper for the new instance
Throws:
BeansException

populateBean

protected void populateBean(java.lang.String beanName,
                            RootBeanDefinition mergedBeanDefinition,
                            BeanWrapper bw)
Populate the bean instance in the given BeanWrapper with the property values from the bean definition.

Parameters:
beanName - name of the bean
mergedBeanDefinition - the bean definition for the bean
bw - BeanWrapper with bean instance

autowireByName

protected void autowireByName(java.lang.String beanName,
                              RootBeanDefinition mergedBeanDefinition,
                              BeanWrapper bw,
                              MutablePropertyValues pvs)
Fills in any missing property values with references to other beans in this factory if autowire is set to "byName".

Parameters:
beanName - name of the bean we're wiring up. Useful for debugging messages; not used functionally.
mergedBeanDefinition - bean definition to update through autowiring
bw - BeanWrapper from which we can obtain information about the bean
pvs - the PropertyValues to register wired objects with

autowireByType

protected void autowireByType(java.lang.String beanName,
                              RootBeanDefinition mergedBeanDefinition,
                              BeanWrapper bw,
                              MutablePropertyValues pvs)
Abstract method defining "autowire by type" (bean properties by type) behaviour.

This is like PicoContainer default, in which there must be exactly one bean of the property type in the bean factory. This makes bean factories simple to configure for small namespaces, but doesn't work as well as standard Spring behaviour for bigger applications.

Parameters:
beanName - name of the bean to autowire by type
mergedBeanDefinition - bean definition to update through autowiring
bw - BeanWrapper from which we can obtain information about the bean
pvs - the PropertyValues to register wired objects with

dependencyCheck

protected void dependencyCheck(java.lang.String beanName,
                               RootBeanDefinition mergedBeanDefinition,
                               BeanWrapper bw,
                               PropertyValues pvs)
                        throws UnsatisfiedDependencyException
Perform a dependency check that all properties exposed have been set, if desired. Dependency checks can be objects (collaborating beans), simple (primitives and String), or all (both).

Parameters:
beanName - name of the bean
Throws:
UnsatisfiedDependencyException

unsatisfiedObjectProperties

protected java.lang.String[] unsatisfiedObjectProperties(RootBeanDefinition mergedBeanDefinition,
                                                         BeanWrapper bw)
Return an array of object-type property names that are unsatisfied. These are probably unsatisfied references to other beans in the factory. Does not include simple properties like primitives or Strings.

Returns:
an array of object-type property names that are unsatisfied
See Also:
BeanUtils.isSimpleProperty(java.lang.Class)

applyPropertyValues

protected void applyPropertyValues(java.lang.String beanName,
                                   RootBeanDefinition mergedBeanDefinition,
                                   BeanWrapper bw,
                                   PropertyValues pvs)
                            throws BeansException
Apply the given property values, resolving any runtime references to other beans in this bean factory. Must use deep copy, so we don't permanently modify this property

Parameters:
beanName - bean name passed for better exception information
bw - BeanWrapper wrapping the target object
pvs - new property values
Throws:
BeansException

resolveValueIfNecessary

protected java.lang.Object resolveValueIfNecessary(java.lang.String beanName,
                                                   RootBeanDefinition mergedBeanDefinition,
                                                   java.lang.String argName,
                                                   java.lang.Object value)
                                            throws BeansException
Given a PropertyValue, return a value, resolving any references to other beans in the factory if necessary. The value could be:
  • A BeanDefinition, which leads to the creation of a corresponding new bean instance. Singleton flags and names of such "inner beans" are always ignored: Inner beans are anonymous prototypes.
  • A RuntimeBeanReference, which must be resolved.
  • A ManagedList. This is a special collection that may contain RuntimeBeanReferences or Collections that will need to be resolved.
  • A ManagedSet. May also contain RuntimeBeanReferences or Collections that will need to be resolved.
  • A ManagedMap. In this case the value may be a RuntimeBeanReference or Collection that will need to be resolved.
  • An ordinary object or null, in which case it's left alone.

    Throws:
    BeansException

  • resolveReference

    protected java.lang.Object resolveReference(RootBeanDefinition mergedBeanDefinition,
                                                java.lang.String beanName,
                                                java.lang.String argName,
                                                RuntimeBeanReference ref)
                                         throws BeansException
    Resolve a reference to another bean in the factory.

    Throws:
    BeansException

    resolveManagedList

    protected java.util.List resolveManagedList(java.lang.String beanName,
                                                RootBeanDefinition mergedBeanDefinition,
                                                java.lang.String argName,
                                                ManagedList ml)
                                         throws BeansException
    For each element in the ManagedList, resolve reference if necessary.

    Throws:
    BeansException

    resolveManagedSet

    protected java.util.Set resolveManagedSet(java.lang.String beanName,
                                              RootBeanDefinition mergedBeanDefinition,
                                              java.lang.String argName,
                                              ManagedSet ms)
                                       throws BeansException
    For each element in the ManagedList, resolve reference if necessary.

    Throws:
    BeansException

    resolveManagedMap

    protected java.util.Map resolveManagedMap(java.lang.String beanName,
                                              RootBeanDefinition mergedBeanDefinition,
                                              java.lang.String argName,
                                              ManagedMap mm)
                                       throws BeansException
    For each element in the ManagedMap, resolve reference if necessary.

    Throws:
    BeansException

    invokeInitMethods

    protected void invokeInitMethods(java.lang.Object bean,
                                     java.lang.String beanName,
                                     RootBeanDefinition mergedBeanDefinition)
                              throws java.lang.Exception
    Give a bean a chance to react now all its properties are set, and a chance to know about its owning bean factory (this object). This means checking whether the bean implements InitializingBean and/or BeanFactoryAware, and invoking the necessary callback(s) if it does.

    Parameters:
    bean - new bean instance we may need to initialize
    beanName - the bean has in the factory. Used for debug output.
    Throws:
    java.lang.Exception

    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
    Overrides:
    destroySingletons in class AbstractBeanFactory

    destroyBean

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

    Specified by:
    destroyBean in class AbstractBeanFactory
    Parameters:
    beanName - name of the bean
    bean - the bean instance to destroy

    invokeCustomDestroyMethod

    protected void invokeCustomDestroyMethod(java.lang.String beanName,
                                             java.lang.Object bean,
                                             java.lang.String destroyMethodName)
    Invoke the specified custom destroy method on the given bean.

    This implementation invokes a no-arg method if found, else checking for a method with a single boolean argument (passing in "true", assuming a "force" parameter), else logging an error.

    Can be overridden in subclasses for custom resolution of destroy methods with arguments.


    findMatchingBeans

    protected abstract java.util.Map findMatchingBeans(java.lang.Class requiredType)
                                                throws BeansException
    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.

    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
    Throws:
    BeansException - in case of errors
    See Also:
    autowireByType(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, org.springframework.beans.BeanWrapper, org.springframework.beans.MutablePropertyValues), autowireConstructor(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition)

    getDependingBeanNames

    protected abstract java.lang.String[] getDependingBeanNames(java.lang.String beanName)
                                                         throws BeansException
    Return the names of the beans that depend on the given bean. Called by destroyBean, to be able to destroy depending beans first.

    Parameters:
    beanName - name of the bean to find depending beans for
    Returns:
    array of names of depending beans, or null if none
    Throws:
    BeansException - in case of errors
    See Also:
    destroyBean(java.lang.String, java.lang.Object)


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