|
|||||||||||
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
Abstract superclass for BeanFactory implementations, implementing the ConfigurableBeanFactory SPI interface. Does not assume a listable bean factory: can therefore also be used as base class for bean factory implementations which fetch bean definitions from a variety of backend resources (where bean definition access is an expensive operation).
This class provides singleton/prototype determination, singleton cache, aliases, FactoryBean handling, bean definition merging for child bean definitions, and bean destruction (DisposableBean interface, custom destroy methods). Furthermore, it can manage a bean factory hierarchy, through implementing the HierarchicalBeanFactory interface (superinterface of ConfigurableBeanFactory).
The main template methods to be implemented by subclasses are
getBeanDefinition
and createBean
, retrieving a bean
definition for a given bean name respectively creating a bean instance for a
given bean definition. Default implementations for those can be found in
DefaultListableBeanFactory respectively AbstractAutowireCapableBeanFactory.
getBeanDefinition(java.lang.String)
,
createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[])
,
HierarchicalBeanFactory
,
DisposableBean
,
RootBeanDefinition
,
ChildBeanDefinition
,
AbstractAutowireCapableBeanFactory.createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[])
,
DefaultListableBeanFactory.getBeanDefinition(java.lang.String)
Field Summary | |
protected Log |
logger
Logger available to subclasses |
Fields inherited from interface org.springframework.beans.factory.BeanFactory |
FACTORY_BEAN_PREFIX |
Constructor Summary | |
AbstractBeanFactory()
Create a new AbstractBeanFactory. |
|
AbstractBeanFactory(BeanFactory parentBeanFactory)
Create a new AbstractBeanFactory with the given parent. |
Method Summary | |
void |
addBeanPostProcessor(BeanPostProcessor beanPostProcessor)
Add a new BeanPostPrcoessor that will get applied to beans created by this factory. |
protected void |
addSingleton(String beanName,
Object singletonObject)
Add the given singleton object to the singleton cache of this factory. |
protected void |
checkMergedBeanDefinition(RootBeanDefinition mergedBeanDefinition,
String beanName,
Class requiredType,
Object[] args)
Check the given merged bean definition, potentially throwing validation exceptions. |
boolean |
containsBean(String name)
Does this bean factory contain a bean definition with the given name? |
protected abstract boolean |
containsBeanDefinition(String beanName)
Check if this bean factory contains a bean definition with the given name. |
protected abstract Object |
createBean(String beanName,
RootBeanDefinition mergedBeanDefinition,
Object[] args)
Create a bean instance for the given bean definition. |
protected void |
destroyBean(String beanName,
Object bean)
Destroy the given bean. |
void |
destroySingletons()
Destroy all cached singletons in this factory. |
String[] |
getAliases(String name)
Return the aliases for the given bean name, if defined. |
Object |
getBean(String name)
Return an instance, which may be shared or independent, of the given bean name. |
Object |
getBean(String name,
Class requiredType)
Return an instance (possibly shared or independent) of the given bean name. |
Object |
getBean(String name,
Class requiredType,
Object[] args)
Return the bean with the given name, checking the parent bean factory if not found. |
Object |
getBean(String name,
Object[] args)
Return the bean with the given name, checking the parent bean factory if not found. |
protected abstract BeanDefinition |
getBeanDefinition(String beanName)
Return the bean definition for the given bean name. |
List |
getBeanPostProcessors()
Return the list of BeanPostProcessors that will get applied to beans created with this factory. |
Map |
getCustomEditors()
Return the map of custom editors, with Classes as keys and PropertyEditors as values. |
Set |
getIgnoredDependencyTypes()
Return the set of classes that will get ignored for autowiring. |
protected RootBeanDefinition |
getMergedBeanDefinition(String beanName,
BeanDefinition bd)
Return a RootBeanDefinition for the given bean name, by merging with the parent if the given original bean definition is a child bean definition. |
protected RootBeanDefinition |
getMergedBeanDefinition(String beanName,
boolean includingAncestors)
Return a RootBeanDefinition, even by traversing parent if the parameter is a child definition. |
protected Object |
getObjectForSharedInstance(String name,
Object beanInstance)
Get the object for the given shared bean, either the bean instance itself or its created object in case of a FactoryBean. |
BeanFactory |
getParentBeanFactory()
Return the parent bean factory, or null if there is none. |
String[] |
getSingletonNames()
Return the names of beans in the singleton cache. |
Class |
getType(String name)
Determine the type of the bean with the given name. |
void |
ignoreDependencyType(Class type)
Ignore the given dependency type for autowiring. |
protected void |
initBeanWrapper(BeanWrapper bw)
Initialize the given BeanWrapper with the custom editors registered with this factory. |
protected void |
invokeCustomDestroyMethod(String beanName,
Object bean,
String destroyMethodName)
Invoke the specified custom destroy method on the given bean. |
protected void |
invokeDestroyMethods(String beanName,
Object bean)
Give a bean a chance to react to the shutdown of the bean factory. |
protected boolean |
isFactoryBean(String name)
Determine whether the bean with the given name is a FactoryBean. |
protected boolean |
isFactoryDereference(String name)
Return whether the given name is a factory dereference (beginning with the factory dereference prefix). |
boolean |
isSingleton(String name)
Is this bean a singleton? |
void |
registerAlias(String beanName,
String alias)
Given a bean name, create an alias. |
void |
registerCustomEditor(Class requiredType,
PropertyEditor propertyEditor)
Register the given custom property editor for all properties of the given type. |
protected void |
registerDependentBean(String beanName,
String dependentBeanName)
Register a dependent bean for the given bean, to be destroyed before the given bean is destroyed. |
protected void |
registerDisposableBean(String beanName,
DisposableBean bean)
Add the given bean to the list of further disposable beans in this factory. |
protected void |
registerDisposableBean(String beanName,
Object bean,
String destroyMethodName)
Add the given bean to the list of further disposable beans in this factory, registering the given destroy method to be called on factory shutdown. |
void |
registerSingleton(String beanName,
Object singletonObject)
Register the given existing object as singleton in the bean factory, under the given bean name. |
protected void |
removeSingleton(String beanName)
Remove the bean with the given name from the singleton cache of this factory. |
void |
setParentBeanFactory(BeanFactory parentBeanFactory)
Set the parent of this bean factory. |
protected String |
transformedBeanName(String name)
Return the bean name, stripping out the factory dereference prefix if necessary, and resolving aliases to canonical names. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected final Log logger
Constructor Detail |
public AbstractBeanFactory()
public AbstractBeanFactory(BeanFactory parentBeanFactory)
parentBeanFactory
- parent bean factory, or null if nonegetBean(java.lang.String)
Method Detail |
public Object getBean(String name) throws BeansException
BeanFactory
Callers may retain references to returned objects in the case of Singleton beans.
This method delegates to the parent factory if the bean cannot be found in this factory instance.
getBean
in interface BeanFactory
name
- the name of the bean to return
BeansException
- if the bean could not be obtainedpublic Object getBean(String name, Class requiredType) throws BeansException
BeanFactory
Behaves the same as getBean(String), but provides a measure of type safety by throwing a Spring BeansException if the bean is not of the required type. This means that ClassCastException can't be thrown on casting the result correctly, as can happen with getBean(String).
getBean
in interface BeanFactory
name
- the name of the bean to returnrequiredType
- type the bean must match. Can be an interface or superclass
of the actual class, or null for any match. For example, if the value is
Object.class, this method will succeed whatever the class of the returned instance.
BeansException
- if the bean could not be createdpublic Object getBean(String name, Object[] args) throws BeansException
name
- the name of the bean to retrieveargs
- 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.
BeansException
public Object getBean(String name, Class requiredType, Object[] args) throws BeansException
name
- the name of the bean to retrieverequiredType
- the required type of the bean to retrieveargs
- 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.
TODO: We could consider supporting this for constructor args also, but it's really a
corner case required for AspectJ integration.
BeansException
public boolean containsBean(String name)
BeanFactory
Will ask the parent factory if the bean cannot be found in this factory instance.
containsBean
in interface BeanFactory
name
- the name of the bean to query
public boolean isSingleton(String name) throws NoSuchBeanDefinitionException
BeanFactory
Will ask the parent factory if the bean cannot be found in this factory instance.
isSingleton
in interface BeanFactory
name
- the name of the bean to query
NoSuchBeanDefinitionException
- if there is no bean with the given namepublic Class getType(String name) throws NoSuchBeanDefinitionException
BeanFactory
getType
in interface BeanFactory
name
- the name of the bean to query
NoSuchBeanDefinitionException
- if there is no bean with the given nameBeanFactory.getBean(java.lang.String)
,
FactoryBean.getObjectType()
public String[] getAliases(String name) throws NoSuchBeanDefinitionException
BeanFactory
Will ask the parent factory if the bean cannot be found in this factory instance.
getAliases
in interface BeanFactory
name
- the bean name to check for aliases
NoSuchBeanDefinitionException
- if there's no such bean definitionpublic BeanFactory getParentBeanFactory()
HierarchicalBeanFactory
getParentBeanFactory
in interface HierarchicalBeanFactory
public void setParentBeanFactory(BeanFactory parentBeanFactory)
ConfigurableBeanFactory
Note that the parent shouldn't be changed: It should only be set outside a constructor if it isn't available when an object of this class is created.
setParentBeanFactory
in interface ConfigurableBeanFactory
parentBeanFactory
- the parent bean factorypublic void registerCustomEditor(Class requiredType, PropertyEditor propertyEditor)
ConfigurableBeanFactory
registerCustomEditor
in interface ConfigurableBeanFactory
requiredType
- type of the propertypropertyEditor
- editor to registerpublic Map getCustomEditors()
public void ignoreDependencyType(Class type)
ConfigurableBeanFactory
This will typically be used for dependencies that are resolved in other ways, like BeanFactory through BeanFactoryAware or ApplicationContext through ApplicationContextAware.
ignoreDependencyType
in interface ConfigurableBeanFactory
BeanFactoryAware
,
ApplicationContextAware
public Set getIgnoredDependencyTypes()
public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor)
ConfigurableBeanFactory
addBeanPostProcessor
in interface ConfigurableBeanFactory
beanPostProcessor
- the bean processor to registerpublic List getBeanPostProcessors()
protected void initBeanWrapper(BeanWrapper bw)
bw
- the BeanWrapper to initializepublic void registerAlias(String beanName, String alias) throws BeanDefinitionStoreException
ConfigurableBeanFactory
Typically invoked during factory configuration, but can also be used for runtime registration of aliases. Therefore, a factory implementation should synchronize alias access.
registerAlias
in interface ConfigurableBeanFactory
beanName
- name of the beanalias
- alias that will behave the same as the bean name
BeanDefinitionStoreException
public void registerSingleton(String beanName, Object singletonObject) throws BeanDefinitionStoreException
ConfigurableBeanFactory
Typically invoked during factory configuration, but can also be used for runtime registration of singletons. Therefore, a factory implementation should synchronize singleton access; it will have to do this anyway if it supports lazy initialization of singletons.
registerSingleton
in interface ConfigurableBeanFactory
beanName
- name of the beansingletonObject
- the existing object
BeanDefinitionStoreException
protected void addSingleton(String beanName, Object singletonObject)
To be called for eager registration of singletons, e.g. to be able to resolve circular references.
beanName
- the name of the beansingletonObject
- the singleton objectprotected void removeSingleton(String beanName)
To be able to clean up eager registration of a singleton if creation failed.
beanName
- the name of the beanpublic String[] getSingletonNames()
Does not consider any hierarchy this factory may participate in.
protected boolean isFactoryBean(String name) throws NoSuchBeanDefinitionException
name
- the name of the bean to check
NoSuchBeanDefinitionException
- if there is no bean with the given nameprotected void registerDependentBean(String beanName, String dependentBeanName)
beanName
- the name of the beandependentBeanName
- the name of the dependent beanprotected void registerDisposableBean(String beanName, DisposableBean bean)
Note: This does not have to be called for beans that reside in the singleton cache! It just allows to register further beans for destruction on shutdown.
beanName
- the name of the beanbean
- the bean instanceprotected void registerDisposableBean(String beanName, Object bean, String destroyMethodName)
Note: This does not have to be called for beans that reside in the singleton cache! It just allows to register further beans for destruction on shutdown.
beanName
- the name of the beanbean
- the bean instancedestroyMethodName
- the name of the destroy methodpublic void destroySingletons()
ConfigurableBeanFactory
destroySingletons
in interface ConfigurableBeanFactory
protected boolean isFactoryDereference(String name)
BeanFactory.FACTORY_BEAN_PREFIX
,
BeanFactoryUtils.isFactoryDereference(java.lang.String)
protected String transformedBeanName(String name)
protected RootBeanDefinition getMergedBeanDefinition(String beanName, boolean includingAncestors) throws BeansException
beanName
- the name of the bean definitionincludingAncestors
- whether to ask the parent bean factory if not found
in this instance
BeansException
protected RootBeanDefinition getMergedBeanDefinition(String beanName, BeanDefinition bd) throws BeansException
beanName
- the name of the bean definitionbd
- the original bean definition (Root/ChildBeanDefinition)
BeansException
protected void checkMergedBeanDefinition(RootBeanDefinition mergedBeanDefinition, String beanName, Class requiredType, Object[] args) throws BeansException
mergedBeanDefinition
- the bean definition to checkbeanName
- the name of the beanrequiredType
- the required type of the beanargs
- the arguments for bean creation, if any
BeansException
- in case of validation failureprotected Object getObjectForSharedInstance(String name, Object beanInstance) throws BeansException
name
- name that may include factory dereference prefixbeanInstance
- the shared bean instance
BeansException
protected void destroyBean(String beanName, Object bean)
beanName
- name of the beanbean
- the bean instance to destroyprotected void invokeDestroyMethods(String beanName, Object bean)
Called by destroyBean.
beanName
- the bean has in the factory. Used for debug output.bean
- new bean instance we may need to notify of destructioninvokeCustomDestroyMethod(java.lang.String, java.lang.Object, java.lang.String)
,
destroyBean(java.lang.String, java.lang.Object)
protected void invokeCustomDestroyMethod(String beanName, Object bean, String destroyMethodName)
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.
beanName
- the bean has in the factory. Used for debug output.bean
- new bean instance we may need to notify of destructiondestroyMethodName
- the name of the custom destroy methodinvokeDestroyMethods(java.lang.String, java.lang.Object)
protected abstract boolean containsBeanDefinition(String beanName)
beanName
- the name of the bean to look for
containsBean(java.lang.String)
protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException
beanName
- name of the bean to find a definition for
NoSuchBeanDefinitionException
- if the bean definition cannot be resolved
BeansException
- in case of errorsRootBeanDefinition
,
ChildBeanDefinition
protected abstract Object createBean(String beanName, RootBeanDefinition mergedBeanDefinition, Object[] args) throws BeansException
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.
beanName
- 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.
BeansException
- in case of errors
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |