java.lang.Object | ||
↳ | org.springframework.core.SimpleAliasRegistry | |
↳ | org.springframework.beans.factory.support.DefaultSingletonBeanRegistry |
Known Direct Subclasses |
Known Indirect Subclasses |
Generic registry for shared bean instances, implementing the
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.
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
NULL_OBJECT | Internal marker for a null singleton object: used as marker value for concurrent Maps (which don't support null values). | ||||||||||
logger | Logger available to subclasses |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Check if this registry contains a singleton instance with the given name.
| |||||||||||
Destroy the given bean.
| |||||||||||
Return the names of all beans that the specified bean depends on, if any.
| |||||||||||
Return the names of all beans which depend on the specified bean, if any.
| |||||||||||
Return the (raw) singleton object registered under the given name,
creating and registering a new one if none registered yet.
| |||||||||||
Return the (raw) singleton object registered under the given name.
| |||||||||||
Return the number of singleton beans registered in this registry.
| |||||||||||
Return the names of singleton beans registered in this registry.
| |||||||||||
Return whether the specified singleton bean is currently in creation
(within the entire factory).
| |||||||||||
Register a containment relationship between two beans,
e.g.
| |||||||||||
Register a dependent bean for the given bean,
to be destroyed before the given bean is destroyed.
| |||||||||||
Add the given bean to the list of disposable beans in this registry.
| |||||||||||
Register the given existing object as singleton in the bean registry,
under the given bean name.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Add the given singleton object to the singleton cache of this factory.
| |||||||||||
Add the given singleton factory for building the specified singleton
if necessary.
| |||||||||||
Callback after singleton creation.
| |||||||||||
Callback before singleton creation.
| |||||||||||
Destroy the given bean.
| |||||||||||
Return the (raw) singleton object registered under the given name.
| |||||||||||
Expose the singleton mutex to subclasses.
| |||||||||||
Determine whether a dependent bean has been registered for the given name.
| |||||||||||
Register an Exception that happened to get suppressed during the creation of a
singleton bean instance, e.g.
| |||||||||||
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.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
org.springframework.core.SimpleAliasRegistry
| |||||||||||
From class
java.lang.Object
| |||||||||||
From interface
org.springframework.beans.factory.config.SingletonBeanRegistry
| |||||||||||
From interface
org.springframework.core.AliasRegistry
|
Internal marker for a null singleton object: used as marker value for concurrent Maps (which don't support null values).
Check if this registry contains a singleton instance with the given name.
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 registerSingleton(String, 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.
beanName | the name of the bean to look for |
---|
Destroy the given bean. Delegates to destroyBean
if a corresponding disposable bean instance is found.
beanName | the name of the bean |
---|
Return the names of all beans that the specified bean depends on, if any.
beanName | the name of the bean |
---|
Return the names of all beans which depend on the specified bean, if any.
beanName | the name of the bean |
---|
Return the (raw) singleton object registered under the given name, creating and registering a new one if none registered yet.
beanName | the name of the bean |
---|---|
singletonFactory | the ObjectFactory to lazily create the singleton with, if necessary |
Return the (raw) singleton object registered under the given name.
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 registerSingleton(String, 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.
beanName | the name of the bean to look for |
---|
null
if none foundReturn the number of singleton beans registered in this registry.
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 registerSingleton(String, Object)
). Can also be used to count the number of
singletons defined by a bean definition that have already been created.
Return the names of singleton beans registered in this registry.
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 registerSingleton(String, Object)
). Can also be used to check which singletons
defined by a bean definition have already been created.
null
)Return whether the specified singleton bean is currently in creation (within the entire factory).
beanName | the name of the bean |
---|
Register a containment relationship between two beans, e.g. between an inner bean and its containing outer bean.
Also registers the containing bean as dependent on the contained bean in terms of destruction order.
containedBeanName | the name of the contained (inner) bean |
---|---|
containingBeanName | the name of the containing (outer) bean |
Register a dependent bean for the given bean, to be destroyed before the given bean is destroyed.
beanName | the name of the bean |
---|---|
dependentBeanName | the name of the dependent bean |
Add the given bean to the list of disposable beans in this registry. 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 bean |
---|---|
bean | the bean instance |
Register the given existing object as singleton in the bean registry, under the given bean name.
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.
beanName | the name of the bean |
---|---|
singletonObject | the existing singleton object |
IllegalStateException |
---|
Add the given singleton object to the singleton cache of this factory.
To be called for eager registration of singletons.
beanName | the name of the bean |
---|---|
singletonObject | the singleton object |
Add the given singleton factory for building the specified singleton if necessary.
To be called for eager registration of singletons, e.g. to be able to resolve circular references.
beanName | the name of the bean |
---|---|
singletonFactory | the factory for the singleton object |
Callback after singleton creation.
Default implementation marks the singleton as not in creation anymore.
beanName | the name of the singleton that has been created |
---|
Callback before singleton creation.
Default implementation register the singleton as currently in creation.
beanName | the name of the singleton about to be created |
---|
Destroy the given bean. Must destroy beans that depend on the given bean before the bean itself. Should not throw any exceptions.
beanName | the name of the bean |
---|---|
bean | the bean instance to destroy |
Return the (raw) singleton object registered under the given name.
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 for |
---|---|
allowEarlyReference | whether early references should be created or not |
null
if none found
Expose the singleton mutex to subclasses.
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.
Determine whether a dependent bean has been registered for the given name.
beanName | the name of the bean to check |
---|
Register an Exception that happened to get suppressed during the creation of a singleton bean instance, e.g. a temporary circular reference resolution problem.
ex | the Exception to register |
---|
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.
beanName | the name of the bean |
---|