站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Spring Framework 2.1.0 API 文档英文版

AbstractBeanFactory (Spring Framework API 2.1) - Spring Framework 2.1.0 API 文档英文版


The Spring Framework

org.springframework.beans.factory.support
Class AbstractBeanFactory

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

public abstract class AbstractBeanFactory
extends DefaultSingletonBeanRegistry
implements ConfigurableBeanFactory

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 a singleton cache (through its base class DefaultSingletonBeanRegistry, singleton/prototype determination, 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.

Since:
15 April 2001
Author:
Rod Johnson, Juergen Hoeller
See Also:
getBeanDefinition(java.lang.String), createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[]), AbstractAutowireCapableBeanFactory.createBean(java.lang.Class, int, boolean), DefaultListableBeanFactory.getBeanDefinition(java.lang.String)

Field Summary
 
Fields inherited from class org.springframework.beans.factory.support.DefaultSingletonBeanRegistry
logger
 
Fields inherited from interface org.springframework.beans.factory.config.ConfigurableBeanFactory
SCOPE_PROTOTYPE, SCOPE_SINGLETON
 
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.
 void addPropertyEditorRegistrar(PropertyEditorRegistrar registrar)
          Add a PropertyEditorRegistrar to be applied to all bean creation processes.
protected  void afterPrototypeCreation(String beanName)
          Callback after prototype creation.
protected  void beforePrototypeCreation(String beanName)
          Callback before prototype creation.
protected  void checkMergedBeanDefinition(RootBeanDefinition mbd, String beanName, 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.
 void copyConfigurationFrom(ConfigurableBeanFactory otherFactory)
          Copy all relevant configuration from the given other factory.
protected abstract  Object createBean(String beanName, RootBeanDefinition mbd, Object[] args)
          Create a bean instance for the given bean definition.
 void destroyBean(String beanName, Object beanInstance)
          Destroy the given bean instance (usually a prototype instance obtained from this factory) according to its bean definition.
protected  void destroyBean(String beanName, Object beanInstance, RootBeanDefinition mbd)
          Destroy the given bean instance (usually a prototype instance obtained from this factory) according to the given bean definition.
 void destroyScopedBean(String beanName)
          Destroy the specified scoped bean in the current target scope, if any.
 String[] getAliases(String name)
          Return the aliases for the given bean name, if any.
 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.
 ClassLoader getBeanClassLoader()
          Return this factory's class loader for loading bean classes.
protected abstract  BeanDefinition getBeanDefinition(String beanName)
          Return the bean definition for the given bean name.
 int getBeanPostProcessorCount()
          Return the current number of registered BeanPostProcessors, if any.
 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 beanName)
          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 beanName, BeanDefinition bd, BeanDefinition containingBd)
          Return a RootBeanDefinition for the given bean, by merging with the parent if the given bean's 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 getObjectForBeanInstance(Object beanInstance, String name, RootBeanDefinition mbd)
          Get the object for the given bean instance, 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.
 Set getPropertyEditorRegistrars()
          Return the set of PropertyEditorRegistrars.
 Scope getRegisteredScope(String scopeName)
          Return the Scope implementation for the given scope name, if any.
 String[] getRegisteredScopeNames()
          Return the names of all currently registered scopes.
 Class getType(String name)
          Determine the type of the bean with the given name.
 TypeConverter getTypeConverter()
          Obtain a type converter as used by this BeanFactory.
protected  Class getTypeForFactoryBean(FactoryBean factoryBean)
          Determine the type for the given FactoryBean.
protected  Class getTypeForFactoryBean(String beanName, RootBeanDefinition mbd)
          Determine the bean type for the given FactoryBean 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  boolean hasInstantiationAwareBeanPostProcessors()
          Return whether this factory holds a InstantiationAwareBeanPostProcessor 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  boolean isAlias(String beanName)
          Determine whether this given bean name is defines as an alias (as opposed to the name of an actual bean definition).
protected  boolean isBeanClassMatch(String beanName, RootBeanDefinition mbd, Class targetType)
          Check whether the bean class of the given bean definition matches the specified target type.
protected  boolean isBeanNameInUse(String beanName)
          Determine whether the given bean name is already in use within this factory, that is, whether there is a local bean registered under this name or an inner bean created with this name.
 boolean isCacheBeanMetadata()
          Return whether to cache bean metadata such as given bean definitions (in merged fashion) and resolved bean classes.
 boolean isCurrentlyInCreation(String beanName)
          Return whether the specified bean is currently in creation.
 boolean isFactoryBean(String name)
          Determine whether the bean with the given name is a FactoryBean.
 boolean isPrototype(String name)
          Is this bean a prototype?
protected  boolean isPrototypeCurrentlyInCreation(String beanName)
          Return whether the specified prototype bean is currently in creation (within the current thread).
 boolean isSingleton(String name)
          Is this bean a shared singleton?
 boolean isTypeMatch(String name, Class targetType)
          Check whether the bean with the given name matches the specified type.
protected  String originalBeanName(String name)
          Determine the original bean name, resolving locally defined aliases to canonical names.
protected  Object postProcessObjectFromFactoryBean(Object object, String beanName)
          Post-process the given object that has been obtained from the FactoryBean.
protected  Class predictBeanType(String beanName, RootBeanDefinition mbd)
          Predict the eventual bean type (of the processed bean instance) for the specified bean.
 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 registerCustomEditors(PropertyEditorRegistry registry)
          Initialize the given PropertyEditorRegistry with the custom editors registered with this BeanFactory.
protected  void registerDisposableBeanIfNecessary(String beanName, Object bean, RootBeanDefinition mbd)
          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 registerScope(String scopeName, Scope scope)
          Register the given scope, backed by the given Scope implementation.
protected  void removeSingleton(String beanName)
          Overridden to clear the FactoryBean object cache as well.
protected  boolean requiresDestruction(Object bean, RootBeanDefinition mbd)
          Determine whether the given bean requires destruction on shutdown.
protected  Class resolveBeanClass(RootBeanDefinition mbd, String beanName)
          Resolve the bean class for the specified bean definition, resolving a bean class name into a Class reference (if necessary) and storing the resolved Class in the bean definition for further use.
 void setBeanClassLoader(ClassLoader beanClassLoader)
          Set the class loader to use for loading bean classes.
 void setCacheBeanMetadata(boolean cacheBeanMetadata)
          Set whether to cache bean metadata such as given bean definitions (in merged fashion) and resolved bean classes.
 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 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.config.ConfigurableBeanFactory
destroySingletons
 
Methods inherited from interface org.springframework.beans.factory.config.SingletonBeanRegistry
containsSingleton, getSingleton, getSingletonCount, getSingletonNames, registerSingleton
 

Constructor Detail

AbstractBeanFactory

public AbstractBeanFactory()
Create a new AbstractBeanFactory.


AbstractBeanFactory

public AbstractBeanFactory(BeanFactory parentBeanFactory)
Create a new AbstractBeanFactory with the given parent.

Parameters:
parentBeanFactory - parent bean factory, or null if none
See Also:
getBean(java.lang.String)
Method Detail

getBean

public Object getBean(String name)
               throws BeansException
Description copied from interface: BeanFactory
Return an instance, which may be shared or independent, of the specified bean.

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.

Specified by:
getBean in interface BeanFactory
Parameters:
name - the name of the bean to retrieve
Returns:
an instance of the bean
Throws:
NoSuchBeanDefinitionException - if there is no bean definition with the specified name
BeansException - if the bean could not be obtained

getBean

public Object getBean(String name,
                      Class requiredType)
               throws BeansException
Description copied from interface: BeanFactory
Return an instance, which may be shared or independent, of the specified bean.

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.

Specified by:
getBean in interface BeanFactory
Parameters:
name - the name of the bean to retrieve
requiredType - 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.
Returns:
an instance of the bean
Throws:
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 created

getBean

public Object getBean(String name,
                      Object[] args)
               throws BeansException
Return an instance, which may be shared or independent, of the specified bean.

Parameters:
name - the name of the bean to retrieve
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:
an instance of the bean
Throws:
BeansException - if the bean could not be created

getBean

public Object getBean(String name,
                      Class requiredType,
                      Object[] args)
               throws BeansException
Return an instance, which may be shared or independent, of the specified bean.

Parameters:
name - the name of the bean to retrieve
requiredType - the required type of the bean to retrieve
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:
an instance of the bean
Throws:
BeansException - if the bean could not be created

containsBean

public boolean containsBean(String name)
Description copied from interface: BeanFactory
Does this bean factory contain a bean with the given name? More specifically, is BeanFactory.getBean(java.lang.String) able to obtain a bean instance for the given name?

Translates aliases back to the corresponding canonical bean name. Will ask the parent factory if the bean cannot be found in this factory instance.

Specified by:
containsBean in interface BeanFactory
Parameters:
name - the name of the bean to query
Returns:
whether a bean with the given name is defined

isSingleton

public boolean isSingleton(String name)
                    throws NoSuchBeanDefinitionException
Description copied from interface: BeanFactory
Is this bean a shared singleton? That is, will BeanFactory.getBean(java.lang.String) always return the same instance?

Note: This method returning false does not clearly indicate independent instances. It indicates non-singleton instances, which may correspond to a scoped bean as well. Use the BeanFactory.isPrototype(java.lang.String) operation to explicitly check for independent instances.

Translates aliases back to the corresponding canonical bean name. Will ask the parent factory if the bean cannot be found in this factory instance.

Specified by:
isSingleton in interface BeanFactory
Parameters:
name - the name of the bean to query
Returns:
whether this bean corresponds to a singleton instance
Throws:
NoSuchBeanDefinitionException - if there is no bean with the given name
See Also:
BeanFactory.getBean(java.lang.String), BeanFactory.isPrototype(java.lang.String)

isPrototype

public boolean isPrototype(String name)
                    throws NoSuchBeanDefinitionException
Description copied from interface: BeanFactory
Is this bean a prototype? That is, will BeanFactory.getBean(java.lang.String) always return independent instances?

Note: This method returning false does not clearly indicate a singleton object. It indicates non-independent instances, which may correspond to a scoped bean as well. Use the BeanFactory.isSingleton(java.lang.String) operation to explicitly check for a shared singleton instance.

Translates aliases back to the corresponding canonical bean name. Will ask the parent factory if the bean cannot be found in this factory instance.

Specified by:
isPrototype in interface BeanFactory
Parameters:
name - the name of the bean to query
Returns:
whether this bean will always deliver independent instances
Throws:
NoSuchBeanDefinitionException - if there is no bean with the given name
See Also:
BeanFactory.getBean(java.lang.String), BeanFactory.isSingleton(java.lang.String)

isTypeMatch

public boolean isTypeMatch(String name,
                           Class targetType)
                    throws NoSuchBeanDefinitionException
Description copied from interface: BeanFactory
Check whether the bean with the given name matches the specified type. More specifically, check whether a BeanFactory.getBean(java.lang.String) call for the given name would return an object that is assignable to the specified target type.

Translates aliases back to the corresponding canonical bean name. Will ask the parent factory if the bean cannot be found in this factory instance.

Specified by:
isTypeMatch in interface BeanFactory
Parameters:
name - the name of the bean to query
targetType - the type to match against
Returns:
true if the bean type matches, false if it doesn't match or cannot be determined yet
Throws:
NoSuchBeanDefinitionException - if there is no bean with the given name
See Also:
BeanFactory.getBean(java.lang.String), BeanFactory.getType(java.lang.String)

getType

public Class getType(String name)
              throws NoSuchBeanDefinitionException
Description copied from interface: BeanFactory
Determine the type of the bean with the given name. More specifically, determine the type of object that BeanFactory.getBean(java.lang.String) would return for the given name.

For a FactoryBean, return the type of object that the FactoryBean creates, as exposed by FactoryBean.getObjectType().

Translates aliases back to the corresponding canonical bean name. Will ask the parent factory if the bean cannot be found in this factory instance.

Specified by:
getType in interface BeanFactory
Parameters:
name - the name of the bean to query
Returns:
the type of the bean, or null if not determinable
Throws:
NoSuchBeanDefinitionException - if there is no bean with the given name
See Also:
BeanFactory.getBean(java.lang.String), BeanFactory.isTypeMatch(java.lang.String, java.lang.Class)

getAliases

public String[] getAliases(String name)
Description copied from interface: BeanFactory
Return the aliases for the given bean name, if any. All of those aliases point to the same bean when used in a BeanFactory.getBean(java.lang.String) call.

If the given name is an alias, the corresponding original bean name and other aliases (if any) will be returned, with the original bean name being the first element in the array.

Will ask the parent factory if the bean cannot be found in this factory instance.

Specified by:
getAliases in interface BeanFactory
Parameters:
name - the bean name to check for aliases
Returns:
the aliases, or an empty array if none
See Also:
BeanFactory.getBean(java.lang.String)

getParentBeanFactory

public BeanFactory getParentBeanFactory()
Description copied from interface: HierarchicalBeanFactory
Return the parent bean factory, or null if there is none.

Specified by:
getParentBeanFactory in interface HierarchicalBeanFactory

containsLocalBean

public boolean containsLocalBean(String name)
Description copied from interface: HierarchicalBeanFactory
Return whether the local bean factory contains a bean of the given name, ignoring beans defined in ancestor contexts.

This is an alternative to containsBean, ignoring a bean of the given name from an ancestor bean factory.

Specified by:
containsLocalBean in interface HierarchicalBeanFactory
Parameters:
name - the name of the bean to query
Returns:
whether a bean with the given name is defined in the local factory
See Also:
BeanFactory.containsBean(java.lang.String)

setParentBeanFactory

public void setParentBeanFactory(BeanFactory parentBeanFactory)
Description copied from interface: ConfigurableBeanFactory
Set the parent of this bean factory.

Note that the parent cannot be changed: It should only be set outside a constructor if it isn't available at the time of factory instantiation.

Specified by:
setParentBeanFactory in interface ConfigurableBeanFactory
Parameters:
parentBeanFactory - the parent BeanFactory
See Also:
HierarchicalBeanFactory.getParentBeanFactory()

setBeanClassLoader

public void setBeanClassLoader(ClassLoader beanClassLoader)
Description copied from interface: ConfigurableBeanFactory
Set the class loader to use for loading bean classes. Default is the thread context class loader.

Note that this class loader will only apply to bean definitions that do not carry a resolved bean class yet. This is the case as of Spring 2.0 by default: Bean definitions only carry bean class names, to be resolved once the factory processes the bean definition.

Specified by:
setBeanClassLoader in interface ConfigurableBeanFactory
Parameters:
beanClassLoader - the class loader to use, or null to suggest the default class loader

getBeanClassLoader

public ClassLoader getBeanClassLoader()
Description copied from interface: ConfigurableBeanFactory
Return this factory's class loader for loading bean classes.

Specified by:
getBeanClassLoader in interface ConfigurableBeanFactory

setCacheBeanMetadata

public void setCacheBeanMetadata(boolean cacheBeanMetadata)
Description copied from interface: ConfigurableBeanFactory
Set whether to cache bean metadata such as given bean definitions (in merged fashion) and resolved bean classes. Default is on.

Turn this flag off to enable hot-refreshing of bean definition objects and in particular bean classes. If this flag is off, any creation of a bean instance will re-query the bean class loader for newly resolved classes.

Specified by:
setCacheBeanMetadata in interface ConfigurableBeanFactory

isCacheBeanMetadata

public boolean isCacheBeanMetadata()
Description copied from interface: ConfigurableBeanFactory
Return whether to cache bean metadata such as given bean definitions (in merged fashion) and resolved bean classes.

Specified by:
isCacheBeanMetadata in interface ConfigurableBeanFactory

addPropertyEditorRegistrar

public void addPropertyEditorRegistrar(PropertyEditorRegistrar registrar)
Description copied from interface: ConfigurableBeanFactory
Add a PropertyEditorRegistrar to be applied to all bean creation processes.

Such a registrar creates new PropertyEditor instances and registers them on the given registry, fresh for each bean creation attempt. This avoids the need for synchronization on custom editors; hence, it is generally preferable to use this method instead of registerCustomEditor.

Specified by:
addPropertyEditorRegistrar in interface ConfigurableBeanFactory
Parameters:
registrar - the PropertyEditorRegistrar to register
See Also:
ConfigurableBeanFactory.registerCustomEditor(java.lang.Class, java.beans.PropertyEditor)

getPropertyEditorRegistrars

public Set getPropertyEditorRegistrars()
Return the set of PropertyEditorRegistrars.


registerCustomEditor

public void registerCustomEditor(Class requiredType,
                                 PropertyEditor propertyEditor)
Description copied from interface: ConfigurableBeanFactory
Register the given custom property editor for all properties of the given type. To be invoked during factory configuration.

Note that this method will register a shared custom editor instance; access to that instance will be synchronized for thread-safety. It is generally prefable to use addPropertyEditorRegistrar instead of this method, to avoid for the need for synchronization on custom editors.

Specified by:
registerCustomEditor in interface ConfigurableBeanFactory
Parameters:
requiredType - type of the property
propertyEditor - editor to register
See Also:
ConfigurableBeanFactory.addPropertyEditorRegistrar(org.springframework.beans.PropertyEditorRegistrar)

getCustomEditors

public Map getCustomEditors()
Return the map of custom editors, with Classes as keys and PropertyEditors as values.


getTypeConverter

public TypeConverter getTypeConverter()
Description copied from interface: ConfigurableBeanFactory
Obtain a type converter as used by this BeanFactory. This is typically a fresh instance for each call, since TypeConverters are usually not thread-safe.

Specified by:
getTypeConverter in interface ConfigurableBeanFactory

addBeanPostProcessor

public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor)
Description copied from interface: ConfigurableBeanFactory
Add a new BeanPostProcessor that will get applied to beans created by this factory. To be invoked during factory configuration.

Specified by:
addBeanPostProcessor in interface ConfigurableBeanFactory
Parameters:
beanPostProcessor - the bean processor to register

getBeanPostProcessorCount

public int getBeanPostProcessorCount()
Description copied from interface: ConfigurableBeanFactory
Return the current number of registered BeanPostProcessors, if any.

Specified by:
getBeanPostProcessorCount in interface ConfigurableBeanFactory

getBeanPostProcessors

public List getBeanPostProcessors()
Return the list of BeanPostProcessors that will get applied to beans created with this factory.


hasInstantiationAwareBeanPostProcessors

protected boolean hasInstantiationAwareBeanPostProcessors()
Return whether this factory holds a InstantiationAwareBeanPostProcessor that will get applied to singleton beans on shutdown.

See Also:
addBeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor), InstantiationAwareBeanPostProcessor

hasDestructionAwareBeanPostProcessors

protected boolean hasDestructionAwareBeanPostProcessors()
Return whether this factory holds a DestructionAwareBeanPostProcessor that will get applied to singleton beans on shutdown.

See Also:
addBeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor), DestructionAwareBeanPostProcessor

registerScope

public void registerScope(String scopeName,
                          Scope scope)
Description copied from interface: ConfigurableBeanFactory
Register the given scope, backed by the given Scope implementation.

Specified by:
registerScope in interface ConfigurableBeanFactory
Parameters:
scopeName - the scope identifier
scope - the backing Scope implementation

getRegisteredScopeNames

public String[] getRegisteredScopeNames()
Description copied from interface: ConfigurableBeanFactory
Return the names of all currently registered scopes.

This will only return the names of explicitly registered scopes. Built-in scopes such as "singleton" and "prototype" won't be exposed.

Specified by:
getRegisteredScopeNames in interface ConfigurableBeanFactory
Returns:
the array of scope names, or an empty array if none
See Also:
ConfigurableBeanFactory.registerScope(java.lang.String, org.springframework.beans.factory.config.Scope)

getRegisteredScope

public Scope getRegisteredScope(String scopeName)
Description copied from interface: ConfigurableBeanFactory
Return the Scope implementation for the given scope name, if any.

This will only return explicitly registered scopes. Built-in scopes such as "singleton" and "prototype" won't be exposed.

Specified by:
getRegisteredScope in interface ConfigurableBeanFactory
Parameters:
scopeName - the name of the scope
Returns:
the registered Scope implementation, or null if none
See Also:
ConfigurableBeanFactory.registerScope(java.lang.String, org.springframework.beans.factory.config.Scope)

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
Parameters:
otherFactory - the other BeanFactory to copy from

registerAlias

public void registerAlias(String beanName,
                          String alias)
                   throws BeanDefinitionStoreException
Description copied from interface: ConfigurableBeanFactory
Given a bean name, create an alias. We typically use this method to support names that are illegal within XML ids (used for bean names).

Typically invoked during factory configuration, but can also be used for runtime registration of aliases. Therefore, a factory implementation should synchronize alias access.

Specified by:
registerAlias in interface ConfigurableBeanFactory
Parameters:
beanName - the canonical name of the bean
alias - the alias to be registered for the bean
Throws:
BeanDefinitionStoreException - if the alias is already in use

beforePrototypeCreation

protected void beforePrototypeCreation(String beanName)
Callback before prototype creation.

The default implementation register the prototype as currently in creation.

Parameters:
beanName - the name of the prototype about to be created
See Also:
isPrototypeCurrentlyInCreation(java.lang.String)

afterPrototypeCreation

protected void afterPrototypeCreation(String beanName)
Callback after prototype creation.

The default implementation marks the prototype as not in creation anymore.

Parameters:
beanName - the name of the prototype that has been created
See Also:
isPrototypeCurrentlyInCreation(java.lang.String)

isPrototypeCurrentlyInCreation

protected final boolean isPrototypeCurrentlyInCreation(String beanName)
Return whether the specified prototype bean is currently in creation (within the current thread).

Parameters:
beanName - the name of the bean

isCurrentlyInCreation

public boolean isCurrentlyInCreation(String beanName)
Description copied from interface: ConfigurableBeanFactory
Return whether the specified bean is currently in creation.

Specified by:
isCurrentlyInCreation in interface ConfigurableBeanFactory
Parameters:
beanName - the name of the bean

destroyBean

public void destroyBean(String beanName,
                        Object beanInstance)
Description copied from interface: ConfigurableBeanFactory
Destroy the given bean instance (usually a prototype instance obtained from this factory) according to its bean definition.

Any exception that arises during destruction should be caught and logged instead of propagated to the caller of this method.

Specified by:
destroyBean in interface ConfigurableBeanFactory
Parameters:
beanName - the name of the bean definition
beanInstance - the bean instance to destroy

destroyBean

protected void destroyBean(String beanName,
                           Object beanInstance,
                           RootBeanDefinition mbd)
Destroy the given bean instance (usually a prototype instance obtained from this factory) according to the given bean definition.

Parameters:
beanName - the name of the bean definition
beanInstance - the bean instance to destroy
mbd - the merged bean definition

destroyScopedBean

public void destroyScopedBean(String beanName)
Description copied from interface: ConfigurableBeanFactory
Destroy the specified scoped bean in the current target scope, if any.

Any exception that arises during destruction should be caught and logged instead of propagated to the caller of this method.

Specified by:
destroyScopedBean in interface ConfigurableBeanFactory
Parameters:
beanName - the name of the scoped bean

transformedBeanName

protected String transformedBeanName(String name)
Return the bean name, stripping out the factory dereference prefix if necessary, and resolving aliases to canonical names.

Parameters:
name - the user-specified name
Returns:
the transformed bean name

originalBeanName

protected String originalBeanName(String name)
Determine the original bean name, resolving locally defined aliases to canonical names.

Parameters:
name - the user-specified name
Returns:
the original bean name

isAlias

protected boolean isAlias(String beanName)
Determine whether this given bean name is defines as an alias (as opposed to the name of an actual bean definition).

Parameters:
beanName - the bean name to check
Returns:
whether the given name is an alias

initBeanWrapper

protected void initBeanWrapper(BeanWrapper bw)
Initialize the given BeanWrapper with the custom editors registered with this factory. To be called for BeanWrappers that will create and populate bean instances.

The default implementation delegates to registerCustomEditors. Can be overridden in subclasses.

Parameters:
bw - the BeanWrapper to initialize
See Also:
registerCustomEditors(org.springframework.beans.PropertyEditorRegistry)

registerCustomEditors

protected void registerCustomEditors(PropertyEditorRegistry registry)
Initialize the given PropertyEditorRegistry with the custom editors registered with this BeanFactory.

To be called for BeanWrappers that will create and populate bean instances, and for SimpleTypeConverter used for constructor argument and factory method type conversion.

Parameters:
registry - the PropertyEditorRegistry to initialize

getMergedBeanDefinition

public RootBeanDefinition getMergedBeanDefinition(String beanName)
                                           throws BeansException
Return a RootBeanDefinition for the given bean name, merging a child bean definition with its parent if necessary.

Parameters:
beanName - the name of the bean to retrieve the merged definition for
Returns:
a (potentially merged) RootBeanDefinition for the given bean
Throws:
NoSuchBeanDefinitionException - if there is no bean with the given name
BeanDefinitionStoreException - in case of an invalid bean definition
BeansException

getMergedBeanDefinition

protected RootBeanDefinition getMergedBeanDefinition(String beanName,
                                                     boolean includingAncestors)
                                              throws BeansException
Return a RootBeanDefinition, even by traversing parent if the parameter is a child definition. Can ask the parent bean factory if not found in this instance.

Parameters:
beanName - the name of the bean to retrieve the merged definition for
includingAncestors - whether to ask the parent bean factory if not found in this instance
Returns:
a (potentially merged) RootBeanDefinition for the given bean
Throws:
NoSuchBeanDefinitionException - if there is no bean with the given name
BeanDefinitionStoreException - in case of an invalid bean definition
BeansException

getMergedBeanDefinition

protected RootBeanDefinition getMergedBeanDefinition(String beanName,
                                                     BeanDefinition bd)
                                              throws BeanDefinitionStoreException
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.

Parameters:
beanName - the name of the bean definition
bd - the original bean definition (Root/ChildBeanDefinition)
Returns:
a (potentially merged) RootBeanDefinition for the given bean
Throws:
BeanDefinitionStoreException - in case of an invalid bean definition

getMergedBeanDefinition

protected RootBeanDefinition getMergedBeanDefinition(String beanName,
                                                     BeanDefinition bd,
                                                     BeanDefinition containingBd)
                                              throws BeanDefinitionStoreException
Return a RootBeanDefinition for the given bean, by merging with the parent if the given bean's definition is a child bean definition.

Parameters:
beanName - the name of the bean definition
bd - the original bean definition (Root/ChildBeanDefinition)
containingBd - the containing bean definition in case of inner bean, or null in case of a top-level bean
Returns:
a (potentially merged) RootBeanDefinition for the given bean
Throws:
BeanDefinitionStoreException - in case of an invalid bean definition

checkMergedBeanDefinition

protected void checkMergedBeanDefinition(RootBeanDefinition mbd,
                                         String beanName,
                                         Object[] args)
                                  throws BeanDefinitionStoreException
Check the given merged bean definition, potentially throwing validation exceptions.

Parameters:
mbd - the merged bean definition to check
beanName - the name of the bean
args - the arguments for bean creation, if any
Throws:
BeanDefinitionStoreException - in case of validation failure

resolveBeanClass

protected Class resolveBeanClass(RootBeanDefinition mbd,
                                 String beanName)
                          throws CannotLoadBeanClassException
Resolve the bean class for the specified bean definition, resolving a bean class name into a Class reference (if necessary) and storing the resolved Class in the bean definition for further use.

Parameters:
mbd - the merged bean definition to determine the class for
beanName - the name of the bean (for error handling purposes)
Returns:
the resolved bean class (or null if none)
Throws:
CannotLoadBeanClassException - if we failed to load the class

isBeanClassMatch

protected boolean isBeanClassMatch(String beanName,
                                   RootBeanDefinition mbd,
                                   Class targetType)
                            throws CannotLoadBeanClassException
Check whether the bean class of the given bean definition matches the specified target type. Allows for lazy loading of the actual bean class, provided that the type match can be determined otherwise.

The default implementation simply delegates to the standard resolveBeanClass method. Subclasses may override this to use a different strategy, such as a throwaway class loaer.

Parameters:
beanName - the name of the bean (for error handling purposes)
mbd - the merged bean definition to determine the class for
targetType - the type to match against (never null)
Returns:
the resolved bean class (or null if none)
Throws:
CannotLoadBeanClassException - if we failed to load the class
See Also:
resolveBeanClass(org.springframework.beans.factory.support.RootBeanDefinition, java.lang.String)

predictBeanType

protected Class predictBeanType(String beanName,
                                RootBeanDefinition mbd)
Predict the eventual bean type (of the processed bean instance) for the specified bean. Called by getType(java.lang.String) and isTypeMatch(java.lang.String, java.lang.Class). Does not need to handle FactoryBeans specifically, since it is only supposed to operate on the raw bean type.

This implementation is simplistic in that it is not able to handle factory methods and InstantiationAwareBeanPostProcessors. It only predicts the bean type correctly for a standard bean. To be overridden in subclasses, applying more sophisticated type detection.

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

getTypeForFactoryBean

protected Class getTypeForFactoryBean(String beanName,
                                      RootBeanDefinition mbd)
Determine the bean type for the given FactoryBean definition, as far as possible. Only called if there is no singleton instance registered for the target bean already.

The default implementation creates the FactoryBean via getBean to call its getObjectType method. Subclasses are encouraged to optimize this, typically by just instantiating the FactoryBean but not populating it yet, trying whether its getObjectType method already returns a type. If no type found, a full FactoryBean creation as performed by this implementation should be used as fallback.

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(), getBean(String)

getTypeForFactoryBean

protected Class getTypeForFactoryBean(FactoryBean factoryBean)
Determine the type for the given FactoryBean.

Parameters:
factoryBean - the FactoryBean instance to check
Returns:
the FactoryBean's object type, or null if the type cannot be determined yet

getObjectForBeanInstance

protected Object getObjectForBeanInstance(Object beanInstance,
                                          String name,
                                          RootBeanDefinition mbd)
Get the object for the given bean instance, either the bean instance itself or its created object in case of a FactoryBean.

Parameters:
beanInstance - the shared bean instance
name - name that may include factory dereference prefix
mbd - the merged bean definition
Returns:
the object to expose for the bean

postProcessObjectFromFactoryBean

protected Object postProcessObjectFromFactoryBean(Object object,
                                                  String beanName)
                                           throws BeansException
Post-process the given object that has been obtained from the FactoryBean. The resulting object will get exposed for bean references.

The default implementation simply returns the given object as-is. Subclasses may override this, for example, to apply post-processors.

Parameters:
object - the object obtained from the FactoryBean.
beanName - the name of the bean
Returns:
the object to expose
Throws:
BeansException - if any post-processing failed

isFactoryBean

public boolean isFactoryBean(String name)
                      throws NoSuchBeanDefinitionException
Determine whether the bean with the given name is a FactoryBean.

Parameters:
name - the name of the bean to check
Returns:
whether the bean is a FactoryBean (false means the bean exists but is not a FactoryBean)
Throws:
NoSuchBeanDefinitionException - if there is no bean with the given name

isBeanNameInUse

protected boolean isBeanNameInUse(String beanName)
Determine whether the given bean name is already in use within this factory, that is, whether there is a local bean registered under this name or an inner bean created with this name.

Parameters:
beanName - the name to check

requiresDestruction

protected boolean requiresDestruction(Object bean,
                                      RootBeanDefinition mbd)
Determine whether the given bean requires destruction on shutdown.

The default implementation checks the DisposableBean interface as well as a specified destroy method and registered DestructionAwareBeanPostProcessors.

Parameters:
bean - the bean instance to check
mbd - the corresponding bean definition
See Also:
DisposableBean, AbstractBeanDefinition.getDestroyMethodName(), DestructionAwareBeanPostProcessor

registerDisposableBeanIfNecessary

protected void registerDisposableBeanIfNecessary(String beanName,
                                                 Object bean,
                                                 RootBeanDefinition mbd)
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). Only applies to singletons.

Also registers bean as dependent on other beans, according to the "depends-on" configuration in the bean definition.

Parameters:
beanName - the name of the bean
bean - the bean instance
mbd - the bean definition for the bean
See Also:
AbstractBeanDefinition.isSingleton(), AbstractBeanDefinition.getDependsOn(), DefaultSingletonBeanRegistry.registerDisposableBean(java.lang.String, org.springframework.beans.factory.DisposableBean), DefaultSingletonBeanRegistry.registerDependentBean(java.lang.String, java.lang.String)

removeSingleton

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

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

containsBeanDefinition

protected abstract boolean containsBeanDefinition(String beanName)
Check if this bean factory contains a bean definition with the given name. Does not consider any hierarchy this factory may participate in. Invoked by 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.

Parameters:
beanName - the name of the bean to look for
Returns:
if this bean factory contains a bean definition with the given name
See Also:
containsBean(java.lang.String), ListableBeanFactory.containsBeanDefinition(java.lang.String)

getBeanDefinition

protected abstract BeanDefinition getBeanDefinition(String beanName)
                                             throws BeansException
Return the bean definition for the given bean name. Subclasses should normally implement caching, as this method is invoked by this class every time bean definition metadata is needed.

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.

Parameters:
beanName - the name of the bean to find a definition for
Returns:
the BeanDefinition for this prototype name (never null)
Throws:
NoSuchBeanDefinitionException - if the bean definition cannot be resolved
BeansException - in case of errors
See Also:
RootBeanDefinition, ChildBeanDefinition, ConfigurableListableBeanFactory.getBeanDefinition(java.lang.String)

createBean

protected abstract Object createBean(String beanName,
                                     RootBeanDefinition mbd,
                                     Object[] args)
                              throws BeanCreationException
Create a bean instance for the given bean definition. The bean definition will already have been merged with the parent definition in case of a child definition.

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.

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

The Spring Framework

Copyright © 2002-2007 The Spring Framework.