public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements SingletonBeanRegistry
SingletonBeanRegistry
.
Allows for registering singleton instances that should be shared
for all callers of the registry, to be obtained via bean name.
Also supports registration of
DisposableBean
instances,
(which might or might not correspond to registered singletons),
to be destroyed on shutdown of the registry. Dependencies between
beans can be registered to enforce an appropriate shutdown order.
This class mainly serves as base class for
BeanFactory
implementations,
factoring out the common management of singleton bean instances. Note that
the ConfigurableBeanFactory
interface extends the SingletonBeanRegistry
interface.
Note that this class assumes neither a bean definition concept
nor a specific creation process for bean instances, in contrast to
AbstractBeanFactory
and DefaultListableBeanFactory
(which inherit from it). Can alternatively also be used as a nested
helper to delegate to.
registerSingleton(java.lang.String, java.lang.Object)
,
registerDisposableBean(java.lang.String, org.springframework.beans.factory.DisposableBean)
,
DisposableBean
,
ConfigurableBeanFactory
logger
Constructor and Description |
---|
DefaultSingletonBeanRegistry() |
Modifier and Type | Method and Description |
---|---|
protected void |
addSingleton(String beanName,
Object singletonObject)
Add the given singleton object to the singleton cache of this factory.
|
protected void |
addSingletonFactory(String beanName,
ObjectFactory<?> singletonFactory)
Add the given singleton factory for building the specified singleton
if necessary.
|
protected void |
afterSingletonCreation(String beanName)
Callback after singleton creation.
|
protected void |
beforeSingletonCreation(String beanName)
Callback before singleton creation.
|
protected void |
clearSingletonCache()
Clear all cached singleton instances in this registry.
|
boolean |
containsSingleton(String beanName)
Check if this registry contains a singleton instance with the given name.
|
protected void |
destroyBean(String beanName,
DisposableBean bean)
Destroy the given bean.
|
void |
destroySingleton(String beanName)
Destroy the given bean.
|
void |
destroySingletons() |
String[] |
getDependenciesForBean(String beanName)
Return the names of all beans that the specified bean depends on, if any.
|
String[] |
getDependentBeans(String beanName)
Return the names of all beans which depend on the specified bean, if any.
|
Object |
getSingleton(String beanName)
Return the (raw) singleton object registered under the given name.
|
protected Object |
getSingleton(String beanName,
boolean allowEarlyReference)
Return the (raw) singleton object registered under the given name.
|
Object |
getSingleton(String beanName,
ObjectFactory<?> singletonFactory)
Return the (raw) singleton object registered under the given name,
creating and registering a new one if none registered yet.
|
int |
getSingletonCount()
Return the number of singleton beans registered in this registry.
|
Object |
getSingletonMutex()
Exposes the singleton mutex to subclasses and external collaborators.
|
String[] |
getSingletonNames()
Return the names of singleton beans registered in this registry.
|
protected boolean |
hasDependentBean(String beanName)
Determine whether a dependent bean has been registered for the given name.
|
protected boolean |
isActuallyInCreation(String beanName) |
boolean |
isCurrentlyInCreation(String beanName) |
protected boolean |
isDependent(String beanName,
String dependentBeanName)
Determine whether the specified dependent bean has been registered as
dependent on the given bean or on any of its transitive dependencies.
|
boolean |
isSingletonCurrentlyInCreation(String beanName)
Return whether the specified singleton bean is currently in creation
(within the entire factory).
|
protected void |
onSuppressedException(Exception ex)
Register an exception that happened to get suppressed during the creation of a
singleton bean instance, e.g.
|
void |
registerContainedBean(String containedBeanName,
String containingBeanName)
Register a containment relationship between two beans,
e.g.
|
void |
registerDependentBean(String beanName,
String dependentBeanName)
Register a dependent bean for the given bean,
to be destroyed before the given bean is destroyed.
|
void |
registerDisposableBean(String beanName,
DisposableBean bean)
Add the given bean to the list of disposable beans in this registry.
|
void |
registerSingleton(String beanName,
Object singletonObject)
Register the given existing object as singleton in the bean registry,
under the given bean name.
|
protected void |
removeSingleton(String beanName)
Remove the bean with the given name from the singleton cache of this factory,
to be able to clean up eager registration of a singleton if creation failed.
|
void |
setCurrentlyInCreation(String beanName,
boolean inCreation) |
allowAliasOverriding, canonicalName, checkForAliasCircle, getAliases, hasAlias, isAlias, registerAlias, removeAlias, resolveAliases
public void registerSingleton(String beanName, Object singletonObject) throws IllegalStateException
SingletonBeanRegistry
The given instance is supposed to be fully initialized; the registry
will not perform any initialization callbacks (in particular, it won't
call InitializingBean's afterPropertiesSet
method).
The given instance will not receive any destruction callbacks
(like DisposableBean's destroy
method) either.
When running within a full BeanFactory: Register a bean definition instead of an existing instance if your bean is supposed to receive initialization and/or destruction callbacks.
Typically invoked during registry configuration, but can also be used for runtime registration of singletons. As a consequence, a registry implementation should synchronize singleton access; it will have to do this anyway if it supports a BeanFactory's lazy initialization of singletons.
registerSingleton
in interface SingletonBeanRegistry
beanName
- the name of the beansingletonObject
- the existing singleton objectIllegalStateException
InitializingBean.afterPropertiesSet()
,
DisposableBean.destroy()
,
BeanDefinitionRegistry.registerBeanDefinition(java.lang.String, org.springframework.beans.factory.config.BeanDefinition)
protected void addSingleton(String beanName, Object singletonObject)
To be called for eager registration of singletons.
beanName
- the name of the beansingletonObject
- the singleton objectprotected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory)
To be called for eager registration of singletons, e.g. to be able to resolve circular references.
beanName
- the name of the beansingletonFactory
- the factory for the singleton object@Nullable public Object getSingleton(String beanName)
SingletonBeanRegistry
Only checks already instantiated singletons; does not return an Object for singleton bean definitions which have not been instantiated yet.
The main purpose of this method is to access manually registered singletons
(see SingletonBeanRegistry.registerSingleton(java.lang.String, java.lang.Object)
). Can also be used to access a singleton
defined by a bean definition that already been created, in a raw fashion.
NOTE: This lookup method is not aware of FactoryBean prefixes or aliases. You need to resolve the canonical bean name first before obtaining the singleton instance.
getSingleton
in interface SingletonBeanRegistry
beanName
- the name of the bean to look fornull
if none foundConfigurableListableBeanFactory.getBeanDefinition(java.lang.String)
@Nullable protected Object getSingleton(String beanName, boolean allowEarlyReference)
Checks already instantiated singletons and also allows for an early reference to a currently created singleton (resolving a circular reference).
beanName
- the name of the bean to look forallowEarlyReference
- whether early references should be created or notnull
if none foundpublic Object getSingleton(String beanName, ObjectFactory<?> singletonFactory)
beanName
- the name of the beansingletonFactory
- the ObjectFactory to lazily create the singleton
with, if necessaryprotected void onSuppressedException(Exception ex)
The default implementation preserves any given exception in this registry's
collection of suppressed exceptions, up to a limit of 100 exceptions, adding
them as related causes to an eventual top-level BeanCreationException
.
ex
- the Exception to registerBeanCreationException.getRelatedCauses()
protected void removeSingleton(String beanName)
beanName
- the name of the beangetSingletonMutex()
public boolean containsSingleton(String beanName)
SingletonBeanRegistry
Only checks already instantiated singletons; does not return true
for singleton bean definitions which have not been instantiated yet.
The main purpose of this method is to check manually registered singletons
(see SingletonBeanRegistry.registerSingleton(java.lang.String, java.lang.Object)
). Can also be used to check whether a
singleton defined by a bean definition has already been created.
To check whether a bean factory contains a bean definition with a given name,
use ListableBeanFactory's containsBeanDefinition
. Calling both
containsBeanDefinition
and containsSingleton
answers
whether a specific bean factory contains a local bean instance with the given name.
Use BeanFactory's containsBean
for general checks whether the
factory knows about a bean with a given name (whether manually registered singleton
instance or created by bean definition), also checking ancestor factories.
NOTE: This lookup method is not aware of FactoryBean prefixes or aliases. You need to resolve the canonical bean name first before checking the singleton status.
containsSingleton
in interface SingletonBeanRegistry
beanName
- the name of the bean to look forSingletonBeanRegistry.registerSingleton(java.lang.String, java.lang.Object)
,
ListableBeanFactory.containsBeanDefinition(java.lang.String)
,
BeanFactory.containsBean(java.lang.String)
public String[] getSingletonNames()
SingletonBeanRegistry
Only checks already instantiated singletons; does not return names for singleton bean definitions which have not been instantiated yet.
The main purpose of this method is to check manually registered singletons
(see SingletonBeanRegistry.registerSingleton(java.lang.String, java.lang.Object)
). Can also be used to check which singletons
defined by a bean definition have already been created.
getSingletonNames
in interface SingletonBeanRegistry
null
)SingletonBeanRegistry.registerSingleton(java.lang.String, java.lang.Object)
,
BeanDefinitionRegistry.getBeanDefinitionNames()
,
ListableBeanFactory.getBeanDefinitionNames()
public int getSingletonCount()
SingletonBeanRegistry
Only checks already instantiated singletons; does not count singleton bean definitions which have not been instantiated yet.
The main purpose of this method is to check manually registered singletons
(see SingletonBeanRegistry.registerSingleton(java.lang.String, java.lang.Object)
). Can also be used to count the number of
singletons defined by a bean definition that have already been created.
getSingletonCount
in interface SingletonBeanRegistry
SingletonBeanRegistry.registerSingleton(java.lang.String, java.lang.Object)
,
BeanDefinitionRegistry.getBeanDefinitionCount()
,
ListableBeanFactory.getBeanDefinitionCount()
public void setCurrentlyInCreation(String beanName, boolean inCreation)
public boolean isCurrentlyInCreation(String beanName)
protected boolean isActuallyInCreation(String beanName)
public boolean isSingletonCurrentlyInCreation(String beanName)
beanName
- the name of the beanprotected void beforeSingletonCreation(String beanName)
The default implementation register the singleton as currently in creation.
beanName
- the name of the singleton about to be createdisSingletonCurrentlyInCreation(java.lang.String)
protected void afterSingletonCreation(String beanName)
The default implementation marks the singleton as not in creation anymore.
beanName
- the name of the singleton that has been createdisSingletonCurrentlyInCreation(java.lang.String)
public void registerDisposableBean(String beanName, DisposableBean bean)
Disposable beans usually correspond to registered singletons, matching the bean name but potentially being a different instance (for example, a DisposableBean adapter for a singleton that does not naturally implement Spring's DisposableBean interface).
beanName
- the name of the beanbean
- the bean instancepublic void registerContainedBean(String containedBeanName, String containingBeanName)
Also registers the containing bean as dependent on the contained bean in terms of destruction order.
containedBeanName
- the name of the contained (inner) beancontainingBeanName
- the name of the containing (outer) beanregisterDependentBean(java.lang.String, java.lang.String)
public void registerDependentBean(String beanName, String dependentBeanName)
beanName
- the name of the beandependentBeanName
- the name of the dependent beanprotected boolean isDependent(String beanName, String dependentBeanName)
beanName
- the name of the bean to checkdependentBeanName
- the name of the dependent beanprotected boolean hasDependentBean(String beanName)
beanName
- the name of the bean to checkpublic String[] getDependentBeans(String beanName)
beanName
- the name of the beanpublic String[] getDependenciesForBean(String beanName)
beanName
- the name of the beanpublic void destroySingletons()
protected void clearSingletonCache()
public void destroySingleton(String beanName)
destroyBean
if a corresponding disposable bean instance is found.beanName
- the name of the beandestroyBean(java.lang.String, org.springframework.beans.factory.DisposableBean)
protected void destroyBean(String beanName, @Nullable DisposableBean bean)
beanName
- the name of the beanbean
- the bean instance to destroypublic final Object getSingletonMutex()
Subclasses should synchronize on the given Object if they perform any sort of extended singleton creation phase. In particular, subclasses should not have their own mutexes involved in singleton creation, to avoid the potential for deadlocks in lazy-init situations.
getSingletonMutex
in interface SingletonBeanRegistry
null
)