Class AbstractBeanFactory
- All Implemented Interfaces:
BeanFactory
,ConfigurableBeanFactory
,SingletonBeanRegistry
,HierarchicalBeanFactory
,AliasRegistry
- Direct Known Subclasses:
AbstractAutowireCapableBeanFactory
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, Costin Leau, Chris Beams, Phillip Webb, Sam Brannen
- See Also:
-
Field Summary
Fields inherited from class org.springframework.core.SimpleAliasRegistry
logger
Fields inherited from interface org.springframework.beans.factory.BeanFactory
FACTORY_BEAN_PREFIX
Fields inherited from interface org.springframework.beans.factory.config.ConfigurableBeanFactory
SCOPE_PROTOTYPE, SCOPE_SINGLETON
-
Constructor Summary
ConstructorDescriptionCreate a new AbstractBeanFactory.AbstractBeanFactory
(BeanFactory parentBeanFactory) Create a new AbstractBeanFactory with the given parent. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addBeanPostProcessor
(BeanPostProcessor beanPostProcessor) Add a new BeanPostProcessor that will get applied to beans created by this factory.void
addBeanPostProcessors
(Collection<? extends BeanPostProcessor> beanPostProcessors) Add new BeanPostProcessors that will get applied to beans created by this factory.void
addEmbeddedValueResolver
(StringValueResolver valueResolver) Add a String resolver for embedded values such as annotation attributes.void
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.protected void
cleanupAfterBeanCreationFailure
(String beanName) Perform appropriate cleanup of cached metadata after bean creation failed.protected void
clearMergedBeanDefinition
(String beanName) Remove the merged bean definition for the specified bean, recreating it on next access.void
Clear the merged bean definition cache, removing entries for beans which are not considered eligible for full metadata caching yet.boolean
containsBean
(String name) Does this bean factory contain a bean definition or externally registered singleton instance 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.void
Initialize the given PropertyEditorRegistry with the custom editors that have been registered with this BeanFactory.protected abstract Object
createBean
(String beanName, RootBeanDefinition mbd, Object[] args) Create a bean instance for the given merged bean definition (and arguments).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 bean, 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.protected <T> T
Return an instance, which may be shared or independent, of the specified bean.protected Object
evaluateBeanDefinitionString
(String value, BeanDefinition beanDefinition) Evaluate the given String as contained in a bean definition, potentially resolving it as an expression.String[]
getAliases
(String name) Return the aliases for the given name, if defined.Return theApplicationStartup
for this bean factory.Return an instance, which may be shared or independent, of the specified bean.<T> T
Return an instance, which may be shared or independent, of the specified bean.<T> T
Return an instance, which may be shared or independent, of the specified bean.Return an instance, which may be shared or independent, of the specified bean.Return this factory's class loader for loading bean classes (onlynull
if even the system ClassLoader isn't accessible).protected abstract BeanDefinition
getBeanDefinition
(String beanName) Return the bean definition for the given bean name.Return the resolution strategy for expressions in bean definition values.int
Return the current number of registered BeanPostProcessors, if any.Return the list of BeanPostProcessors that will get applied to beans created with this factory.Return the associated ConversionService, if any.Map<Class<?>,
Class<? extends PropertyEditor>> Return the map of custom editors, with Classes as keys and PropertyEditor classes as values.protected TypeConverter
Return the custom TypeConverter to use, if any.Return a 'merged' BeanDefinition 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
getMergedLocalBeanDefinition
(String beanName) Return a merged RootBeanDefinition, traversing the parent bean definition if the specified bean corresponds to a child bean definition.protected Object
getObjectForBeanInstance
(Object beanInstance, String name, String beanName, RootBeanDefinition mbd) Get the object for the given bean instance, either the bean instance itself or its created object in case of a FactoryBean.Return the parent bean factory, ornull
if there is none.Return the set of PropertyEditorRegistrars.getRegisteredScope
(String scopeName) Return the Scope implementation for the given scope name, if any.String[]
Return the names of all currently registered scopes.Return the temporary ClassLoader to use for type matching purposes, if any.Class<?>
Determine the type of the bean with the given name.Class<?>
Determine the type of the bean with the given name.Obtain a type converter as used by this BeanFactory.protected ResolvableType
getTypeForFactoryBean
(String beanName, RootBeanDefinition mbd, boolean allowInit) Determine the bean type for the given FactoryBean definition, as far as possible.protected boolean
Check whether this factory's bean creation phase already started, i.e.protected boolean
Return whether this factory holds a DestructionAwareBeanPostProcessor that will get applied to singleton beans on shutdown.boolean
Determine whether an embedded value resolver has been registered with this bean factory, to be applied throughConfigurableBeanFactory.resolveEmbeddedValue(String)
.protected boolean
Return whether this factory holds a InstantiationAwareBeanPostProcessor that will get applied to singleton beans on creation.protected void
Initialize the given BeanWrapper with the custom editors registered with this factory.boolean
isActuallyInCreation
(String beanName) protected boolean
isBeanEligibleForMetadataCaching
(String beanName) Determine whether the specified bean is eligible for having its bean definition metadata cached.boolean
isBeanNameInUse
(String beanName) Determine whether the given bean name is already in use within this factory, i.e.boolean
Return whether to cache bean metadata such as given bean definitions (in merged fashion) and resolved bean classes.boolean
isFactoryBean
(String name) Determine whether the bean with the given name is a FactoryBean.protected boolean
isFactoryBean
(String beanName, RootBeanDefinition mbd) Check whether the given bean is defined as aFactoryBean
.boolean
isPrototype
(String name) Is this bean a prototype? That is, willBeanFactory.getBean(java.lang.String)
always return independent instances?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? That is, willBeanFactory.getBean(java.lang.String)
always return the same instance?boolean
isTypeMatch
(String name, Class<?> typeToMatch) Check whether the bean with the given name matches the specified type.boolean
isTypeMatch
(String name, ResolvableType typeToMatch) Check whether the bean with the given name matches the specified type.protected boolean
isTypeMatch
(String name, ResolvableType typeToMatch, boolean allowFactoryBeanInit) Internal extended variant ofisTypeMatch(String, ResolvableType)
to check whether the bean with the given name matches the specified type.protected void
markBeanAsCreated
(String beanName) Mark the specified bean as already created (or about to be created).protected String
originalBeanName
(String name) Determine the original bean name, resolving locally defined aliases to canonical names.protected Class<?>
predictBeanType
(String beanName, RootBeanDefinition mbd, Class<?>... typesToMatch) Predict the eventual bean type (of the processed bean instance) for the specified bean.void
registerCustomEditor
(Class<?> requiredType, Class<? extends PropertyEditor> propertyEditorClass) 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 that have been 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 boolean
Remove the singleton instance (if any) for the given bean name, but only if it hasn't been used for other purposes than type checking.protected boolean
requiresDestruction
(Object bean, RootBeanDefinition mbd) Determine whether the given bean requires destruction on shutdown.protected Class<?>
resolveBeanClass
(RootBeanDefinition mbd, String beanName, Class<?>... typesToMatch) 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.resolveEmbeddedValue
(String value) Resolve the given embedded value, e.g.void
setApplicationStartup
(ApplicationStartup applicationStartup) Set theApplicationStartup
for this bean factory.void
setBeanClassLoader
(ClassLoader beanClassLoader) Set the class loader to use for loading bean classes.void
Specify the resolution strategy for expressions in bean definition values.void
setCacheBeanMetadata
(boolean cacheBeanMetadata) Set whether to cache bean metadata such as given bean definitions (in merged fashion) and resolved bean classes.void
setConversionService
(ConversionService conversionService) Specify a Spring 3.0 ConversionService to use for converting property values, as an alternative to JavaBeans PropertyEditors.void
setParentBeanFactory
(BeanFactory parentBeanFactory) Set the parent of this bean factory.void
setTempClassLoader
(ClassLoader tempClassLoader) Specify a temporary ClassLoader to use for type matching purposes.void
setTypeConverter
(TypeConverter typeConverter) Set a custom type converter that this BeanFactory should use for converting bean property values, constructor argument values, etc.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.FactoryBeanRegistrySupport
clearSingletonCache, getCachedObjectForFactoryBean, getFactoryBean, getObjectFromFactoryBean, getTypeForFactoryBean, postProcessObjectFromFactoryBean, removeSingleton
Methods inherited from class org.springframework.beans.factory.support.DefaultSingletonBeanRegistry
addSingleton, addSingletonFactory, afterSingletonCreation, beforeSingletonCreation, containsSingleton, destroyBean, destroySingleton, destroySingletons, getDependenciesForBean, getDependentBeans, getSingleton, getSingleton, getSingleton, getSingletonCount, getSingletonMutex, getSingletonNames, hasDependentBean, isCurrentlyInCreation, isDependent, isSingletonCurrentlyInCreation, onSuppressedException, registerContainedBean, registerDependentBean, registerDisposableBean, registerSingleton, setCurrentlyInCreation
Methods inherited from class org.springframework.core.SimpleAliasRegistry
allowAliasOverriding, canonicalName, checkForAliasCircle, hasAlias, isAlias, registerAlias, removeAlias, resolveAliases
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.beans.factory.BeanFactory
getBean, getBean, getBeanProvider, getBeanProvider
Methods inherited from interface org.springframework.beans.factory.config.ConfigurableBeanFactory
destroySingletons, getDependenciesForBean, getDependentBeans, isCurrentlyInCreation, registerAlias, registerDependentBean, resolveAliases, setCurrentlyInCreation
Methods inherited from interface org.springframework.beans.factory.config.SingletonBeanRegistry
containsSingleton, getSingleton, getSingletonCount, getSingletonMutex, getSingletonNames, registerSingleton
-
Constructor Details
-
AbstractBeanFactory
public AbstractBeanFactory()Create a new AbstractBeanFactory. -
AbstractBeanFactory
Create a new AbstractBeanFactory with the given parent.- Parameters:
parentBeanFactory
- parent bean factory, ornull
if none- See Also:
-
-
Method Details
-
getBean
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 interfaceBeanFactory
- Parameters:
name
- the name of the bean to retrieve- Returns:
- an instance of the bean
- Throws:
NoSuchBeanDefinitionException
- if there is no bean with the specified nameBeansException
- if the bean could not be obtained
-
getBean
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 withBeanFactory.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 interfaceBeanFactory
- Parameters:
name
- the name of the bean to retrieverequiredType
- type the bean must match; can be an interface or superclass- Returns:
- an instance of the bean
- Throws:
NoSuchBeanDefinitionException
- if there is no such bean definitionBeanNotOfRequiredTypeException
- if the bean is not of the required typeBeansException
- if the bean could not be created
-
getBean
Description copied from interface:BeanFactory
Return an instance, which may be shared or independent, of the specified bean.Allows for specifying explicit constructor arguments / factory method arguments, overriding the specified default arguments (if any) in the bean definition.
- Specified by:
getBean
in interfaceBeanFactory
- Parameters:
name
- the name of the bean to retrieveargs
- arguments to use when creating a bean instance using explicit arguments (only applied when creating a new instance as opposed to retrieving an existing one)- Returns:
- an instance of the bean
- Throws:
NoSuchBeanDefinitionException
- if there is no such bean definitionBeanDefinitionStoreException
- if arguments have been given but the affected bean isn't a prototypeBeansException
- if the bean could not be created
-
getBean
public <T> T getBean(String name, @Nullable Class<T> requiredType, @Nullable 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 retrieverequiredType
- the required type of the bean to retrieveargs
- arguments to use when creating a bean instance using explicit arguments (only applied when creating a new instance as opposed to retrieving an existing one)- Returns:
- an instance of the bean
- Throws:
BeansException
- if the bean could not be created
-
doGetBean
protected <T> T doGetBean(String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly) throws BeansException Return an instance, which may be shared or independent, of the specified bean.- Parameters:
name
- the name of the bean to retrieverequiredType
- the required type of the bean to retrieveargs
- arguments to use when creating a bean instance using explicit arguments (only applied when creating a new instance as opposed to retrieving an existing one)typeCheckOnly
- whether the instance is obtained for a type check, not for actual use- Returns:
- an instance of the bean
- Throws:
BeansException
- if the bean could not be created
-
containsBean
Description copied from interface:BeanFactory
Does this bean factory contain a bean definition or externally registered singleton instance with the given name?If the given name is an alias, it will be translated back to the corresponding canonical bean name.
If this factory is hierarchical, will ask any parent factory if the bean cannot be found in this factory instance.
If a bean definition or singleton instance matching the given name is found, this method will return
true
whether the named bean definition is concrete or abstract, lazy or eager, in scope or not. Therefore, note that atrue
return value from this method does not necessarily indicate thatBeanFactory.getBean(java.lang.String)
will be able to obtain an instance for the same name.- Specified by:
containsBean
in interfaceBeanFactory
- Parameters:
name
- the name of the bean to query- Returns:
- whether a bean with the given name is present
-
isSingleton
Description copied from interface:BeanFactory
Is this bean a shared singleton? That is, willBeanFactory.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 theBeanFactory.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 interfaceBeanFactory
- 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:
-
isPrototype
Description copied from interface:BeanFactory
Is this bean a prototype? That is, willBeanFactory.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 theBeanFactory.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 interfaceBeanFactory
- 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:
-
isTypeMatch
public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException Description copied from interface:BeanFactory
Check whether the bean with the given name matches the specified type. More specifically, check whether aBeanFactory.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 interfaceBeanFactory
- Parameters:
name
- the name of the bean to querytypeToMatch
- the type to match against (as aResolvableType
)- 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:
-
isTypeMatch
protected boolean isTypeMatch(String name, ResolvableType typeToMatch, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException Internal extended variant ofisTypeMatch(String, ResolvableType)
to check whether the bean with the given name matches the specified type. Allow additional constraints to be applied to ensure that beans are not created early.- Parameters:
name
- the name of the bean to querytypeToMatch
- the type to match against (as aResolvableType
)- 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- Since:
- 5.2
- See Also:
-
isTypeMatch
Description copied from interface:BeanFactory
Check whether the bean with the given name matches the specified type. More specifically, check whether aBeanFactory.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 interfaceBeanFactory
- Parameters:
name
- the name of the bean to querytypeToMatch
- the type to match against (as aClass
)- 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:
-
getType
Description copied from interface:BeanFactory
Determine the type of the bean with the given name. More specifically, determine the type of object thatBeanFactory.getBean(java.lang.String)
would return for the given name.For a
FactoryBean
, return the type of object that the FactoryBean creates, as exposed byFactoryBean.getObjectType()
. This may lead to the initialization of a previously uninitializedFactoryBean
(seeBeanFactory.getType(String, boolean)
).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 interfaceBeanFactory
- 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:
-
getType
@Nullable public Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException Description copied from interface:BeanFactory
Determine the type of the bean with the given name. More specifically, determine the type of object thatBeanFactory.getBean(java.lang.String)
would return for the given name.For a
FactoryBean
, return the type of object that the FactoryBean creates, as exposed byFactoryBean.getObjectType()
. Depending on theallowFactoryBeanInit
flag, this may lead to the initialization of a previously uninitializedFactoryBean
if no early type information is available.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 interfaceBeanFactory
- Parameters:
name
- the name of the bean to queryallowFactoryBeanInit
- whether aFactoryBean
may get initialized just for the purpose of determining its object type- Returns:
- the type of the bean, or
null
if not determinable - Throws:
NoSuchBeanDefinitionException
- if there is no bean with the given name- See Also:
-
getAliases
Description copied from interface:AliasRegistry
Return the aliases for the given name, if defined.- Specified by:
getAliases
in interfaceAliasRegistry
- Specified by:
getAliases
in interfaceBeanFactory
- Overrides:
getAliases
in classSimpleAliasRegistry
- Parameters:
name
- the name to check for aliases- Returns:
- the aliases, or an empty array if none
- See Also:
-
getParentBeanFactory
Description copied from interface:HierarchicalBeanFactory
Return the parent bean factory, ornull
if there is none.- Specified by:
getParentBeanFactory
in interfaceHierarchicalBeanFactory
-
containsLocalBean
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 interfaceHierarchicalBeanFactory
- 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:
-
setParentBeanFactory
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 interfaceConfigurableBeanFactory
- Parameters:
parentBeanFactory
- the parent BeanFactory- See Also:
-
setBeanClassLoader
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 interfaceConfigurableBeanFactory
- Parameters:
beanClassLoader
- the class loader to use, ornull
to suggest the default class loader
-
getBeanClassLoader
Description copied from interface:ConfigurableBeanFactory
Return this factory's class loader for loading bean classes (onlynull
if even the system ClassLoader isn't accessible).- Specified by:
getBeanClassLoader
in interfaceConfigurableBeanFactory
- See Also:
-
setTempClassLoader
Description copied from interface:ConfigurableBeanFactory
Specify a temporary ClassLoader to use for type matching purposes. Default is none, simply using the standard bean ClassLoader.A temporary ClassLoader is usually just specified if load-time weaving is involved, to make sure that actual bean classes are loaded as lazily as possible. The temporary loader is then removed once the BeanFactory completes its bootstrap phase.
- Specified by:
setTempClassLoader
in interfaceConfigurableBeanFactory
-
getTempClassLoader
Description copied from interface:ConfigurableBeanFactory
Return the temporary ClassLoader to use for type matching purposes, if any.- Specified by:
getTempClassLoader
in interfaceConfigurableBeanFactory
-
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 interfaceConfigurableBeanFactory
-
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 interfaceConfigurableBeanFactory
-
setBeanExpressionResolver
Description copied from interface:ConfigurableBeanFactory
Specify the resolution strategy for expressions in bean definition values.There is no expression support active in a BeanFactory by default. An ApplicationContext will typically set a standard expression strategy here, supporting "#{...}" expressions in a Unified EL compatible style.
- Specified by:
setBeanExpressionResolver
in interfaceConfigurableBeanFactory
-
getBeanExpressionResolver
Description copied from interface:ConfigurableBeanFactory
Return the resolution strategy for expressions in bean definition values.- Specified by:
getBeanExpressionResolver
in interfaceConfigurableBeanFactory
-
setConversionService
Description copied from interface:ConfigurableBeanFactory
Specify a Spring 3.0 ConversionService to use for converting property values, as an alternative to JavaBeans PropertyEditors.- Specified by:
setConversionService
in interfaceConfigurableBeanFactory
-
getConversionService
Description copied from interface:ConfigurableBeanFactory
Return the associated ConversionService, if any.- Specified by:
getConversionService
in interfaceConfigurableBeanFactory
-
addPropertyEditorRegistrar
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
ConfigurableBeanFactory.registerCustomEditor(java.lang.Class<?>, java.lang.Class<? extends java.beans.PropertyEditor>)
.- Specified by:
addPropertyEditorRegistrar
in interfaceConfigurableBeanFactory
- Parameters:
registrar
- the PropertyEditorRegistrar to register
-
getPropertyEditorRegistrars
Return the set of PropertyEditorRegistrars. -
registerCustomEditor
public void registerCustomEditor(Class<?> requiredType, Class<? extends PropertyEditor> propertyEditorClass) 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 preferable to use
ConfigurableBeanFactory.addPropertyEditorRegistrar(org.springframework.beans.PropertyEditorRegistrar)
instead of this method, to avoid for the need for synchronization on custom editors.- Specified by:
registerCustomEditor
in interfaceConfigurableBeanFactory
- Parameters:
requiredType
- type of the propertypropertyEditorClass
- thePropertyEditor
class to register
-
copyRegisteredEditorsTo
Description copied from interface:ConfigurableBeanFactory
Initialize the given PropertyEditorRegistry with the custom editors that have been registered with this BeanFactory.- Specified by:
copyRegisteredEditorsTo
in interfaceConfigurableBeanFactory
- Parameters:
registry
- the PropertyEditorRegistry to initialize
-
getCustomEditors
Return the map of custom editors, with Classes as keys and PropertyEditor classes as values. -
setTypeConverter
Description copied from interface:ConfigurableBeanFactory
Set a custom type converter that this BeanFactory should use for converting bean property values, constructor argument values, etc.This will override the default PropertyEditor mechanism and hence make any custom editors or custom editor registrars irrelevant.
-
getCustomTypeConverter
Return the custom TypeConverter to use, if any.- Returns:
- the custom TypeConverter, or
null
if none specified
-
getTypeConverter
Description copied from interface:ConfigurableBeanFactory
Obtain a type converter as used by this BeanFactory. This may be a fresh instance for each call, since TypeConverters are usually not thread-safe.If the default PropertyEditor mechanism is active, the returned TypeConverter will be aware of all custom editors that have been registered.
- Specified by:
getTypeConverter
in interfaceConfigurableBeanFactory
-
addEmbeddedValueResolver
Description copied from interface:ConfigurableBeanFactory
Add a String resolver for embedded values such as annotation attributes.- Specified by:
addEmbeddedValueResolver
in interfaceConfigurableBeanFactory
- Parameters:
valueResolver
- the String resolver to apply to embedded values
-
hasEmbeddedValueResolver
public boolean hasEmbeddedValueResolver()Description copied from interface:ConfigurableBeanFactory
Determine whether an embedded value resolver has been registered with this bean factory, to be applied throughConfigurableBeanFactory.resolveEmbeddedValue(String)
.- Specified by:
hasEmbeddedValueResolver
in interfaceConfigurableBeanFactory
-
resolveEmbeddedValue
Description copied from interface:ConfigurableBeanFactory
Resolve the given embedded value, e.g. an annotation attribute.- Specified by:
resolveEmbeddedValue
in interfaceConfigurableBeanFactory
- Parameters:
value
- the value to resolve- Returns:
- the resolved value (may be the original value as-is)
-
addBeanPostProcessor
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.Note: Post-processors submitted here will be applied in the order of registration; any ordering semantics expressed through implementing the
Ordered
interface will be ignored. Note that autodetected post-processors (e.g. as beans in an ApplicationContext) will always be applied after programmatically registered ones.- Specified by:
addBeanPostProcessor
in interfaceConfigurableBeanFactory
- Parameters:
beanPostProcessor
- the post-processor to register
-
addBeanPostProcessors
Add new BeanPostProcessors that will get applied to beans created by this factory. To be invoked during factory configuration.- Since:
- 5.3
- See Also:
-
getBeanPostProcessorCount
public int getBeanPostProcessorCount()Description copied from interface:ConfigurableBeanFactory
Return the current number of registered BeanPostProcessors, if any.- Specified by:
getBeanPostProcessorCount
in interfaceConfigurableBeanFactory
-
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 creation. -
hasDestructionAwareBeanPostProcessors
protected boolean hasDestructionAwareBeanPostProcessors()Return whether this factory holds a DestructionAwareBeanPostProcessor that will get applied to singleton beans on shutdown. -
registerScope
Description copied from interface:ConfigurableBeanFactory
Register the given scope, backed by the given Scope implementation.- Specified by:
registerScope
in interfaceConfigurableBeanFactory
- Parameters:
scopeName
- the scope identifierscope
- the backing Scope implementation
-
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 interfaceConfigurableBeanFactory
- Returns:
- the array of scope names, or an empty array if none
- See Also:
-
getRegisteredScope
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 interfaceConfigurableBeanFactory
- Parameters:
scopeName
- the name of the scope- Returns:
- the registered Scope implementation, or
null
if none - See Also:
-
setApplicationStartup
Description copied from interface:ConfigurableBeanFactory
Set theApplicationStartup
for this bean factory.This allows the application context to record metrics during application startup.
- Specified by:
setApplicationStartup
in interfaceConfigurableBeanFactory
- Parameters:
applicationStartup
- the new application startup
-
getApplicationStartup
Description copied from interface:ConfigurableBeanFactory
Return theApplicationStartup
for this bean factory.- Specified by:
getApplicationStartup
in interfaceConfigurableBeanFactory
-
copyConfigurationFrom
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 interfaceConfigurableBeanFactory
- Parameters:
otherFactory
- the other BeanFactory to copy from
-
getMergedBeanDefinition
Return a 'merged' BeanDefinition for the given bean name, merging a child bean definition with its parent if necessary.This
getMergedBeanDefinition
considers bean definition in ancestors as well.- Specified by:
getMergedBeanDefinition
in interfaceConfigurableBeanFactory
- Parameters:
name
- the name of the bean to retrieve the merged definition for (may be an alias)- Returns:
- a (potentially merged) RootBeanDefinition for the given bean
- Throws:
NoSuchBeanDefinitionException
- if there is no bean with the given nameBeanDefinitionStoreException
- in case of an invalid bean definitionNoSuchBeanDefinitionException
- if there is no bean definition with the given nameBeansException
-
isFactoryBean
Description copied from interface:ConfigurableBeanFactory
Determine whether the bean with the given name is a FactoryBean.- Specified by:
isFactoryBean
in interfaceConfigurableBeanFactory
- 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
-
isActuallyInCreation
- Overrides:
isActuallyInCreation
in classDefaultSingletonBeanRegistry
-
isPrototypeCurrentlyInCreation
Return whether the specified prototype bean is currently in creation (within the current thread).- Parameters:
beanName
- the name of the bean
-
beforePrototypeCreation
Callback before prototype creation.The default implementation registers the prototype as currently in creation.
- Parameters:
beanName
- the name of the prototype about to be created- See Also:
-
afterPrototypeCreation
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:
-
destroyBean
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 interfaceConfigurableBeanFactory
- Parameters:
beanName
- the name of the bean definitionbeanInstance
- the bean instance to destroy
-
destroyBean
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 definitionbean
- the bean instance to destroymbd
- the merged bean definition
-
destroyScopedBean
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 interfaceConfigurableBeanFactory
- Parameters:
beanName
- the name of the scoped bean
-
transformedBeanName
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
Determine the original bean name, resolving locally defined aliases to canonical names.- Parameters:
name
- the user-specified name- Returns:
- the original bean name
-
initBeanWrapper
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(org.springframework.beans.PropertyEditorRegistry)
. Can be overridden in subclasses.- Parameters:
bw
- the BeanWrapper to initialize
-
registerCustomEditors
Initialize the given PropertyEditorRegistry with the custom editors that have been 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
-
getMergedLocalBeanDefinition
Return a merged RootBeanDefinition, traversing the parent bean definition if the specified bean corresponds to a child bean definition.- 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 nameBeanDefinitionStoreException
- in case of an invalid bean definitionBeansException
-
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 definitionbd
- 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, @Nullable 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 definitionbd
- the original bean definition (Root/ChildBeanDefinition)containingBd
- the containing bean definition in case of inner bean, ornull
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, @Nullable Object[] args) throws BeanDefinitionStoreException Check the given merged bean definition, potentially throwing validation exceptions.- Parameters:
mbd
- the merged bean definition to checkbeanName
- the name of the beanargs
- the arguments for bean creation, if any- Throws:
BeanDefinitionStoreException
- in case of validation failure
-
clearMergedBeanDefinition
Remove the merged bean definition for the specified bean, recreating it on next access.- Parameters:
beanName
- the bean name to clear the merged definition for
-
clearMetadataCache
public void clearMetadataCache()Clear the merged bean definition cache, removing entries for beans which are not considered eligible for full metadata caching yet.Typically triggered after changes to the original bean definitions, e.g. after applying a
BeanFactoryPostProcessor
. Note that metadata for beans which have already been created at this point will be kept around.- Since:
- 4.2
-
resolveBeanClass
@Nullable protected Class<?> resolveBeanClass(RootBeanDefinition mbd, String beanName, Class<?>... typesToMatch) 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 forbeanName
- the name of the bean (for error handling purposes)typesToMatch
- the types to match in case of internal type matching purposes (also signals that the returnedClass
will never be exposed to application code)- Returns:
- the resolved bean class (or
null
if none) - Throws:
CannotLoadBeanClassException
- if we failed to load the class
-
evaluateBeanDefinitionString
@Nullable protected Object evaluateBeanDefinitionString(@Nullable String value, @Nullable BeanDefinition beanDefinition) Evaluate the given String as contained in a bean definition, potentially resolving it as an expression.- Parameters:
value
- the value to checkbeanDefinition
- the bean definition that the value comes from- Returns:
- the resolved value
- See Also:
-
predictBeanType
@Nullable protected Class<?> predictBeanType(String beanName, RootBeanDefinition mbd, Class<?>... typesToMatch) Predict the eventual bean type (of the processed bean instance) for the specified bean. Called bygetType(java.lang.String)
andisTypeMatch(java.lang.String, org.springframework.core.ResolvableType)
. 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 beanmbd
- the merged bean definition to determine the type fortypesToMatch
- the types to match in case of internal type matching purposes (also signals that the returnedClass
will never be exposed to application code)- Returns:
- the type of the bean, or
null
if not predictable
-
isFactoryBean
Check whether the given bean is defined as aFactoryBean
.- Parameters:
beanName
- the name of the beanmbd
- the corresponding bean definition
-
getTypeForFactoryBean
protected ResolvableType getTypeForFactoryBean(String beanName, RootBeanDefinition mbd, boolean allowInit) 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 implementation is allowed to instantiate the target factory bean ifallowInit
istrue
and the type cannot be determined another way; otherwise it is restricted to introspecting signatures and related metadata.If no
FactoryBean.OBJECT_TYPE_ATTRIBUTE
if set on the bean definition andallowInit
istrue
, the default implementation will create the FactoryBean viagetBean
to call itsgetObjectType
method. Subclasses are encouraged to optimize this, typically by inspecting the generic signature of the factory bean class or the factory method that creates it. If subclasses do instantiate the FactoryBean, they should consider trying thegetObjectType
method without fully populating the bean. If this fails, a full FactoryBean creation as performed by this implementation should be used as fallback.- Parameters:
beanName
- the name of the beanmbd
- the merged bean definition for the beanallowInit
- if initialization of the FactoryBean is permitted if the type cannot be determined another way- Returns:
- the type for the bean if determinable, otherwise
ResolvableType.NONE
- Since:
- 5.2
- See Also:
-
markBeanAsCreated
Mark the specified bean as already created (or about to be created).This allows the bean factory to optimize its caching for repeated creation of the specified bean.
- Parameters:
beanName
- the name of the bean
-
cleanupAfterBeanCreationFailure
Perform appropriate cleanup of cached metadata after bean creation failed.- Parameters:
beanName
- the name of the bean
-
isBeanEligibleForMetadataCaching
Determine whether the specified bean is eligible for having its bean definition metadata cached.- Parameters:
beanName
- the name of the bean- Returns:
true
if the bean's metadata may be cached at this point already
-
removeSingletonIfCreatedForTypeCheckOnly
Remove the singleton instance (if any) for the given bean name, but only if it hasn't been used for other purposes than type checking.- Parameters:
beanName
- the name of the bean- Returns:
true
if actually removed,false
otherwise
-
hasBeanCreationStarted
protected boolean hasBeanCreationStarted()Check whether this factory's bean creation phase already started, i.e. whether any bean has been marked as created in the meantime.- Since:
- 4.2.2
- See Also:
-
getObjectForBeanInstance
protected Object getObjectForBeanInstance(Object beanInstance, String name, String beanName, @Nullable 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 instancename
- the name that may include factory dereference prefixbeanName
- the canonical bean namembd
- the merged bean definition- Returns:
- the object to expose for the bean
-
isBeanNameInUse
Determine whether the given bean name is already in use within this factory, i.e. whether there is a local bean or alias registered under this name or an inner bean created with this name.- Parameters:
beanName
- the name to check
-
requiresDestruction
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 checkmbd
- the corresponding bean definition- See Also:
-
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.- Parameters:
beanName
- the name of the beanbean
- the bean instancembd
- the bean definition for the bean- See Also:
-
containsBeanDefinition
Check if this bean factory contains a bean definition with the given name. Does not consider any hierarchy this factory may participate in. Invoked bycontainsBean
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:
-
getBeanDefinition
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 resolvedBeansException
- in case of errors- See Also:
-
createBean
protected abstract Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) throws BeanCreationException Create a bean instance for the given merged bean definition (and arguments). The bean definition will already have been merged with the parent definition in case of a child definition.All bean retrieval methods delegate to this method for actual bean creation.
- Parameters:
beanName
- the name of the beanmbd
- the merged bean definition for the beanargs
- explicit arguments to use for constructor or factory method invocation- Returns:
- a new instance of the bean
- Throws:
BeanCreationException
- if the bean could not be created
-