Generic registry for shared bean instances, implementing the
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
(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.
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.
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.
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.
public void registerDisposableBean(java.lang.String beanName,
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
public void registerContainedBean(java.lang.String containedBeanName,
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
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
public final java.lang.Object getSingletonMutex()
Exposes the singleton mutex to subclasses and external collaborators.
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.