|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.beans.factory.support.AbstractBeanFactory org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
public abstract class AbstractAutowireCapableBeanFactory
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.
AutowireCapableBeanFactory
,
AbstractBeanFactory.createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[])
,
RootBeanDefinition
,
findMatchingBeans(Class)
,
DefaultListableBeanFactory
,
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 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. |
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)
Central method of this class: creates a bean instance, populates the bean instance, applies post-processors, etc. |
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. |
InstantiationStrategy |
getInstantiationStrategy()
Return the current instantiation strategy. |
protected Class |
getTypeForFactoryMethod(String beanName,
RootBeanDefinition mergedBeanDefinition)
This implementation determines the type matching createBean 's
different creation strategies. |
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 |
instantiateBean(String beanName,
RootBeanDefinition mergedBeanDefinition)
Instantiate the given bean using its default constructor. |
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,
boolean enforceInitMethod)
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). |
boolean |
isAllowCircularReferences()
Return whether to allow circular references between beans - and automatically try to resolve them. |
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 |
setAllowCircularReferences(boolean allowCircularReferences)
Set whether to allow circular references between beans - and automatically try to resolve them. |
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 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 |
---|
public AbstractAutowireCapableBeanFactory()
public AbstractAutowireCapableBeanFactory(BeanFactory parentBeanFactory)
parentBeanFactory
- parent bean factory, or null
if noneMethod Detail |
---|
public void setInstantiationStrategy(InstantiationStrategy instantiationStrategy)
public InstantiationStrategy getInstantiationStrategy()
public void setAllowCircularReferences(boolean allowCircularReferences)
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.
public boolean isAllowCircularReferences()
public void ignoreDependencyType(Class type)
public Set getIgnoredDependencyTypes()
public void ignoreDependencyInterface(Class ifc)
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.
BeanFactoryAware
,
ApplicationContextAware
public Set getIgnoredDependencyInterfaces()
public Object autowire(Class beanClass, int autowireMode, boolean dependencyCheck) throws BeansException
AutowireCapableBeanFactory
autowire
in interface AutowireCapableBeanFactory
beanClass
- the class of the bean to instantiateautowireMode
- by name or type, using the constants in this interfacedependencyCheck
- whether to perform a dependency check for objects
(not applicable to autowiring a constructor, thus ignored there)
BeansException
- if instantiation or wiring failedAutowireCapableBeanFactory.AUTOWIRE_BY_NAME
,
AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE
,
AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR
,
AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT
public void autowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck) throws BeansException
AutowireCapableBeanFactory
autowireBeanProperties
in interface AutowireCapableBeanFactory
existingBean
- the existing bean instanceautowireMode
- by name or type, using the constants in this interfacedependencyCheck
- whether to perform a dependency check for object
BeansException
- if wiring failedAutowireCapableBeanFactory.AUTOWIRE_BY_NAME
,
AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE
public void applyBeanPropertyValues(Object existingBean, String beanName) throws BeansException
AutowireCapableBeanFactory
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.
applyBeanPropertyValues
in interface AutowireCapableBeanFactory
existingBean
- the existing bean instancebeanName
- the name of the bean definition in the bean factory
NoSuchBeanDefinitionException
- if there is no bean with the given name
BeansException
- if applying the property values failedAutowireCapableBeanFactory.autowireBeanProperties(java.lang.Object, int, boolean)
protected Object applyBeanPostProcessorsBeforeInstantiation(Class beanClass, String beanName) throws BeansException
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.
beanClass
- the class of the bean to be instantiatedbeanName
- the name of the bean
null
BeansException
- if any post-processing failedInstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(java.lang.Class, java.lang.String)
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException
AutowireCapableBeanFactory
postProcessBeforeInitialization
methods.
The returned bean instance may be a wrapper around the original.
applyBeanPostProcessorsBeforeInitialization
in interface AutowireCapableBeanFactory
existingBean
- the new bean instancebeanName
- the name of the bean
BeansException
- if any post-processing failedBeanPostProcessor.postProcessBeforeInitialization(java.lang.Object, java.lang.String)
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException
AutowireCapableBeanFactory
postProcessAfterInitialization
methods.
The returned bean instance may be a wrapper around the original.
applyBeanPostProcessorsAfterInitialization
in interface AutowireCapableBeanFactory
existingBean
- the new bean instancebeanName
- the name of the bean
BeansException
- if any post-processing failedBeanPostProcessor.postProcessAfterInitialization(java.lang.Object, java.lang.String)
protected Object createBean(String beanName, RootBeanDefinition mergedBeanDefinition, Object[] args) throws BeanCreationException
Differentiates between default bean instantiation, use of a factory method, and autowiring a constructor.
createBean
in class AbstractBeanFactory
beanName
- the name of the beanmergedBeanDefinition
- the bean definition for the beanargs
- arguments to use if creating a prototype using explicit arguments to a
static factory method. This parameter must be null
except in this case.
BeanCreationException
- if the bean could not be createdinstantiateBean(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)
protected Class getTypeForFactoryMethod(String beanName, RootBeanDefinition mergedBeanDefinition)
createBean
's
different creation strategies.
As far as possible, we'll perform static type checking to avoid creation of the target bean. However, in case of a non-lazy-init singleton with a "factory-bean" reference, checking the actual return type is preferable: For example, a generic service factory might be declared with return type "Object" or a generic service marker interface, where static type checking wouldn't reveal any actual type.
getTypeForFactoryMethod
in class AbstractBeanFactory
beanName
- name of the beanmergedBeanDefinition
- the bean definition for the bean
null
elseAbstractBeanFactory.createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[])
protected BeanWrapper instantiateBean(String beanName, RootBeanDefinition mergedBeanDefinition) throws BeansException
beanName
- name of the beanmergedBeanDefinition
- the bean definition for the bean
BeansException
protected BeanWrapper instantiateUsingFactoryMethod(String beanName, RootBeanDefinition mergedBeanDefinition, Object[] explicitArgs) throws BeansException
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.
beanName
- name of the beanmergedBeanDefinition
- the bean definition for the beanexplicitArgs
- argument values passed in programmatically via the getBean
method, or null
if none (-> use constructor argument values from bean definition)
BeansException
AbstractBeanFactory.getBean(String, Object[])
protected BeanWrapper autowireConstructor(String beanName, RootBeanDefinition mergedBeanDefinition) throws BeansException
This corresponds to constructor injection: In this mode, a Spring bean factory is able to host components that expect constructor-based dependency resolution.
beanName
- name of the beanmergedBeanDefinition
- the bean definition for the bean
BeansException
protected void populateBean(String beanName, RootBeanDefinition mergedBeanDefinition, BeanWrapper bw) throws BeansException
beanName
- name of the beanmergedBeanDefinition
- the bean definition for the beanbw
- BeanWrapper with bean instance
BeansException
protected void autowireByName(String beanName, RootBeanDefinition mergedBeanDefinition, BeanWrapper bw, MutablePropertyValues pvs) throws BeansException
beanName
- name of the bean we're wiring up.
Useful for debugging messages; not used functionally.mergedBeanDefinition
- bean definition to update through autowiringbw
- BeanWrapper from which we can obtain information about the beanpvs
- the PropertyValues to register wired objects with
BeansException
protected void autowireByType(String beanName, RootBeanDefinition mergedBeanDefinition, BeanWrapper bw, MutablePropertyValues pvs) throws BeansException
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.
beanName
- name of the bean to autowire by typemergedBeanDefinition
- bean definition to update through autowiringbw
- BeanWrapper from which we can obtain information about the beanpvs
- the PropertyValues to register wired objects with
BeansException
protected String[] unsatisfiedNonSimpleProperties(RootBeanDefinition mergedBeanDefinition, BeanWrapper bw)
mergedBeanDefinition
- the bean definition the bean was created withbw
- the BeanWrapper the bean was created with
BeanUtils.isSimpleProperty(java.lang.Class)
protected void checkDependencies(String beanName, RootBeanDefinition mergedBeanDefinition, BeanWrapper bw, PropertyValues pvs) throws UnsatisfiedDependencyException
beanName
- the name of the beanmergedBeanDefinition
- the bean definition the bean was created withbw
- the BeanWrapper the bean was created withpvs
- the property values to be applied to the bean
UnsatisfiedDependencyException
isExcludedFromDependencyCheck(java.beans.PropertyDescriptor)
protected boolean isExcludedFromDependencyCheck(PropertyDescriptor pd)
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.
pd
- the PropertyDescriptor of the bean property
AutowireUtils.isExcludedFromDependencyCheck(java.beans.PropertyDescriptor)
,
ignoreDependencyType(Class)
,
ignoreDependencyInterface(Class)
,
AutowireUtils.isExcludedFromDependencyCheck(java.beans.PropertyDescriptor)
,
AutowireUtils.isSetterDefinedInInterface(java.beans.PropertyDescriptor, java.util.Set)
protected void invokeInitMethods(String beanName, Object bean, RootBeanDefinition mergedBeanDefinition) throws Throwable
beanName
- the bean has in the factory. Used for debug output.bean
- new bean instance we may need to initializemergedBeanDefinition
- the bean definition that the bean was created with
(can also be null
, if given an existing bean instance)
Throwable
- if thrown by init methods or by the invocation processinvokeCustomInitMethod(java.lang.String, java.lang.Object, java.lang.String, boolean)
protected void invokeCustomInitMethod(String beanName, Object bean, String initMethodName, boolean enforceInitMethod) throws Throwable
Can be overridden in subclasses for custom resolution of init methods with arguments.
beanName
- the bean has in the factory. Used for debug output.bean
- new bean instance we may need to initializeinitMethodName
- the name of the custom init methodenforceInitMethod
- indicates whether the defined init method needs to exist
Throwable
invokeInitMethods(java.lang.String, java.lang.Object, org.springframework.beans.factory.support.RootBeanDefinition)
protected abstract Map findMatchingBeans(Class requiredType) throws BeansException
requiredType
- the type of the beans to look up
null
if none found
BeansException
- in case of errorsautowireByType(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)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |