The Spring Framework

org.springframework.beans.factory.support
Class AbstractAutowireCapableBeanFactory

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

public abstract class AbstractAutowireCapableBeanFactory
extends AbstractBeanFactory
implements AutowireCapableBeanFactory

Abstract bean factory 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(java.lang.Class, int, boolean) 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 findAutowireCandidates(java.lang.String, java.lang.Class), 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 and SPI view of such a factory, respectively.

Since:
13.02.2004
Author:
Rod Johnson, Juergen Hoeller, Rob Harrop
See Also:
RootBeanDefinition, DefaultListableBeanFactory, BeanDefinitionRegistry

Field Summary
 
Fields inherited from class org.springframework.beans.factory.support.DefaultSingletonBeanRegistry
logger
 
Fields inherited from interface org.springframework.beans.factory.config.AutowireCapableBeanFactory
AUTOWIRE_AUTODETECT, AUTOWIRE_BY_NAME, AUTOWIRE_BY_TYPE, AUTOWIRE_CONSTRUCTOR, AUTOWIRE_NO
 
Fields inherited from interface org.springframework.beans.factory.BeanFactory
FACTORY_BEAN_PREFIX
 
Fields inherited from interface org.springframework.beans.factory.config.ConfigurableBeanFactory
SCOPE_PROTOTYPE, SCOPE_SINGLETON
 
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 specified bean definition (by class and name), 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.
protected  void applyPropertyValues(String beanName, RootBeanDefinition mbd, BeanWrapper bw, PropertyValues pvs)
          Apply the given property values, resolving any runtime references to other beans in this bean factory.
 Object autowire(Class beanClass, int autowireMode, boolean dependencyCheck)
          Instantiate 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 mbd, 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 mbd, BeanWrapper bw, MutablePropertyValues pvs)
          Abstract method defining "autowire by type" (bean properties by type) behavior.
protected  BeanWrapper autowireConstructor(String beanName, RootBeanDefinition mbd, Constructor ctor)
          "autowire constructor" (with constructor arguments by type) behavior.
protected  void checkDependencies(String beanName, RootBeanDefinition mbd, PropertyDescriptor[] pds, PropertyValues pvs)
          Perform a dependency check that all properties exposed have been set, if desired.
 Object configureBean(Object existingBean, String beanName)
          Configure the given bean instance: autowiring bean properties, applying bean property values, applying factory callbacks such as setBeanName and setBeanFactory, and also applying all bean post processors.
 void copyConfigurationFrom(ConfigurableBeanFactory otherFactory)
          Copy all relevant configuration from the given other factory.
 Object createBean(Class beanClass, int autowireMode, boolean dependencyCheck)
          Fully create a new bean instance of the given class with the specified autowire strategy.
protected  Object createBean(String beanName, RootBeanDefinition mbd, Object[] args)
          Central method of this class: creates a bean instance, populates the bean instance, applies post-processors, etc.
protected  BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, Object[] args)
          Create a new instance for the specified bean, using an appropriate instantiation strategy: factory method, constructor autowiring, or simple instantiation.
protected  Constructor determineConstructorFromBeanPostProcessors(Class beanClass, String beanName)
          Determine the constructor to use for the given bean, checking all registered SmartInstantiationAwareBeanPostProcessors.
protected  PropertyDescriptor[] filterPropertyDescriptorsForDependencyCheck(BeanWrapper bw)
          Extract a filtered set of PropertyDescriptors from the given BeanWrapper, excluding ignored dependency types or properties defined on ignored dependency interfaces.
protected  Map findAutowireCandidates(String beanName, Class requiredType)
          Find bean instances that match the required type.
protected  Map findMatchingBeans(Class requiredType)
          Deprecated. as of Spring 2.0.1: Override findAutowireCandidates instead
protected  InstantiationStrategy getInstantiationStrategy()
          Return the instantiation strategy to use for creating bean instances.
protected  Class getTypeForFactoryBean(String beanName, RootBeanDefinition mbd)
          This implementation checks the FactoryBean's getObjectType method on a plain instance of the FactoryBean, without bean properties applied yet.
protected  Class getTypeForFactoryMethod(String beanName, RootBeanDefinition mbd)
          Determine the bean type for the given bean definition which is based on a factory method.
 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.
 Object initializeBean(Object existingBean, String beanName)
          Initialize the given bean instance, applying factory callbacks such as setBeanName and setBeanFactory, also applying all bean post processors.
protected  Object initializeBean(String beanName, Object bean, RootBeanDefinition mbd)
          Initialize the given bean instance, applying factory callbacks as well as init methods and bean post processors.
protected  BeanWrapper instantiateBean(String beanName, RootBeanDefinition mbd)
          Instantiate the given bean using its default constructor.
protected  BeanWrapper instantiateUsingFactoryMethod(String beanName, RootBeanDefinition mbd, Object[] explicitArgs)
          Instantiate the bean using a named factory method.
protected  void invokeCustomInitMethod(String beanName, Object bean, String initMethodName, boolean enforceInitMethod)
          Invoke the specified custom init method on the given bean.
protected  void invokeInitMethods(String beanName, Object bean, RootBeanDefinition mbd)
          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 mbd, BeanWrapper bw)
          Populate the bean instance in the given BeanWrapper with the property values from the bean definition.
protected  Object postProcessObjectFromFactoryBean(Object object, String beanName)
          Applies the postProcessAfterInitialization callback of all registered BeanPostProcessors, giving them a chance to post-process the object obtained from FactoryBeans (for example, to auto-proxy them).
protected  Class predictBeanType(String beanName, RootBeanDefinition mbd)
          Predict the eventual bean type for the given bean.
protected  void removeSingleton(String beanName)
          Overridden to clear FactoryBean instance cache as well.
 void setAllowCircularReferences(boolean allowCircularReferences)
          Set whether to allow circular references between beans - and automatically try to resolve them.
 void setAllowRawInjectionDespiteWrapping(boolean allowRawInjectionDespiteWrapping)
          Set whether to allow the raw injection of a bean instance into some other bean's property, despite the injected bean eventually getting wrapped (for example, through AOP auto-proxying).
 void setInstantiationStrategy(InstantiationStrategy instantiationStrategy)
          Set the instantiation strategy to use for creating bean instances.
protected  String[] unsatisfiedNonSimpleProperties(RootBeanDefinition mbd, BeanWrapper bw)
          Return an array of non-simple bean properties that are unsatisfied.
 
Methods inherited from class org.springframework.beans.factory.support.AbstractBeanFactory
addBeanPostProcessor, addPropertyEditorRegistrar, afterPrototypeCreation, beforePrototypeCreation, checkMergedBeanDefinition, clearMergedBeanDefinition, containsBean, containsBeanDefinition, containsLocalBean, destroyBean, destroyBean, destroyScopedBean, getAliases, getBean, getBean, getBean, getBean, getBeanClassLoader, getBeanDefinition, getBeanPostProcessorCount, getBeanPostProcessors, getCustomEditors, getMergedBeanDefinition, getMergedBeanDefinition, getMergedBeanDefinition, getMergedBeanDefinition, getObjectForBeanInstance, getObjectFromFactoryBean, getParentBeanFactory, getPropertyEditorRegistrars, getRegisteredScope, getRegisteredScopeNames, getType, getTypeConverter, getTypeForFactoryBean, hasDestructionAwareBeanPostProcessors, hasInstantiationAwareBeanPostProcessors, initBeanWrapper, isAlias, isBeanClassMatch, isBeanNameInUse, isCacheBeanMetadata, isCurrentlyInCreation, isFactoryBean, isPrototype, isPrototypeCurrentlyInCreation, isSingleton, isTypeMatch, originalBeanName, registerAlias, registerCustomEditor, registerCustomEditors, registerDisposableBeanIfNecessary, registerScope, requiresDestruction, resolveBeanClass, setBeanClassLoader, setCacheBeanMetadata, setParentBeanFactory, transformedBeanName
 
Methods inherited from class org.springframework.beans.factory.support.DefaultSingletonBeanRegistry
addSingleton, afterSingletonCreation, beforeSingletonCreation, containsSingleton, destroyBean, destroySingleton, destroySingletons, getDependentBeans, getSingleton, getSingleton, getSingletonCount, getSingletonMutex, getSingletonNames, hasDependentBean, isSingletonCurrentlyInCreation, registerDependentBean, registerDisposableBean, registerSingleton
 
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, isPrototype, isSingleton, isTypeMatch
 
Methods inherited from interface org.springframework.beans.factory.config.ConfigurableBeanFactory
destroySingletons
 
Methods inherited from interface org.springframework.beans.factory.config.SingletonBeanRegistry
containsSingleton, getSingleton, getSingletonCount, getSingletonNames, registerSingleton
 

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

getInstantiationStrategy

protected InstantiationStrategy getInstantiationStrategy()
Return the instantiation strategy to use for creating bean instances.


setAllowCircularReferences

public void setAllowCircularReferences(boolean allowCircularReferences)
Set whether to allow circular references between beans - and automatically try to resolve them.

Note that circular reference resolution means that one of the involved beans will receive a reference to another bean that is not fully initialized yet. This can lead to subtle and not-so-subtle side effects on initialization; it does work fine for many scenarios, though.

Default is "true". Turn this off to throw an exception when encountering a circular reference, disallowing them completely.

NOTE: It is generally recommended to not rely on circular references between your beans. Refactor your application logic to have the two beans involved delegate to a third bean that encapsulates their common logic.


setAllowRawInjectionDespiteWrapping

public void setAllowRawInjectionDespiteWrapping(boolean allowRawInjectionDespiteWrapping)
Set whether to allow the raw injection of a bean instance into some other bean's property, despite the injected bean eventually getting wrapped (for example, through AOP auto-proxying).

This will only be used as a last resort in case of a circular reference that cannot be resolved otherwise: essentially, preferring a raw instance getting injected over a failure of the entire bean wiring process.

Default is "false", as of Spring 2.0. Turn this on to allow for non-wrapped raw beans injected into some of your references, which was Spring 1.2's (arguably unclean) default behavior.

NOTE: It is generally recommended to not rely on circular references between your beans, in particular with auto-proxying involved.

See Also:
setAllowCircularReferences(boolean)

ignoreDependencyType

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


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 BeanFactoryAware interface is ignored. For further types to ignore, invoke this method for each type.

See Also:
BeanFactoryAware, ApplicationContextAware

copyConfigurationFrom

public void copyConfigurationFrom(ConfigurableBeanFactory otherFactory)
Description copied from interface: ConfigurableBeanFactory
Copy all relevant configuration from the given other factory.

Should include all standard configuration settings as well as BeanPostProcessors, Scopes, and factory-specific internal settings. Should not include any metadata of actual bean definitions, such as BeanDefinition objects and bean name aliases.

Specified by:
copyConfigurationFrom in interface ConfigurableBeanFactory
Overrides:
copyConfigurationFrom in class AbstractBeanFactory
Parameters:
otherFactory - the other BeanFactory to copy from

createBean

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

Performs full initialization of the bean, including all applicable BeanPostProcessors.

Specified by:
createBean in interface AutowireCapableBeanFactory
Parameters:
beanClass - the class of the bean to create
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_NO, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT

autowire

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

Does not apply any BeanPostProcessors or perform any further initialization of the bean. This interface offers distinct, fine-grained operations for those purposes, for example AutowireCapableBeanFactory.initializeBean(java.lang.Object, java.lang.String).

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 object references in the bean instance (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_NO, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT, AutowireCapableBeanFactory.initializeBean(java.lang.Object, java.lang.String), AutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(java.lang.Object, java.lang.String), AutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(java.lang.Object, java.lang.String)

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 references in the bean instance
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 AutowireCapableBeanFactory.autowireBeanProperties(java.lang.Object, int, boolean) 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 (a bean definition of that name has to be available)
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)

configureBean

public Object configureBean(Object existingBean,
                            String beanName)
                     throws BeansException
Description copied from interface: AutowireCapableBeanFactory
Configure the given bean instance: autowiring bean properties, applying bean property values, applying factory callbacks such as setBeanName and setBeanFactory, and also applying all bean post processors.

This is effectively a superset of what initializeBean provides, fully applying the configuration specified by the corresponding bean definition.

Specified by:
configureBean in interface AutowireCapableBeanFactory
Parameters:
existingBean - the existing bean instance
beanName - the name of the bean, to be passed to it if necessary (a bean definition of that name has to be available)
Returns:
the bean instance to use, either the original or a wrapped one
Throws:
NoSuchBeanDefinitionException - if there is no bean with the given name
BeansException - if the initialization failed
See Also:
AutowireCapableBeanFactory.initializeBean(java.lang.Object, java.lang.String)

initializeBean

public Object initializeBean(Object existingBean,
                             String beanName)
Description copied from interface: AutowireCapableBeanFactory
Initialize the given bean instance, applying factory callbacks such as setBeanName and setBeanFactory, also applying all bean post processors.

Note that no bean definition of the given name has to exist in the bean factory. The passed-in bean name will simply be used for callbacks but not checked against the registered bean definitions.

Specified by:
initializeBean in interface AutowireCapableBeanFactory
Parameters:
existingBean - the existing bean instance
beanName - the name of the bean, to be passed to it if necessary (only passed to BeanPostProcessors)
Returns:
the bean instance to use, either the original or a wrapped one

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 mbd,
                            Object[] args)
                     throws BeanCreationException
Central method of this class: creates a bean instance, populates the bean instance, applies post-processors, etc.

Differentiates between default bean instantiation, use of a factory method, and autowiring a constructor.

Specified by:
createBean in class AbstractBeanFactory
Parameters:
beanName - the name of the bean
mbd - the merged 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
See Also:
instantiateBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition), instantiateUsingFactoryMethod(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[]), autowireConstructor(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.reflect.Constructor)

predictBeanType

protected Class predictBeanType(String beanName,
                                RootBeanDefinition mbd)
Predict the eventual bean type for the given bean.

Overrides:
predictBeanType in class AbstractBeanFactory
Parameters:
beanName - the name of the bean
mbd - the merged bean definition to determine the type for
Returns:
the type of the bean, or null if not predictable

getTypeForFactoryMethod

protected Class getTypeForFactoryMethod(String beanName,
                                        RootBeanDefinition mbd)
Determine the bean type for the given bean definition which is based on a factory method. Only called if there is no singleton instance registered for the target bean already.

This implementation determines the type matching createBean(java.lang.Class, int, boolean)'s different creation strategies. As far as possible, we'll perform static type checking to avoid creation of the target bean.

Parameters:
beanName - the name of the bean (for error handling purposes)
mbd - the merged bean definition for the bean
Returns:
the type for the bean if determinable, or null else
See Also:
createBean(java.lang.Class, int, boolean)

getTypeForFactoryBean

protected Class getTypeForFactoryBean(String beanName,
                                      RootBeanDefinition mbd)
This implementation checks the FactoryBean's getObjectType method on a plain instance of the FactoryBean, without bean properties applied yet. If this doesn't return a type yet, a full creation of the FactoryBean is used as fallback (through delegation to the superclass's implementation).

The shortcut check for a FactoryBean is only applied in case of a singleton FactoryBean. If the FactoryBean instance itself is not kept as singleton, it will be fully created to check the type of its exposed object.

Overrides:
getTypeForFactoryBean in class AbstractBeanFactory
Parameters:
beanName - the name of the bean
mbd - the merged bean definition for the bean
Returns:
the type for the bean if determinable, or null else
See Also:
FactoryBean.getObjectType(), AbstractBeanFactory.getBean(String)

applyBeanPostProcessorsBeforeInstantiation

protected Object applyBeanPostProcessorsBeforeInstantiation(Class beanClass,
                                                            String beanName)
                                                     throws BeansException
Apply InstantiationAwareBeanPostProcessors to the specified bean definition (by class and name), invoking their postProcessBeforeInstantiation methods.

Any returned object will be used as the bean instead of actually instantiating the target bean. A null return value from the post-processor will result in the target bean being instantiated.

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, or null
Throws:
BeansException - if any post-processing failed
See Also:
InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(java.lang.Class, java.lang.String)

createBeanInstance

protected BeanWrapper createBeanInstance(String beanName,
                                         RootBeanDefinition mbd,
                                         Object[] args)
Create a new instance for the specified bean, using an appropriate instantiation strategy: factory method, constructor autowiring, or simple instantiation.

Parameters:
beanName - the name of the bean
mbd - the bean definition for the bean
args - arguments to use if creating a prototype using explicit arguments to a static factory method. It is invalid to use a non-null args value in any other case.
Returns:
BeanWrapper for the new instance
See Also:
instantiateUsingFactoryMethod(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[]), autowireConstructor(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.reflect.Constructor), instantiateBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition)

determineConstructorFromBeanPostProcessors

protected Constructor determineConstructorFromBeanPostProcessors(Class beanClass,
                                                                 String beanName)
                                                          throws BeansException
Determine the constructor to use for the given bean, checking all registered SmartInstantiationAwareBeanPostProcessors.

Parameters:
beanClass - the raw class of the bean
beanName - the name of the bean
Returns:
the constructor to use, or null if none specified
Throws:
BeansException - in case of errors
See Also:
SmartInstantiationAwareBeanPostProcessor.determineConstructor(java.lang.Class, java.lang.String)

instantiateBean

protected BeanWrapper instantiateBean(String beanName,
                                      RootBeanDefinition mbd)
Instantiate the given bean using its default constructor.

Parameters:
beanName - the name of the bean
mbd - the bean definition for the bean
Returns:
BeanWrapper for the new instance

instantiateUsingFactoryMethod

protected BeanWrapper instantiateUsingFactoryMethod(String beanName,
                                                    RootBeanDefinition mbd,
                                                    Object[] explicitArgs)
Instantiate the bean using a named factory method. The method may be static, if the mbd 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 - the name of the bean
mbd - the bean definition for the bean
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
See Also:
AbstractBeanFactory.getBean(String, Object[])

autowireConstructor

protected BeanWrapper autowireConstructor(String beanName,
                                          RootBeanDefinition mbd,
                                          Constructor ctor)
"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 - the name of the bean
mbd - the bean definition for the bean
ctor - the chosen candidate constructor
Returns:
BeanWrapper for the new instance

populateBean

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

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

autowireByName

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

Parameters:
beanName - the name of the bean we're wiring up. Useful for debugging messages; not used functionally.
mbd - 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(String beanName,
                              RootBeanDefinition mbd,
                              BeanWrapper bw,
                              MutablePropertyValues pvs)
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 - the name of the bean to autowire by type
mbd - the merged 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

unsatisfiedNonSimpleProperties

protected String[] unsatisfiedNonSimpleProperties(RootBeanDefinition mbd,
                                                  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:
mbd - the merged 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)

filterPropertyDescriptorsForDependencyCheck

protected PropertyDescriptor[] filterPropertyDescriptorsForDependencyCheck(BeanWrapper bw)
Extract a filtered set of PropertyDescriptors from the given BeanWrapper, excluding ignored dependency types or properties defined on ignored dependency interfaces.

Parameters:
bw - the BeanWrapper the bean was created with
Returns:
the filtered PropertyDescriptors
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:
ignoreDependencyType(Class), ignoreDependencyInterface(Class)

checkDependencies

protected void checkDependencies(String beanName,
                                 RootBeanDefinition mbd,
                                 PropertyDescriptor[] pds,
                                 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
mbd - the merged bean definition the bean was created with
pds - the relevant property descriptors for the target bean
pvs - the property values to be applied to the bean
Throws:
UnsatisfiedDependencyException
See Also:
isExcludedFromDependencyCheck(java.beans.PropertyDescriptor)

applyPropertyValues

protected void applyPropertyValues(String beanName,
                                   RootBeanDefinition mbd,
                                   BeanWrapper bw,
                                   PropertyValues pvs)
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 - the bean name passed for better exception information
mbd - the merged bean definition
bw - the BeanWrapper wrapping the target object
pvs - the new property values

initializeBean

protected Object initializeBean(String beanName,
                                Object bean,
                                RootBeanDefinition mbd)
Initialize the given bean instance, applying factory callbacks as well as init methods and bean post processors.

Called from createBean(java.lang.Class, int, boolean) for traditionally defined beans, and from initializeBean(java.lang.Object, java.lang.String) for existing bean instances.

Parameters:
beanName - the bean name in the factory (for debugging purposes)
bean - the new bean instance we may need to initialize
mbd - the bean definition that the bean was created with (can also be null, if given an existing bean instance)
Returns:
the initialized bean instance (potentially wrapped)
See Also:
BeanNameAware, BeanClassLoaderAware, BeanFactoryAware, applyBeanPostProcessorsBeforeInitialization(java.lang.Object, java.lang.String), invokeInitMethods(java.lang.String, java.lang.Object, org.springframework.beans.factory.support.RootBeanDefinition), applyBeanPostProcessorsAfterInitialization(java.lang.Object, java.lang.String)

invokeInitMethods

protected void invokeInitMethods(String beanName,
                                 Object bean,
                                 RootBeanDefinition mbd)
                          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.

Parameters:
beanName - the bean name in the factory (for debugging purposes)
bean - the new bean instance we may need to initialize
mbd - the merged bean definition that the bean was created with (can also be null, if given an existing bean instance)
Throws:
Throwable - if thrown by init methods or by the invocation process
See Also:
invokeCustomInitMethod(java.lang.String, java.lang.Object, java.lang.String, boolean)

invokeCustomInitMethod

protected void invokeCustomInitMethod(String beanName,
                                      Object bean,
                                      String initMethodName,
                                      boolean enforceInitMethod)
                               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 name in the factory (for debugging purposes)
bean - the new bean instance we may need to initialize
initMethodName - the name of the custom init method
enforceInitMethod - indicates whether the defined init method needs to exist
Throws:
Throwable
See Also:
invokeInitMethods(java.lang.String, java.lang.Object, org.springframework.beans.factory.support.RootBeanDefinition)

postProcessObjectFromFactoryBean

protected Object postProcessObjectFromFactoryBean(Object object,
                                                  String beanName)
Applies the postProcessAfterInitialization callback of all registered BeanPostProcessors, giving them a chance to post-process the object obtained from FactoryBeans (for example, to auto-proxy them).

Overrides:
postProcessObjectFromFactoryBean in class AbstractBeanFactory
Parameters:
object - the object obtained from the FactoryBean.
beanName - the name of the bean
Returns:
the object to expose
See Also:
applyBeanPostProcessorsAfterInitialization(java.lang.Object, java.lang.String)

removeSingleton

protected void removeSingleton(String beanName)
Overridden to clear FactoryBean instance cache as well.

Overrides:
removeSingleton in class AbstractBeanFactory
Parameters:
beanName - the name of the bean

findAutowireCandidates

protected Map findAutowireCandidates(String beanName,
                                     Class requiredType)
                              throws BeansException
Find bean instances that match the required type. Called during autowiring for the specified bean.

If a subclass cannot obtain information about bean names by type, a corresponding exception should be thrown.

Parameters:
beanName - the name of the bean that is about to be wired
requiredType - the type of the autowired property or argument
Returns:
a Map of candidate names and candidate instances that match the required type (never null)
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, java.lang.reflect.Constructor)

findMatchingBeans

protected Map findMatchingBeans(Class requiredType)
                         throws BeansException
Deprecated. as of Spring 2.0.1: Override findAutowireCandidates instead

Find bean instances that match the required type. Called by autowiring.

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

The Spring Framework

Copyright © 2002-2007 The Spring Framework.