org.springframework.beans.factory.support
Class AbstractAutowireCapableBeanFactory

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

public abstract class AbstractAutowireCapableBeanFactory
extends AbstractBeanFactory
implements AutowireCapableBeanFactory

Abstract BeanFactory superclass that implements default bean creation, with the full capabilities specified by the RootBeanDefinition class. Implements the AutowireCapableBeanFactory interface in addition to AbstractBeanFactory's createBean method.

Provides bean creation (with constructor resolution), property population, wiring (including autowiring), and initialization. Handles runtime bean references, resolves managed collections, calls initialization methods, etc. Supports autowiring constructors, properties by name, and properties by type.

The main template method to be implemented by subclasses is findMatchingBeans, used for autowiring by type. In case of a factory which is capable of searching its bean definitions, matching beans will typically be implemented through such a search. For other factory styles, simplified matching algorithms can be implemented.

Note that this class does not assume or implement bean definition registry capabilities. See DefaultListableBeanFactory for an implementation of the ListableBeanFactory and BeanDefinitionRegistry interfaces, which represent the API (or SPI) view of such a factory.

Since:
13.02.2004
Author:
Rod Johnson, Juergen Hoeller
See Also:
AutowireCapableBeanFactory, AbstractBeanFactory.createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[]), RootBeanDefinition, findMatchingBeans(Class), DefaultListableBeanFactory, ListableBeanFactory, BeanDefinitionRegistry

Field Summary
 
Fields inherited from class org.springframework.beans.factory.support.AbstractBeanFactory
logger
 
Fields inherited from interface org.springframework.beans.factory.config.AutowireCapableBeanFactory
AUTOWIRE_AUTODETECT, AUTOWIRE_BY_NAME, AUTOWIRE_BY_TYPE, AUTOWIRE_CONSTRUCTOR
 
Fields inherited from interface org.springframework.beans.factory.BeanFactory
FACTORY_BEAN_PREFIX
 
Constructor Summary
AbstractAutowireCapableBeanFactory()
          Create a new AbstractAutowireCapableBeanFactory.
AbstractAutowireCapableBeanFactory(BeanFactory parentBeanFactory)
          Create a new AbstractAutowireCapableBeanFactory with the given parent.
 
Method Summary
 Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
          Apply BeanPostProcessors to the given existing bean instance, invoking their postProcessAfterInitialization methods.
 Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
          Apply BeanPostProcessors to the given existing bean instance, invoking their postProcessBeforeInitialization methods.
protected  Object applyBeanPostProcessorsBeforeInstantiation(Class beanClass, String beanName)
          Apply InstantiationAwareBeanPostProcessors to the given existing bean instance, invoking their postProcessBeforeInstantiation methods.
 void applyBeanPropertyValues(Object existingBean, String beanName)
          Apply the property values of the bean definition with the given name to the given bean instance.
 Object autowire(Class beanClass, int autowireMode, boolean dependencyCheck)
          Create a new bean instance of the given class with the specified autowire strategy.
 void autowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck)
          Autowire the bean properties of the given bean instance by name or type.
protected  void autowireByName(String beanName, RootBeanDefinition mergedBeanDefinition, BeanWrapper bw, MutablePropertyValues pvs)
          Fill in any missing property values with references to other beans in this factory if autowire is set to "byName".
protected  void autowireByType(String beanName, RootBeanDefinition mergedBeanDefinition, BeanWrapper bw, MutablePropertyValues pvs)
          Abstract method defining "autowire by type" (bean properties by type) behavior.
protected  BeanWrapper autowireConstructor(String beanName, RootBeanDefinition mergedBeanDefinition)
          "autowire constructor" (with constructor arguments by type) behavior.
protected  void checkDependencies(String beanName, RootBeanDefinition mergedBeanDefinition, BeanWrapper bw, PropertyValues pvs)
          Perform a dependency check that all properties exposed have been set, if desired.
protected  Object createBean(String beanName, RootBeanDefinition mergedBeanDefinition, Object[] args)
          Create a bean instance for the given bean definition.
protected abstract  Map findMatchingBeans(Class requiredType)
          Find bean instances that match the required type.
 Set getIgnoredDependencyInterfaces()
          Return the set of dependency interfaces that will get ignored for autowiring.
 Set getIgnoredDependencyTypes()
          Return the set of dependency types that will get ignored for autowiring.
protected  Class getTypeForFactoryMethod(String beanName, RootBeanDefinition mergedBeanDefinition)
          Determine the bean type for the given bean definition, as far as possible.
 void ignoreDependencyInterface(Class ifc)
          Ignore the given dependency interface for autowiring.
 void ignoreDependencyType(Class type)
          Ignore the given dependency type for autowiring: for example, String.
protected  BeanWrapper instantiateUsingFactoryMethod(String beanName, RootBeanDefinition mergedBeanDefinition, Object[] explicitArgs)
          Instantiate the bean using a named factory method.
protected  void invokeCustomInitMethod(String beanName, Object bean, String initMethodName)
          Invoke the specified custom init method on the given bean.
protected  void invokeInitMethods(String beanName, Object bean, 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  boolean isExcludedFromDependencyCheck(PropertyDescriptor pd)
          Determine whether the given bean property is excluded from dependency checks.
protected  void populateBean(String beanName, RootBeanDefinition mergedBeanDefinition, BeanWrapper bw)
          Populate the bean instance in the given BeanWrapper with the property values from the bean definition.
 void setInstantiationStrategy(InstantiationStrategy instantiationStrategy)
          Set the instantiation strategy to use for creating bean instances.
protected  String[] unsatisfiedNonSimpleProperties(RootBeanDefinition mergedBeanDefinition, BeanWrapper bw)
          Return an array of non-simple bean properties that are unsatisfied.
 
Methods inherited from class org.springframework.beans.factory.support.AbstractBeanFactory
addBeanPostProcessor, addSingleton, checkMergedBeanDefinition, containsBean, containsBeanDefinition, containsSingleton, destroyBean, destroySingletons, doTypeConversionIfNecessary, doTypeConversionIfNecessary, getAliases, getBean, getBean, getBean, getBean, getBeanDefinition, getBeanPostProcessorCount, getBeanPostProcessors, getCustomEditors, getMergedBeanDefinition, getMergedBeanDefinition, getMergedBeanDefinition, getObjectForSharedInstance, getParentBeanFactory, getSingletonCount, getSingletonNames, getType, hasDestructionAwareBeanPostProcessors, initBeanWrapper, invokeCustomDestroyMethod, isFactoryBean, isFactoryDereference, isSingleton, isSingletonCurrentlyInCreation, registerAlias, registerCustomEditor, registerDependentBean, registerDisposableBean, registerDisposableBeanIfNecessary, registerSingleton, removeSingleton, 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, getType, isSingleton
 

Constructor Detail

AbstractAutowireCapableBeanFactory

public AbstractAutowireCapableBeanFactory()
Create a new AbstractAutowireCapableBeanFactory.


AbstractAutowireCapableBeanFactory

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

Parameters:
parentBeanFactory - parent bean factory, or null if none
Method Detail

setInstantiationStrategy

public void setInstantiationStrategy(InstantiationStrategy instantiationStrategy)
Set the instantiation strategy to use for creating bean instances. Default is CglibSubclassingInstantiationStrategy.

See Also:
CglibSubclassingInstantiationStrategy

ignoreDependencyType

public void ignoreDependencyType(Class type)
Ignore the given dependency type for autowiring: for example, String. Default is none.


getIgnoredDependencyTypes

public Set getIgnoredDependencyTypes()
Return the set of dependency types that will get ignored for autowiring.


ignoreDependencyInterface

public void ignoreDependencyInterface(Class ifc)
Ignore the given dependency interface for autowiring.

This will typically be used by application contexts to register dependencies that are resolved in other ways, like BeanFactory through BeanFactoryAware or ApplicationContext through ApplicationContextAware.

By default, only the BeanFactory interface is ignored. For further types to ignore, invoke this method for each type.

See Also:
BeanFactoryAware, ApplicationContextAware

getIgnoredDependencyInterfaces

public Set getIgnoredDependencyInterfaces()
Return the set of dependency interfaces that will get ignored for autowiring.


autowire

public Object autowire(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 or wiring failed
See Also:
AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT

autowireBeanProperties

public void autowireBeanProperties(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

applyBeanPropertyValues

public void applyBeanPropertyValues(Object existingBean,
                                    String beanName)
                             throws BeansException
Description copied from interface: AutowireCapableBeanFactory
Apply the property values of the bean definition with the given name to the given bean instance. The bean definition can either define a fully self-contained bean, reusing its property values, or just property values meant to be used for existing bean instances.

Note: This method does not autowire bean properties; it just applies explicitly defined property values. Use the autowireBeanProperties method to autowire an existing bean instance.

Specified by:
applyBeanPropertyValues in interface AutowireCapableBeanFactory
Parameters:
existingBean - the existing bean instance
beanName - the name of the bean definition in the bean factory
Throws:
NoSuchBeanDefinitionException - if there is no bean with the given name
BeansException - if applying the property values failed
See Also:
AutowireCapableBeanFactory.autowireBeanProperties(java.lang.Object, int, boolean)

applyBeanPostProcessorsBeforeInstantiation

protected Object applyBeanPostProcessorsBeforeInstantiation(Class beanClass,
                                                            String beanName)
                                                     throws BeansException
Apply InstantiationAwareBeanPostProcessors to the given existing bean instance, invoking their postProcessBeforeInstantiation methods. The returned bean instance may be a wrapper around the original.

Parameters:
beanClass - the class of the bean to be instantiated
beanName - the name of the bean
Returns:
the bean object to use instead of a default instance of the target bean
Throws:
BeansException - if any post-processing failed
See Also:
InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(java.lang.Class, java.lang.String)

applyBeanPostProcessorsBeforeInitialization

public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean,
                                                          String beanName)
                                                   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:
existingBean - the new bean instance
beanName - 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 Object applyBeanPostProcessorsAfterInitialization(Object existingBean,
                                                         String beanName)
                                                  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:
existingBean - the new bean instance
beanName - 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 Object createBean(String beanName,
                            RootBeanDefinition mergedBeanDefinition,
                            Object[] args)
                     throws BeanCreationException
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
args - arguments to use if creating a prototype using explicit arguments to a static factory method. This parameter must be null except in this case.
Returns:
a new instance of the bean
Throws:
BeanCreationException - if the bean could not be created

getTypeForFactoryMethod

protected Class getTypeForFactoryMethod(String beanName,
                                        RootBeanDefinition mergedBeanDefinition)
Description copied from class: AbstractBeanFactory
Determine the bean type for the given bean definition, as far as possible.

Default implementation returns null to indicate that the type cannot be determined. Subclasses are encouraged to try to determine the actual return type here, matching their strategy of resolving factory methods in the createBean imüplementation.

Overrides:
getTypeForFactoryMethod in class AbstractBeanFactory
Parameters:
beanName - name of the bean
mergedBeanDefinition - the bean definition for the bean
Returns:
the type for the bean if determinable, or null else
See Also:
AbstractBeanFactory.createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[])

instantiateUsingFactoryMethod

protected BeanWrapper instantiateUsingFactoryMethod(String beanName,
                                                    RootBeanDefinition mergedBeanDefinition,
                                                    Object[] explicitArgs)
                                             throws BeansException
Instantiate the bean using a named factory method. The method may be static, if the mergedBeanDefinition parameter specifies a class, rather than a factoryBean, or an instance variable on a factory object itself configured using Dependency Injection.

Implementation requires iterating over the static or instance methods with the name specified in the RootBeanDefinition (the method may be overloaded) and trying to match with the parameters. We don't have the types attached to constructor args, so trial and error is the only way to go here. The explicitArgs array may contain argument values passed in programmatically via the corresponding getBean method.

Parameters:
beanName - name of the bean to autowire by type
mergedBeanDefinition - bean definition to update through autowiring
explicitArgs - argument values passed in programmatically via the getBean method, or null if none (-> use constructor argument values from bean definition)
Returns:
BeanWrapper for the new instance
Throws:
BeansException
See Also:
AbstractBeanFactory.getBean(String, Object[])

autowireConstructor

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

This corresponds to constructor injection: 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(String beanName,
                            RootBeanDefinition mergedBeanDefinition,
                            BeanWrapper bw)
                     throws BeansException
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
Throws:
BeansException

autowireByName

protected void autowireByName(String beanName,
                              RootBeanDefinition mergedBeanDefinition,
                              BeanWrapper bw,
                              MutablePropertyValues pvs)
                       throws BeansException
Fill 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
Throws:
BeansException

autowireByType

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

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 behavior 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
Throws:
BeansException

unsatisfiedNonSimpleProperties

protected String[] unsatisfiedNonSimpleProperties(RootBeanDefinition mergedBeanDefinition,
                                                  BeanWrapper bw)
Return an array of non-simple bean properties that are unsatisfied. These are probably unsatisfied references to other beans in the factory. Does not include simple properties like primitives or Strings.

Parameters:
mergedBeanDefinition - the bean definition the bean was created with
bw - the BeanWrapper the bean was created with
Returns:
an array of bean property names
See Also:
BeanUtils.isSimpleProperty(java.lang.Class)

checkDependencies

protected void checkDependencies(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 - the name of the bean
mergedBeanDefinition - the bean definition the bean was created with
bw - the BeanWrapper the bean was created with
pvs - the property values to be applied to the bean
Throws:
UnsatisfiedDependencyException
See Also:
isExcludedFromDependencyCheck(java.beans.PropertyDescriptor)

isExcludedFromDependencyCheck

protected boolean isExcludedFromDependencyCheck(PropertyDescriptor pd)
Determine whether the given bean property is excluded from dependency checks.

This implementation excludes properties defined by CGLIB and properties whose type matches an ignored dependency type or which are defined by an ignored dependency interface.

Parameters:
pd - the PropertyDescriptor of the bean property
Returns:
whether the bean property is excluded
See Also:
AutowireUtils.isExcludedFromDependencyCheck(java.beans.PropertyDescriptor), ignoreDependencyType(Class), ignoreDependencyInterface(Class), AutowireUtils.isExcludedFromDependencyCheck(java.beans.PropertyDescriptor), AutowireUtils.isSetterDefinedInInterface(java.beans.PropertyDescriptor, java.util.Set)

invokeInitMethods

protected void invokeInitMethods(String beanName,
                                 Object bean,
                                 RootBeanDefinition mergedBeanDefinition)
                          throws Throwable
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 or defines a custom init method, and invoking the necessary callback(s) if it does.

To be called by createBean implementations of concrete subclasses.

Parameters:
beanName - the bean has in the factory. Used for debug output.
bean - new bean instance we may need to initialize
mergedBeanDefinition - the bean definition that the bean was created with (can also be null, if initializing )
Throws:
Throwable - if thrown by init methods or by the invocation process
See Also:
invokeCustomInitMethod(java.lang.String, java.lang.Object, java.lang.String), createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[])

invokeCustomInitMethod

protected void invokeCustomInitMethod(String beanName,
                                      Object bean,
                                      String initMethodName)
                               throws Throwable
Invoke the specified custom init method on the given bean. Called by invokeInitMethods.

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

Parameters:
beanName - the bean has in the factory. Used for debug output.
bean - new bean instance we may need to initialize
initMethodName - the name of the custom init method
Throws:
Throwable
See Also:
invokeInitMethods(java.lang.String, java.lang.Object, org.springframework.beans.factory.support.RootBeanDefinition)

findMatchingBeans

protected abstract Map findMatchingBeans(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)


Copyright (c) 2002-2005 The Spring Framework Project.