|
||||||||||
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
public abstract class AbstractBeanFactory
Abstract base class for BeanFactory
implementations, providing the full capabilities of the
ConfigurableBeanFactory
SPI.
Does not assume a listable bean factory: can therefore also be used
as base class for bean factory implementations which obtain bean definitions
from some backend resource (where bean definition access is an expensive operation).
This class provides singleton/prototype determination, singleton cache,
FactoryBean
handling, aliases,
bean definition merging for child bean definitions, and bean destruction
(DisposableBean
interface, custom
destroy methods). Furthermore, it can manage a bean factory hierarchy
(delegating to the parent in case of an unknown bean), through implementing
the HierarchicalBeanFactory
interface.
The main template methods to be implemented by subclasses are
getBeanDefinition(java.lang.String)
and createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[])
, retrieving a bean definition
for a given bean name and creating a bean instance for a given bean definition,
respectively. Default implementations of those operations can be found in
DefaultListableBeanFactory
and AbstractAutowireCapableBeanFactory
.
getBeanDefinition(java.lang.String)
,
createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[])
,
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 BeanPostProcessor 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 with the given name? |
protected abstract boolean |
containsBeanDefinition(String beanName)
Check if this bean factory contains a bean definition with the given name. |
boolean |
containsLocalBean(String name)
Return whether the local bean factory contains a bean of the given name, ignoring beans defined in ancestor contexts. |
boolean |
containsSingleton(String beanName)
Check if this bean factory contains a singleton instance 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. |
protected Object |
doTypeConversionIfNecessary(Object value,
Class targetType)
Convert the given value into the specified target type, using a default BeanWrapper instance. |
protected Object |
doTypeConversionIfNecessary(Object value,
Class targetType,
BeanWrapperImpl bw)
Convert the given value into the specified target type, using the specified BeanWrapper. |
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 specified bean. |
Object |
getBean(String name,
Class requiredType)
Return an instance, which may be shared or independent, of the specified bean. |
Object |
getBean(String name,
Class requiredType,
Object[] args)
Return an instance, which may be shared or independent, of the specified bean. |
Object |
getBean(String name,
Object[] args)
Return an instance, which may be shared or independent, of the specified bean. |
protected abstract BeanDefinition |
getBeanDefinition(String beanName)
Return the bean definition for the given bean name. |
int |
getBeanPostProcessorCount()
Return the current number of registered BeanPostProcessors. |
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. |
RootBeanDefinition |
getMergedBeanDefinition(String name)
Return a RootBeanDefinition for the given bean name, merging a child bean definition with its parent if necessary. |
protected RootBeanDefinition |
getMergedBeanDefinition(String beanName,
BeanDefinition bd)
Return a RootBeanDefinition for the given top-level bean, by merging with the parent if the given bean's definition is a child bean definition. |
protected RootBeanDefinition |
getMergedBeanDefinition(String name,
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. |
int |
getSingletonCount()
Return the number of beans in the singleton cache. |
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. |
protected Class |
getTypeForFactoryMethod(String beanName,
RootBeanDefinition mergedBeanDefinition)
Determine the bean type for the given bean definition, as far as possible. |
protected boolean |
hasDestructionAwareBeanPostProcessors()
Return whether this factory holds a DestructionAwareBeanPostProcessor that will get applied to singleton beans on shutdown. |
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,
boolean enforceDestroyMethod)
Invoke the specified custom destroy method on the given bean. |
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 shared singleton? |
protected boolean |
isSingletonCurrentlyInCreation(String beanName)
Return whether the specified singleton is currently in creation |
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 |
registerDisposableBeanIfNecessary(String beanName,
Object bean,
RootBeanDefinition mergedBeanDefinition)
Add the given bean to the list of disposable beans in this factory, registering its DisposableBean interface and/or the given destroy method to be called on factory shutdown (if applicable). |
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
This method allows a Spring BeanFactory to be used as a replacement for the Singleton or Prototype design pattern. Callers may retain references to returned objects in the case of Singleton beans.
Translates aliases back to the corresponding canonical bean name. Will ask the parent factory if the bean cannot be found in this factory instance.
getBean
in interface BeanFactory
name
- the name of the bean to retrieve
NoSuchBeanDefinitionException
- if there is no bean definition
with the specified name
BeansException
- if the bean could not be obtainedpublic Object getBean(String name, Class requiredType) throws BeansException
BeanFactory
Behaves the same as BeanFactory.getBean(String)
, but provides a measure of type
safety by throwing a BeanNotOfRequiredTypeException 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 BeanFactory.getBean(String)
.
Translates aliases back to the corresponding canonical bean name. Will ask the parent factory if the bean cannot be found in this factory instance.
getBean
in interface BeanFactory
name
- the name of the bean to retrieverequiredType
- 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.
BeanNotOfRequiredTypeException
- if the bean is not of the required type
NoSuchBeanDefinitionException
- if there's no such bean definition
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
- if the bean could not be createdpublic 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.
BeansException
- if the bean could not be createdpublic 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
BeanFactory.getBean(java.lang.String)
always
return the same instance?
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 nameBeanFactory.getBean(java.lang.String)
public Class getType(String name) throws NoSuchBeanDefinitionException
BeanFactory
BeanFactory.getBean(java.lang.String)
would return.
For a FactoryBean, return the type of object that the FactoryBean creates.
getType
in interface BeanFactory
name
- the name of the bean to query
null
if not determinable
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 is no bean definition
with the specified namepublic BeanFactory getParentBeanFactory()
HierarchicalBeanFactory
null
if there is none.
getParentBeanFactory
in interface HierarchicalBeanFactory
public boolean containsLocalBean(String name)
HierarchicalBeanFactory
This is an alternative to containsBean
, ignoring a bean
of the given name from an ancestor bean factory.
containsLocalBean
in interface HierarchicalBeanFactory
name
- the name of the bean to query
BeanFactory.containsBean(java.lang.String)
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 addBeanPostProcessor(BeanPostProcessor beanPostProcessor)
ConfigurableBeanFactory
addBeanPostProcessor
in interface ConfigurableBeanFactory
beanPostProcessor
- the bean processor to registerpublic int getBeanPostProcessorCount()
ConfigurableBeanFactory
getBeanPostProcessorCount
in interface ConfigurableBeanFactory
public List getBeanPostProcessors()
protected boolean hasDestructionAwareBeanPostProcessors()
public 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
The given instance is supposed to be fully initialized; the factory
will not perform any initialization callbacks (in particular, it won't
call InitializingBean's afterPropertiesSet
method).
The given instance will not receive any destruction callbacks
(like DisposableBean's destroy
method) either.
Register a bean definition instead of an existing instance if your bean is supposed to receive initialization and/or destruction callbacks.
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
InitializingBean.afterPropertiesSet()
,
DisposableBean.destroy()
,
DefaultListableBeanFactory.registerBeanDefinition(java.lang.String, org.springframework.beans.factory.config.BeanDefinition)
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 int getSingletonCount()
Does not consider any hierarchy this factory may participate in.
public String[] getSingletonNames()
Does not consider any hierarchy this factory may participate in.
protected boolean isSingletonCurrentlyInCreation(String beanName)
beanName
- the name of the beanpublic boolean containsSingleton(String beanName)
ConfigurableBeanFactory
The main purpose of this method is to check manually registered singletons
(see registerSingleton
). Can also be used to check whether a
singleton defined by a bean definition has already been created.
To check whether a bean factory contains a bean definition with a given name,
use ListableBeanFactory's containsBeanDefinition
. Analogous to the
present method, this checks whether the factory contains a registered bean definition.
Note that both of those methods check for the actual given bean name, ignoring aliases.
Use BeanFactory's containsBean
for general checks whether the
factory knows about a bean with a given name (whether manually registed singleton
instance or created by bean definition), also checking ancestor factories. To check
the local factory only, use HierarchicalBeanFactory's containsLocalBean
.
Both of those methods translate aliases back to the corresponding canonical bean name.
containsSingleton
in interface ConfigurableBeanFactory
beanName
- the name of the bean to look for
ConfigurableBeanFactory.registerSingleton(java.lang.String, java.lang.Object)
,
BeanFactory.containsBean(java.lang.String)
,
HierarchicalBeanFactory.containsLocalBean(java.lang.String)
,
ListableBeanFactory.containsBeanDefinition(java.lang.String)
public void destroySingletons()
ConfigurableBeanFactory
destroySingletons
in interface ConfigurableBeanFactory
protected boolean isFactoryDereference(String name)
protected String transformedBeanName(String name)
protected void initBeanWrapper(BeanWrapper bw)
bw
- the BeanWrapper to initializeprotected Object doTypeConversionIfNecessary(Object value, Class targetType) throws TypeMismatchException
value
- the original valuetargetType
- the target type
TypeMismatchException
- if type conversion faileddoTypeConversionIfNecessary(Object, Class, org.springframework.beans.BeanWrapperImpl)
protected Object doTypeConversionIfNecessary(Object value, Class targetType, BeanWrapperImpl bw) throws TypeMismatchException
value
- the original valuetargetType
- the target typebw
- the BeanWrapper to work on
TypeMismatchException
- if type conversion failedBeanWrapperImpl.doTypeConversionIfNecessary(Object, Class)
public RootBeanDefinition getMergedBeanDefinition(String name) throws BeansException
name
- the name of the bean to retrieve the merged definition for
NoSuchBeanDefinitionException
- if there is no bean with the given name
BeansException
- in case of errorsprotected RootBeanDefinition getMergedBeanDefinition(String name, boolean includingAncestors) throws BeansException
name
- the name of the bean to retrieve the merged definition forincludingAncestors
- whether to ask the parent bean factory if not found
in this instance
NoSuchBeanDefinitionException
- if there is no bean with the given name
BeanDefinitionStoreException
- in case of an invalid bean definition
BeansException
protected RootBeanDefinition getMergedBeanDefinition(String beanName, BeanDefinition bd) throws BeanDefinitionStoreException
beanName
- the name of the bean definitionbd
- the original bean definition (Root/ChildBeanDefinition)
BeanDefinitionStoreException
- in case of an invalid bean definitionprotected 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
public 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 registerDisposableBeanIfNecessary(String beanName, Object bean, RootBeanDefinition mergedBeanDefinition)
Also registers bean as dependent on other beans, according to the "depends-on" configuration in the bean definition.
beanName
- the name of the beanbean
- the bean instancemergedBeanDefinition
- the bean definition for the beanAbstractBeanDefinition.isSingleton()
,
AbstractBeanDefinition.getDependsOn()
,
registerDisposableBean(java.lang.String, org.springframework.beans.factory.DisposableBean)
,
registerDependentBean(java.lang.String, java.lang.String)
protected void registerDisposableBean(String beanName, DisposableBean bean)
beanName
- the name of the beanbean
- the bean instanceprotected void registerDependentBean(String beanName, String dependentBeanName)
beanName
- the name of the beandependentBeanName
- the name of the dependent beanprotected void destroyBean(String beanName, Object bean)
beanName
- name of the beanbean
- the bean instance to destroyprotected void invokeCustomDestroyMethod(String beanName, Object bean, String destroyMethodName, boolean enforceDestroyMethod)
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 methodenforceDestroyMethod
- indicates whether the defined destroy method needs to existprotected abstract boolean containsBeanDefinition(String beanName)
containsBean
when no cached singleton instance is found.
Depending on the nature of the concrete bean factory implementation, this operation might be expensive (for example, because of directory lookups in external registries). However, for listable bean factories, this usually just amounts to a local hash lookup: The operation is therefore part of the public interface there. The same implementation can serve for both this template method and the public interface method in that case.
beanName
- the name of the bean to look for
containsBean(java.lang.String)
,
ListableBeanFactory.containsBeanDefinition(java.lang.String)
protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException
Depending on the nature of the concrete bean factory implementation, this operation might be expensive (for example, because of directory lookups in external registries). However, for listable bean factories, this usually just amounts to a local hash lookup: The operation is therefore part of the public interface there. The same implementation can serve for both this template method and the public interface method in that case.
beanName
- the name of the bean to find a definition for
null
)
NoSuchBeanDefinitionException
- if the bean definition cannot be resolved
BeansException
- in case of errorsRootBeanDefinition
,
ChildBeanDefinition
,
ConfigurableListableBeanFactory.getBeanDefinition(java.lang.String)
protected abstract Object createBean(String beanName, RootBeanDefinition mergedBeanDefinition, Object[] args) throws BeanCreationException
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
- 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 createdprotected Class getTypeForFactoryMethod(String beanName, RootBeanDefinition mergedBeanDefinition)
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
implementation.
beanName
- name of the beanmergedBeanDefinition
- the bean definition for the bean
null
elsecreateBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[])
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |