public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport implements ConfigurableBeanFactory
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
.
getBeanDefinition(java.lang.String)
,
createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[])
,
AbstractAutowireCapableBeanFactory.createBean(java.lang.Class<T>)
,
DefaultListableBeanFactory.getBeanDefinition(java.lang.String)
Modifier and Type | Field and Description |
---|---|
private java.util.Set<java.lang.String> |
alreadyCreated
Names of beans that have already been created at least once
|
private java.lang.ClassLoader |
beanClassLoader
ClassLoader to resolve bean class names with, if necessary
|
private BeanExpressionResolver |
beanExpressionResolver
Resolution strategy for expressions in bean definition values
|
private java.util.List<BeanPostProcessor> |
beanPostProcessors
BeanPostProcessors to apply in createBean
|
private boolean |
cacheBeanMetadata
Whether to cache bean metadata or rather reobtain it for every access
|
private ConversionService |
conversionService
Spring ConversionService to use instead of PropertyEditors
|
private java.util.Map<java.lang.Class<?>,java.lang.Class<? extends java.beans.PropertyEditor>> |
customEditors
Custom PropertyEditors to apply to the beans of this factory
|
private java.util.List<StringValueResolver> |
embeddedValueResolvers
String resolvers to apply e.g.
|
private boolean |
hasDestructionAwareBeanPostProcessors
Indicates whether any DestructionAwareBeanPostProcessors have been registered
|
private boolean |
hasInstantiationAwareBeanPostProcessors
Indicates whether any InstantiationAwareBeanPostProcessors have been registered
|
private java.util.Map<java.lang.String,RootBeanDefinition> |
mergedBeanDefinitions
Map from bean name to merged RootBeanDefinition
|
private BeanFactory |
parentBeanFactory
Parent bean factory, for bean inheritance support
|
private java.util.Set<PropertyEditorRegistrar> |
propertyEditorRegistrars
Custom PropertyEditorRegistrars to apply to the beans of this factory
|
private java.lang.ThreadLocal<java.lang.Object> |
prototypesCurrentlyInCreation
Names of beans that are currently in creation
|
private java.util.Map<java.lang.String,Scope> |
scopes
Map from scope identifier String to corresponding Scope
|
private SecurityContextProvider |
securityContextProvider
Security context used when running with a SecurityManager
|
private java.lang.ClassLoader |
tempClassLoader
ClassLoader to temporarily resolve bean class names with, if necessary
|
private TypeConverter |
typeConverter
A custom TypeConverter to use, overriding the default PropertyEditor mechanism
|
logger, NULL_OBJECT
SCOPE_PROTOTYPE, SCOPE_SINGLETON
FACTORY_BEAN_PREFIX
Constructor and Description |
---|
AbstractBeanFactory()
Create a new AbstractBeanFactory.
|
AbstractBeanFactory(BeanFactory parentBeanFactory)
Create a new AbstractBeanFactory with the given parent.
|
Modifier and Type | Method and Description |
---|---|
void |
addBeanPostProcessor(BeanPostProcessor beanPostProcessor)
Add a new BeanPostProcessor 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 |
addPropertyEditorRegistrar(PropertyEditorRegistrar registrar)
Add a PropertyEditorRegistrar to be applied to all bean creation processes.
|
protected void |
afterPrototypeCreation(java.lang.String beanName)
Callback after prototype creation.
|
protected void |
beforePrototypeCreation(java.lang.String beanName)
Callback before prototype creation.
|
protected void |
checkMergedBeanDefinition(RootBeanDefinition mbd,
java.lang.String beanName,
java.lang.Object[] args)
Check the given merged bean definition,
potentially throwing validation exceptions.
|
protected void |
cleanupAfterBeanCreationFailure(java.lang.String beanName)
Perform appropriate cleanup of cached metadata after bean creation failed.
|
protected void |
clearMergedBeanDefinition(java.lang.String beanName)
Remove the merged bean definition for the specified bean,
recreating it on next access.
|
boolean |
containsBean(java.lang.String name)
Does this bean factory contain a bean definition or externally registered singleton
instance with the given name?
|
protected abstract boolean |
containsBeanDefinition(java.lang.String beanName)
Check if this bean factory contains a bean definition with the given name.
|
boolean |
containsLocalBean(java.lang.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 |
copyRegisteredEditorsTo(PropertyEditorRegistry registry)
Initialize the given PropertyEditorRegistry with the custom editors
that have been registered with this BeanFactory.
|
protected abstract java.lang.Object |
createBean(java.lang.String beanName,
RootBeanDefinition mbd,
java.lang.Object[] args)
Create a bean instance for the given bean definition.
|
void |
destroyBean(java.lang.String beanName,
java.lang.Object beanInstance)
Destroy the given bean instance (usually a prototype instance
obtained from this factory) according to its bean definition.
|
protected void |
destroyBean(java.lang.String beanName,
java.lang.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(java.lang.String beanName)
Destroy the specified scoped bean in the current target scope, if any.
|
protected <T> T |
doGetBean(java.lang.String name,
java.lang.Class<T> requiredType,
java.lang.Object[] args,
boolean typeCheckOnly)
Return an instance, which may be shared or independent, of the specified bean.
|
private java.lang.Class<?> |
doResolveBeanClass(RootBeanDefinition mbd,
java.lang.Class<?>... typesToMatch) |
protected java.lang.Object |
evaluateBeanDefinitionString(java.lang.String value,
BeanDefinition beanDefinition)
Evaluate the given String as contained in a bean definition,
potentially resolving it as an expression.
|
java.security.AccessControlContext |
getAccessControlContext()
Delegate the creation of the access control context to the
SecurityContextProvider . |
java.lang.String[] |
getAliases(java.lang.String name)
Return the aliases for the given name, if defined.
|
java.lang.Object |
getBean(java.lang.String name)
Return an instance, which may be shared or independent, of the specified bean.
|
<T> T |
getBean(java.lang.String name,
java.lang.Class<T> requiredType)
Return an instance, which may be shared or independent, of the specified bean.
|
<T> T |
getBean(java.lang.String name,
java.lang.Class<T> requiredType,
java.lang.Object... args)
Return an instance, which may be shared or independent, of the specified bean.
|
java.lang.Object |
getBean(java.lang.String name,
java.lang.Object... args)
Return an instance, which may be shared or independent, of the specified bean.
|
java.lang.ClassLoader |
getBeanClassLoader()
Return this factory's class loader for loading bean classes.
|
protected abstract BeanDefinition |
getBeanDefinition(java.lang.String beanName)
Return the bean definition for the given bean name.
|
BeanExpressionResolver |
getBeanExpressionResolver()
Return the resolution strategy for expressions in bean definition values.
|
int |
getBeanPostProcessorCount()
Return the current number of registered BeanPostProcessors, if any.
|
java.util.List<BeanPostProcessor> |
getBeanPostProcessors()
Return the list of BeanPostProcessors that will get applied
to beans created with this factory.
|
ConversionService |
getConversionService()
Return the associated ConversionService, if any.
|
java.util.Map<java.lang.Class<?>,java.lang.Class<? extends java.beans.PropertyEditor>> |
getCustomEditors()
Return the map of custom editors, with Classes as keys and PropertyEditor classes as values.
|
protected TypeConverter |
getCustomTypeConverter()
Return the custom TypeConverter to use, if any.
|
BeanDefinition |
getMergedBeanDefinition(java.lang.String name)
Return a 'merged' BeanDefinition for the given bean name,
merging a child bean definition with its parent if necessary.
|
protected RootBeanDefinition |
getMergedBeanDefinition(java.lang.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(java.lang.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(java.lang.String beanName)
Return a merged RootBeanDefinition, traversing the parent bean definition
if the specified bean corresponds to a child bean definition.
|
protected java.lang.Object |
getObjectForBeanInstance(java.lang.Object beanInstance,
java.lang.String name,
java.lang.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.
|
BeanFactory |
getParentBeanFactory()
Return the parent bean factory, or
null if there is none. |
java.util.Set<PropertyEditorRegistrar> |
getPropertyEditorRegistrars()
Return the set of PropertyEditorRegistrars.
|
Scope |
getRegisteredScope(java.lang.String scopeName)
Return the Scope implementation for the given scope name, if any.
|
java.lang.String[] |
getRegisteredScopeNames()
Return the names of all currently registered scopes.
|
java.lang.ClassLoader |
getTempClassLoader()
Return the temporary ClassLoader to use for type matching purposes,
if any.
|
java.lang.Class<?> |
getType(java.lang.String name)
Determine the type of the bean with the given name.
|
TypeConverter |
getTypeConverter()
Obtain a type converter as used by this BeanFactory.
|
protected java.lang.Class<?> |
getTypeForFactoryBean(java.lang.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.
|
boolean |
isActuallyInCreation(java.lang.String beanName) |
protected boolean |
isBeanEligibleForMetadataCaching(java.lang.String beanName)
Determine whether the specified bean is eligible for having
its bean definition metadata cached.
|
boolean |
isBeanNameInUse(java.lang.String beanName)
Determine whether the given bean name is already in use within this factory,
i.e.
|
boolean |
isCacheBeanMetadata()
Return whether to cache bean metadata such as given bean definitions
(in merged fashion) and resolved bean classes.
|
boolean |
isFactoryBean(java.lang.String name)
Determine whether the bean with the given name is a FactoryBean.
|
protected boolean |
isFactoryBean(java.lang.String beanName,
RootBeanDefinition mbd)
Check whether the given bean is defined as a
FactoryBean . |
boolean |
isPrototype(java.lang.String name)
Is this bean a prototype? That is, will
BeanFactory.getBean(java.lang.String) always return
independent instances? |
protected boolean |
isPrototypeCurrentlyInCreation(java.lang.String beanName)
Return whether the specified prototype bean is currently in creation
(within the current thread).
|
boolean |
isSingleton(java.lang.String name)
Is this bean a shared singleton? That is, will
BeanFactory.getBean(java.lang.String) always
return the same instance? |
boolean |
isTypeMatch(java.lang.String name,
java.lang.Class<?> targetType)
Check whether the bean with the given name matches the specified type.
|
protected void |
markBeanAsCreated(java.lang.String beanName)
Mark the specified bean as already created (or about to be created).
|
protected java.lang.String |
originalBeanName(java.lang.String name)
Determine the original bean name, resolving locally defined aliases to canonical names.
|
protected java.lang.Class<?> |
predictBeanType(java.lang.String beanName,
RootBeanDefinition mbd,
java.lang.Class<?>... typesToMatch)
Predict the eventual bean type (of the processed bean instance) for the
specified bean.
|
void |
registerCustomEditor(java.lang.Class<?> requiredType,
java.lang.Class<? extends java.beans.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(java.lang.String beanName,
java.lang.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(java.lang.String scopeName,
Scope scope)
Register the given scope, backed by the given Scope implementation.
|
protected boolean |
removeSingletonIfCreatedForTypeCheckOnly(java.lang.String beanName)
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(java.lang.Object bean,
RootBeanDefinition mbd)
Determine whether the given bean requires destruction on shutdown.
|
protected java.lang.Class<?> |
resolveBeanClass(RootBeanDefinition mbd,
java.lang.String beanName,
java.lang.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.
|
java.lang.String |
resolveEmbeddedValue(java.lang.String value)
Resolve the given embedded value, e.g.
|
void |
setBeanClassLoader(java.lang.ClassLoader beanClassLoader)
Set the class loader to use for loading bean classes.
|
void |
setBeanExpressionResolver(BeanExpressionResolver resolver)
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 |
setSecurityContextProvider(SecurityContextProvider securityProvider)
Set the security context provider for this bean factory.
|
void |
setTempClassLoader(java.lang.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 java.lang.String |
transformedBeanName(java.lang.String name)
Return the bean name, stripping out the factory dereference prefix if necessary,
and resolving aliases to canonical names.
|
getCachedObjectForFactoryBean, getFactoryBean, getObjectFromFactoryBean, getTypeForFactoryBean, postProcessObjectFromFactoryBean, removeSingleton
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
allowAliasOverriding, canonicalName, checkForAliasCircle, isAlias, registerAlias, removeAlias, resolveAliases
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
destroySingletons, getDependenciesForBean, getDependentBeans, isCurrentlyInCreation, registerAlias, registerDependentBean, resolveAliases, setCurrentlyInCreation
getBean
containsSingleton, getSingleton, getSingletonCount, getSingletonNames, registerSingleton
private BeanFactory parentBeanFactory
private java.lang.ClassLoader beanClassLoader
private java.lang.ClassLoader tempClassLoader
private boolean cacheBeanMetadata
private BeanExpressionResolver beanExpressionResolver
private ConversionService conversionService
private final java.util.Set<PropertyEditorRegistrar> propertyEditorRegistrars
private TypeConverter typeConverter
private final java.util.Map<java.lang.Class<?>,java.lang.Class<? extends java.beans.PropertyEditor>> customEditors
private final java.util.List<StringValueResolver> embeddedValueResolvers
private final java.util.List<BeanPostProcessor> beanPostProcessors
private boolean hasInstantiationAwareBeanPostProcessors
private boolean hasDestructionAwareBeanPostProcessors
private final java.util.Map<java.lang.String,Scope> scopes
private SecurityContextProvider securityContextProvider
private final java.util.Map<java.lang.String,RootBeanDefinition> mergedBeanDefinitions
private final java.util.Set<java.lang.String> alreadyCreated
private final java.lang.ThreadLocal<java.lang.Object> prototypesCurrentlyInCreation
public AbstractBeanFactory()
public AbstractBeanFactory(BeanFactory parentBeanFactory)
parentBeanFactory
- parent bean factory, or null
if nonegetBean(java.lang.String)
public java.lang.Object getBean(java.lang.String name) throws BeansException
BeanFactory
This method allows a Spring BeanFactory to be used as a replacement for the Singleton or Prototype design pattern. Callers may retain references to returned objects in the case of Singleton beans.
Translates aliases back to the corresponding canonical bean name. Will ask the parent factory if the bean cannot be found in this factory instance.
getBean
in interface BeanFactory
name
- the name of the bean to retrieveNoSuchBeanDefinitionException
- if there is no bean definition
with the specified nameBeansException
- if the bean could not be obtainedpublic <T> T getBean(java.lang.String name, java.lang.Class<T> requiredType) throws BeansException
BeanFactory
Behaves the same as BeanFactory.getBean(String)
, but provides a measure of type
safety by throwing a BeanNotOfRequiredTypeException if the bean is not of the
required type. This means that ClassCastException can't be thrown on casting
the result correctly, as can happen with BeanFactory.getBean(String)
.
Translates aliases back to the corresponding canonical bean name. Will ask the parent factory if the bean cannot be found in this factory instance.
getBean
in interface BeanFactory
name
- the name of the bean to retrieverequiredType
- type the bean must match. Can be an interface or superclass
of the actual class, or null
for any match. For example, if the value
is Object.class
, this method will succeed whatever the class of the
returned instance.NoSuchBeanDefinitionException
- if there is no such bean definitionBeanNotOfRequiredTypeException
- if the bean is not of the required typeBeansException
- if the bean could not be createdpublic java.lang.Object getBean(java.lang.String name, java.lang.Object... args) throws BeansException
BeanFactory
Allows for specifying explicit constructor arguments / factory method arguments, overriding the specified default arguments (if any) in the bean definition.
getBean
in interface BeanFactory
name
- the name of the bean to retrieveargs
- arguments to use if creating a prototype using explicit arguments to a
static factory method. It is invalid to use a non-null args value in any other case.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 createdpublic <T> T getBean(java.lang.String name, java.lang.Class<T> requiredType, java.lang.Object... args) throws BeansException
name
- the name of the bean to retrieverequiredType
- the required type of the bean to retrieveargs
- arguments to use if creating a prototype using explicit arguments to a
static factory method. It is invalid to use a non-null args value in any other case.BeansException
- if the bean could not be createdprotected <T> T doGetBean(java.lang.String name, java.lang.Class<T> requiredType, java.lang.Object[] args, boolean typeCheckOnly) throws BeansException
name
- the name of the bean to retrieverequiredType
- the required type of the bean to retrieveargs
- arguments to use if creating a prototype using explicit arguments to a
static factory method. It is invalid to use a non-null args value in any other case.typeCheckOnly
- whether the instance is obtained for a type check,
not for actual useBeansException
- if the bean could not be createdpublic boolean containsBean(java.lang.String name)
BeanFactory
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 a true
return value from this method does not necessarily indicate that BeanFactory.getBean(java.lang.String)
will be able to obtain an instance for the same name.
containsBean
in interface BeanFactory
name
- the name of the bean to querypublic boolean isSingleton(java.lang.String name) throws NoSuchBeanDefinitionException
BeanFactory
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.
isSingleton
in interface BeanFactory
name
- the name of the bean to queryNoSuchBeanDefinitionException
- if there is no bean with the given nameBeanFactory.getBean(java.lang.String)
,
BeanFactory.isPrototype(java.lang.String)
public boolean isPrototype(java.lang.String name) throws NoSuchBeanDefinitionException
BeanFactory
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.
isPrototype
in interface BeanFactory
name
- the name of the bean to queryNoSuchBeanDefinitionException
- if there is no bean with the given nameBeanFactory.getBean(java.lang.String)
,
BeanFactory.isSingleton(java.lang.String)
public boolean isTypeMatch(java.lang.String name, java.lang.Class<?> targetType) throws NoSuchBeanDefinitionException
BeanFactory
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.
isTypeMatch
in interface BeanFactory
name
- the name of the bean to querytargetType
- the type to match againsttrue
if the bean type matches,
false
if it doesn't match or cannot be determined yetNoSuchBeanDefinitionException
- if there is no bean with the given nameBeanFactory.getBean(java.lang.String)
,
BeanFactory.getType(java.lang.String)
public java.lang.Class<?> getType(java.lang.String name) throws NoSuchBeanDefinitionException
BeanFactory
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.
getType
in interface BeanFactory
name
- the name of the bean to querynull
if not determinableNoSuchBeanDefinitionException
- if there is no bean with the given nameBeanFactory.getBean(java.lang.String)
,
BeanFactory.isTypeMatch(java.lang.String, java.lang.Class<?>)
public java.lang.String[] getAliases(java.lang.String name)
AliasRegistry
getAliases
in interface BeanFactory
getAliases
in interface AliasRegistry
getAliases
in class SimpleAliasRegistry
name
- the name to check for aliasesBeanFactory.getBean(java.lang.String)
public BeanFactory getParentBeanFactory()
HierarchicalBeanFactory
null
if there is none.getParentBeanFactory
in interface HierarchicalBeanFactory
public boolean containsLocalBean(java.lang.String name)
HierarchicalBeanFactory
This is an alternative to containsBean
, ignoring a bean
of the given name from an ancestor bean factory.
containsLocalBean
in interface HierarchicalBeanFactory
name
- the name of the bean to queryBeanFactory.containsBean(java.lang.String)
public void setParentBeanFactory(BeanFactory parentBeanFactory)
ConfigurableBeanFactory
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.
setParentBeanFactory
in interface ConfigurableBeanFactory
parentBeanFactory
- the parent BeanFactoryHierarchicalBeanFactory.getParentBeanFactory()
public void setBeanClassLoader(java.lang.ClassLoader beanClassLoader)
ConfigurableBeanFactory
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.
setBeanClassLoader
in interface ConfigurableBeanFactory
beanClassLoader
- the class loader to use,
or null
to suggest the default class loaderpublic java.lang.ClassLoader getBeanClassLoader()
ConfigurableBeanFactory
getBeanClassLoader
in interface ConfigurableBeanFactory
public void setTempClassLoader(java.lang.ClassLoader tempClassLoader)
ConfigurableBeanFactory
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.
setTempClassLoader
in interface ConfigurableBeanFactory
public java.lang.ClassLoader getTempClassLoader()
ConfigurableBeanFactory
getTempClassLoader
in interface ConfigurableBeanFactory
public void setCacheBeanMetadata(boolean cacheBeanMetadata)
ConfigurableBeanFactory
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.
setCacheBeanMetadata
in interface ConfigurableBeanFactory
public boolean isCacheBeanMetadata()
ConfigurableBeanFactory
isCacheBeanMetadata
in interface ConfigurableBeanFactory
public void setBeanExpressionResolver(BeanExpressionResolver resolver)
ConfigurableBeanFactory
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.
setBeanExpressionResolver
in interface ConfigurableBeanFactory
public BeanExpressionResolver getBeanExpressionResolver()
ConfigurableBeanFactory
getBeanExpressionResolver
in interface ConfigurableBeanFactory
public void setConversionService(ConversionService conversionService)
ConfigurableBeanFactory
setConversionService
in interface ConfigurableBeanFactory
public ConversionService getConversionService()
ConfigurableBeanFactory
getConversionService
in interface ConfigurableBeanFactory
public void addPropertyEditorRegistrar(PropertyEditorRegistrar registrar)
ConfigurableBeanFactory
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>)
.
addPropertyEditorRegistrar
in interface ConfigurableBeanFactory
registrar
- the PropertyEditorRegistrar to registerpublic java.util.Set<PropertyEditorRegistrar> getPropertyEditorRegistrars()
public void registerCustomEditor(java.lang.Class<?> requiredType, java.lang.Class<? extends java.beans.PropertyEditor> propertyEditorClass)
ConfigurableBeanFactory
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.
registerCustomEditor
in interface ConfigurableBeanFactory
requiredType
- type of the propertypropertyEditorClass
- the PropertyEditor
class to registerpublic void copyRegisteredEditorsTo(PropertyEditorRegistry registry)
ConfigurableBeanFactory
copyRegisteredEditorsTo
in interface ConfigurableBeanFactory
registry
- the PropertyEditorRegistry to initializepublic java.util.Map<java.lang.Class<?>,java.lang.Class<? extends java.beans.PropertyEditor>> getCustomEditors()
public void setTypeConverter(TypeConverter typeConverter)
ConfigurableBeanFactory
This will override the default PropertyEditor mechanism and hence make any custom editors or custom editor registrars irrelevant.
protected TypeConverter getCustomTypeConverter()
null
if none specifiedpublic TypeConverter getTypeConverter()
ConfigurableBeanFactory
If the default PropertyEditor mechanism is active, the returned TypeConverter will be aware of all custom editors that have been registered.
getTypeConverter
in interface ConfigurableBeanFactory
public void addEmbeddedValueResolver(StringValueResolver valueResolver)
ConfigurableBeanFactory
addEmbeddedValueResolver
in interface ConfigurableBeanFactory
valueResolver
- the String resolver to apply to embedded valuespublic java.lang.String resolveEmbeddedValue(java.lang.String value)
ConfigurableBeanFactory
resolveEmbeddedValue
in interface ConfigurableBeanFactory
value
- the value to resolvepublic void addBeanPostProcessor(BeanPostProcessor beanPostProcessor)
ConfigurableBeanFactory
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.
addBeanPostProcessor
in interface ConfigurableBeanFactory
beanPostProcessor
- the post-processor to registerpublic int getBeanPostProcessorCount()
ConfigurableBeanFactory
getBeanPostProcessorCount
in interface ConfigurableBeanFactory
public java.util.List<BeanPostProcessor> getBeanPostProcessors()
protected boolean hasInstantiationAwareBeanPostProcessors()
protected boolean hasDestructionAwareBeanPostProcessors()
public void registerScope(java.lang.String scopeName, Scope scope)
ConfigurableBeanFactory
registerScope
in interface ConfigurableBeanFactory
scopeName
- the scope identifierscope
- the backing Scope implementationpublic java.lang.String[] getRegisteredScopeNames()
ConfigurableBeanFactory
This will only return the names of explicitly registered scopes. Built-in scopes such as "singleton" and "prototype" won't be exposed.
getRegisteredScopeNames
in interface ConfigurableBeanFactory
ConfigurableBeanFactory.registerScope(java.lang.String, org.springframework.beans.factory.config.Scope)
public Scope getRegisteredScope(java.lang.String scopeName)
ConfigurableBeanFactory
This will only return explicitly registered scopes. Built-in scopes such as "singleton" and "prototype" won't be exposed.
getRegisteredScope
in interface ConfigurableBeanFactory
scopeName
- the name of the scopenull
if noneConfigurableBeanFactory.registerScope(java.lang.String, org.springframework.beans.factory.config.Scope)
public void setSecurityContextProvider(SecurityContextProvider securityProvider)
public java.security.AccessControlContext getAccessControlContext()
SecurityContextProvider
.getAccessControlContext
in interface ConfigurableBeanFactory
getAccessControlContext
in class FactoryBeanRegistrySupport
null
)AccessController.getContext()
public void copyConfigurationFrom(ConfigurableBeanFactory otherFactory)
ConfigurableBeanFactory
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.
copyConfigurationFrom
in interface ConfigurableBeanFactory
otherFactory
- the other BeanFactory to copy frompublic BeanDefinition getMergedBeanDefinition(java.lang.String name) throws BeansException
This getMergedBeanDefinition
considers bean definition
in ancestors as well.
getMergedBeanDefinition
in interface ConfigurableBeanFactory
name
- the name of the bean to retrieve the merged definition for
(may be an alias)NoSuchBeanDefinitionException
- if there is no bean with the given nameBeanDefinitionStoreException
- in case of an invalid bean definitionBeansException
public boolean isFactoryBean(java.lang.String name) throws NoSuchBeanDefinitionException
ConfigurableBeanFactory
isFactoryBean
in interface ConfigurableBeanFactory
name
- the name of the bean to checkfalse
means the bean exists but is not a FactoryBean)NoSuchBeanDefinitionException
- if there is no bean with the given namepublic boolean isActuallyInCreation(java.lang.String beanName)
isActuallyInCreation
in class DefaultSingletonBeanRegistry
protected boolean isPrototypeCurrentlyInCreation(java.lang.String beanName)
beanName
- the name of the beanprotected void beforePrototypeCreation(java.lang.String beanName)
The default implementation register the prototype as currently in creation.
beanName
- the name of the prototype about to be createdisPrototypeCurrentlyInCreation(java.lang.String)
protected void afterPrototypeCreation(java.lang.String beanName)
The default implementation marks the prototype as not in creation anymore.
beanName
- the name of the prototype that has been createdisPrototypeCurrentlyInCreation(java.lang.String)
public void destroyBean(java.lang.String beanName, java.lang.Object beanInstance)
ConfigurableBeanFactory
Any exception that arises during destruction should be caught and logged instead of propagated to the caller of this method.
destroyBean
in interface ConfigurableBeanFactory
beanName
- the name of the bean definitionbeanInstance
- the bean instance to destroyprotected void destroyBean(java.lang.String beanName, java.lang.Object beanInstance, RootBeanDefinition mbd)
beanName
- the name of the bean definitionbeanInstance
- the bean instance to destroymbd
- the merged bean definitionpublic void destroyScopedBean(java.lang.String beanName)
ConfigurableBeanFactory
Any exception that arises during destruction should be caught and logged instead of propagated to the caller of this method.
destroyScopedBean
in interface ConfigurableBeanFactory
beanName
- the name of the scoped beanprotected java.lang.String transformedBeanName(java.lang.String name)
name
- the user-specified nameprotected java.lang.String originalBeanName(java.lang.String name)
name
- the user-specified nameprotected void initBeanWrapper(BeanWrapper bw)
The default implementation delegates to registerCustomEditors(org.springframework.beans.PropertyEditorRegistry)
.
Can be overridden in subclasses.
bw
- the BeanWrapper to initializeprotected void registerCustomEditors(PropertyEditorRegistry registry)
To be called for BeanWrappers that will create and populate bean instances, and for SimpleTypeConverter used for constructor argument and factory method type conversion.
registry
- the PropertyEditorRegistry to initializeprotected RootBeanDefinition getMergedLocalBeanDefinition(java.lang.String beanName) throws BeansException
beanName
- the name of the bean to retrieve the merged definition forNoSuchBeanDefinitionException
- if there is no bean with the given nameBeanDefinitionStoreException
- in case of an invalid bean definitionBeansException
protected RootBeanDefinition getMergedBeanDefinition(java.lang.String beanName, BeanDefinition bd) throws BeanDefinitionStoreException
beanName
- the name of the bean definitionbd
- the original bean definition (Root/ChildBeanDefinition)BeanDefinitionStoreException
- in case of an invalid bean definitionprotected RootBeanDefinition getMergedBeanDefinition(java.lang.String beanName, BeanDefinition bd, BeanDefinition containingBd) throws BeanDefinitionStoreException
beanName
- the name of the bean definitionbd
- the original bean definition (Root/ChildBeanDefinition)containingBd
- the containing bean definition in case of inner bean,
or null
in case of a top-level beanBeanDefinitionStoreException
- in case of an invalid bean definitionprotected void checkMergedBeanDefinition(RootBeanDefinition mbd, java.lang.String beanName, java.lang.Object[] args) throws BeanDefinitionStoreException
mbd
- the merged bean definition to checkbeanName
- the name of the beanargs
- the arguments for bean creation, if anyBeanDefinitionStoreException
- in case of validation failureprotected void clearMergedBeanDefinition(java.lang.String beanName)
beanName
- the bean name to clear the merged definition forprotected java.lang.Class<?> resolveBeanClass(RootBeanDefinition mbd, java.lang.String beanName, java.lang.Class<?>... typesToMatch) throws CannotLoadBeanClassException
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 returned Class
will never be exposed to application code)null
if none)CannotLoadBeanClassException
- if we failed to load the classprivate java.lang.Class<?> doResolveBeanClass(RootBeanDefinition mbd, java.lang.Class<?>... typesToMatch) throws java.lang.ClassNotFoundException
java.lang.ClassNotFoundException
protected java.lang.Object evaluateBeanDefinitionString(java.lang.String value, BeanDefinition beanDefinition)
value
- the value to checkbeanDefinition
- the bean definition that the value comes fromsetBeanExpressionResolver(org.springframework.beans.factory.config.BeanExpressionResolver)
protected java.lang.Class<?> predictBeanType(java.lang.String beanName, RootBeanDefinition mbd, java.lang.Class<?>... typesToMatch)
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.
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 returned Class
will never be exposed to application code)null
if not predictableprotected boolean isFactoryBean(java.lang.String beanName, RootBeanDefinition mbd)
FactoryBean
.beanName
- the name of the beanmbd
- the corresponding bean definitionprotected java.lang.Class<?> getTypeForFactoryBean(java.lang.String beanName, RootBeanDefinition mbd)
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.
beanName
- the name of the beanmbd
- the merged bean definition for the beannull
elseFactoryBean.getObjectType()
,
getBean(String)
protected void markBeanAsCreated(java.lang.String beanName)
This allows the bean factory to optimize its caching for repeated creation of the specified bean.
beanName
- the name of the beanprotected void cleanupAfterBeanCreationFailure(java.lang.String beanName)
beanName
- the name of the beanprotected boolean isBeanEligibleForMetadataCaching(java.lang.String beanName)
beanName
- the name of the beantrue
if the bean's metadata may be cached
at this point alreadyprotected boolean removeSingletonIfCreatedForTypeCheckOnly(java.lang.String beanName)
beanName
- the name of the beantrue
if actually removed, false
otherwiseprotected java.lang.Object getObjectForBeanInstance(java.lang.Object beanInstance, java.lang.String name, java.lang.String beanName, RootBeanDefinition mbd)
beanInstance
- the shared bean instancename
- name that may include factory dereference prefixbeanName
- the canonical bean namembd
- the merged bean definitionpublic boolean isBeanNameInUse(java.lang.String beanName)
beanName
- the name to checkprotected boolean requiresDestruction(java.lang.Object bean, RootBeanDefinition mbd)
The default implementation checks the DisposableBean interface as well as a specified destroy method and registered DestructionAwareBeanPostProcessors.
bean
- the bean instance to checkmbd
- the corresponding bean definitionDisposableBean
,
AbstractBeanDefinition.getDestroyMethodName()
,
DestructionAwareBeanPostProcessor
protected void registerDisposableBeanIfNecessary(java.lang.String beanName, java.lang.Object bean, RootBeanDefinition mbd)
beanName
- the name of the beanbean
- the bean instancembd
- the bean definition for the beanAbstractBeanDefinition.isSingleton()
,
AbstractBeanDefinition.getDependsOn()
,
DefaultSingletonBeanRegistry.registerDisposableBean(java.lang.String, org.springframework.beans.factory.DisposableBean)
,
DefaultSingletonBeanRegistry.registerDependentBean(java.lang.String, java.lang.String)
protected abstract boolean containsBeanDefinition(java.lang.String beanName)
containsBean
when no cached singleton instance is found.
Depending on the nature of the concrete bean factory implementation, this operation might be expensive (for example, because of directory lookups in external registries). However, for listable bean factories, this usually just amounts to a local hash lookup: The operation is therefore part of the public interface there. The same implementation can serve for both this template method and the public interface method in that case.
beanName
- the name of the bean to look forcontainsBean(java.lang.String)
,
ListableBeanFactory.containsBeanDefinition(java.lang.String)
protected abstract BeanDefinition getBeanDefinition(java.lang.String beanName) throws BeansException
Depending on the nature of the concrete bean factory implementation, this operation might be expensive (for example, because of directory lookups in external registries). However, for listable bean factories, this usually just amounts to a local hash lookup: The operation is therefore part of the public interface there. The same implementation can serve for both this template method and the public interface method in that case.
beanName
- the name of the bean to find a definition fornull
)NoSuchBeanDefinitionException
- if the bean definition cannot be resolvedBeansException
- in case of errorsRootBeanDefinition
,
ChildBeanDefinition
,
ConfigurableListableBeanFactory.getBeanDefinition(java.lang.String)
protected abstract java.lang.Object createBean(java.lang.String beanName, RootBeanDefinition mbd, java.lang.Object[] args) throws BeanCreationException
All the other methods in this class invoke this method, although beans may be cached after being instantiated by this method. All bean instantiation within this class is performed by this method.
beanName
- the name of the beanmbd
- the merged bean definition for the beanargs
- arguments to use if creating a prototype using explicit arguments to a
static factory method. This parameter must be null
except in this case.BeanCreationException
- if the bean could not be created