|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.springframework.beans.factory.support.AbstractBeanFactory
Abstract superclass for BeanFactory implementations. Implements the ConfigurableBeanFactory SPI interface.
This class provides singleton/prototype determination, singleton cache, aliases, FactoryBean handling, and bean definition merging for child bean definitions. It also allows for management of a bean factory hierarchy, implementing the HierarchicalBeanFactory interface.
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.
getBeanDefinition(java.lang.String)
,
createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[])
,
destroyBean(java.lang.String, java.lang.Object)
Field Summary | |
static java.lang.String |
FACTORY_BEAN_PREFIX
Used to dereference a FactoryBean and distinguish it from beans created by the factory. |
protected org.apache.commons.logging.Log |
logger
Logger available to subclasses |
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(java.lang.String beanName,
java.lang.Object singletonObject)
Add the given singleton object to the singleton cache of this factory. |
boolean |
containsBean(java.lang.String name)
Does this bean factory contain a bean definition with the given name? |
abstract boolean |
containsBeanDefinition(java.lang.String beanName)
Check if this bean factory contains a bean definition with the given name. |
protected abstract java.lang.Object |
createBean(java.lang.String beanName,
RootBeanDefinition mergedBeanDefinition,
java.lang.Object[] args)
Create a bean instance for the given bean definition. |
protected abstract void |
destroyBean(java.lang.String beanName,
java.lang.Object bean)
Destroy the given bean. |
protected void |
destroySingleton(java.lang.String beanName)
Destroy the given bean. |
void |
destroySingletons()
Destroy all cached singletons in this factory. |
java.lang.String[] |
getAliases(java.lang.String name)
Return the aliases for the given bean name, if defined. |
java.lang.Object |
getBean(java.lang.String name)
Return the bean with the given name, checking the parent bean factory if not found. |
java.lang.Object |
getBean(java.lang.String name,
java.lang.Class requiredType)
Return an instance (possibly shared or independent) of the given bean name. |
java.lang.Object |
getBean(java.lang.String name,
java.lang.Object[] args)
Return the bean with the given name, checking the parent bean factory if not found. |
protected abstract BeanDefinition |
getBeanDefinition(java.lang.String beanName)
Return the bean definition for the given bean name. |
java.util.List |
getBeanPostProcessors()
Return the list of BeanPostProcessors that will get applied to beans created with this factory. |
java.util.Map |
getCustomEditors()
Return the map of custom editors, with Classes as keys and PropertyEditors as values. |
java.util.Set |
getIgnoredDependencyTypes()
Return the set of classes that will get ignored for autowiring. |
protected RootBeanDefinition |
getMergedBeanDefinition(java.lang.String beanName,
BeanDefinition bd)
Return a RootBeanDefinition, even by traversing parent if the parameter is a child definition. |
RootBeanDefinition |
getMergedBeanDefinition(java.lang.String beanName,
boolean includingAncestors)
Return a RootBeanDefinition, even by traversing parent if the parameter is a child definition. |
protected java.lang.Object |
getObjectForSharedInstance(java.lang.String name,
java.lang.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. |
java.lang.String[] |
getSingletonNames(java.lang.Class type)
Return the names of beans in the singleton cache that match the given object type (including subclasses). |
void |
ignoreDependencyType(java.lang.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 boolean |
isFactoryDereference(java.lang.String name)
Return whether this name is a factory dereference (beginning with the factory dereference prefix). |
boolean |
isSingleton(java.lang.String name)
Is this bean a singleton? |
void |
registerAlias(java.lang.String beanName,
java.lang.String alias)
Given a bean name, create an alias. |
void |
registerCustomEditor(java.lang.Class requiredType,
java.beans.PropertyEditor propertyEditor)
Register the given custom property editor for all properties of the given type. |
void |
registerSingleton(java.lang.String beanName,
java.lang.Object singletonObject)
Register the given existing object as singleton in the bean factory, under the given bean name. |
protected void |
removeSingleton(java.lang.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 java.lang.String |
transformedBeanName(java.lang.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 |
public static final java.lang.String FACTORY_BEAN_PREFIX
myEjb
is a factory, getting &myEjb
will
return the factory, not the instance returned by the factory.
protected final org.apache.commons.logging.Log logger
Constructor Detail |
public AbstractBeanFactory()
public AbstractBeanFactory(BeanFactory parentBeanFactory)
parentBeanFactory
- parent bean factory, or null if nonegetBean(java.lang.String)
Method Detail |
public java.lang.Object getBean(java.lang.String name) throws BeansException
getBean
in interface BeanFactory
name
- name of the bean to retrieve
BeansException
- if the bean could not be obtainedpublic java.lang.Object getBean(java.lang.String name, java.lang.Object[] args) throws BeansException
name
- 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.
TODO: We could consider supporting this for constructor args also, but it's really a
corner case required for AspectJ integration.
BeansException
public java.lang.Object getBean(java.lang.String name, java.lang.Class requiredType) throws BeansException
BeanFactory
getBean
in interface BeanFactory
name
- name of the bean to returnrequiredType
- type the bean must match. Can be an interface or superclass
of the actual class. 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 boolean containsBean(java.lang.String name)
BeanFactory
Will ask the parent factory if the bean cannot be found in this factory instance.
containsBean
in interface BeanFactory
name
- name of the bean to query
public boolean isSingleton(java.lang.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
- name of the bean to query
NoSuchBeanDefinitionException
- if there is no bean with the given namepublic java.lang.String[] getAliases(java.lang.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(java.lang.Class requiredType, java.beans.PropertyEditor propertyEditor)
ConfigurableBeanFactory
registerCustomEditor
in interface ConfigurableBeanFactory
requiredType
- type of the propertypropertyEditor
- editor to registerpublic java.util.Map getCustomEditors()
public void ignoreDependencyType(java.lang.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 java.util.Set getIgnoredDependencyTypes()
public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor)
ConfigurableBeanFactory
addBeanPostProcessor
in interface ConfigurableBeanFactory
beanPostProcessor
- the bean processor to registerpublic java.util.List getBeanPostProcessors()
public void registerAlias(java.lang.String beanName, java.lang.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(java.lang.String beanName, java.lang.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(java.lang.String beanName, java.lang.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(java.lang.String beanName)
To be able to clean up eager registration of a singleton if creation failed.
beanName
- the name of the beanpublic void destroySingletons()
ConfigurableBeanFactory
destroySingletons
in interface ConfigurableBeanFactory
protected final void destroySingleton(java.lang.String beanName)
beanName
- name of the beandestroyBean(java.lang.String, java.lang.Object)
protected java.lang.String transformedBeanName(java.lang.String name) throws NoSuchBeanDefinitionException
NoSuchBeanDefinitionException
protected boolean isFactoryDereference(java.lang.String name)
protected void initBeanWrapper(BeanWrapper bw)
bw
- the BeanWrapper to initializepublic java.lang.String[] getSingletonNames(java.lang.Class type)
Does not consider any hierarchy this factory may participate in.
type
- class or interface to match, or null for all bean names
protected java.lang.Object getObjectForSharedInstance(java.lang.String name, java.lang.Object beanInstance)
name
- name that may include factory dereference prefixbeanInstance
- the shared bean instance
public RootBeanDefinition getMergedBeanDefinition(java.lang.String beanName, boolean includingAncestors) throws BeansException
BeansException
protected RootBeanDefinition getMergedBeanDefinition(java.lang.String beanName, BeanDefinition bd)
public abstract boolean containsBeanDefinition(java.lang.String beanName)
beanName
- the name of the bean to look for
containsBean(java.lang.String)
protected abstract BeanDefinition getBeanDefinition(java.lang.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 java.lang.Object createBean(java.lang.String beanName, RootBeanDefinition mergedBeanDefinition, java.lang.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 errorsprotected abstract void destroyBean(java.lang.String beanName, java.lang.Object bean)
beanName
- name of the beanbean
- the bean instance to destroy
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |