org.springframework.beans.factory.config.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
org. 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
org. implementations,
factoring out the common management of singleton bean instances. Note that
the org.
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, registerDisposableBean, org.springframework.beans.factory.DisposableBean , org.springframework.beans.factory.config.ConfigurableBeanFactory
| Modifier and Type | Field and Description |
|---|---|
| private final Map | containedBeanMap
Map between containing bean names: bean name to Set of bean names that the bean contains. |
| private final Map | dependenciesForBeanMap
Map between depending bean names: bean name to Set of bean names for the bean's dependencies. |
| private final Map | dependentBeanMap
Map between dependent bean names: bean name to Set of dependent bean names. |
| private final Map | disposableBeans
Disposable bean instances: bean name to disposable instance. |
| private final Map | earlySingletonObjects
Cache of early singleton objects: bean name to bean instance. |
| private final Set | inCreationCheckExclusions
Names of beans currently excluded from in creation checks. |
| private final Set | registeredSingletons
Set of registered singletons, containing the bean names in registration order. |
| private final Map | singletonFactories
Cache of singleton factories: bean name to ObjectFactory. |
| private final Map | singletonObjects
Cache of singleton objects: bean name to bean instance. |
| private final Set | singletonsCurrentlyInCreation
Names of beans that are currently in creation. |
| private boolean | singletonsCurrentlyInDestruction
Flag that indicates whether we're currently within destroySingletons. |
| private static final int | SUPPRESSED_EXCEPTIONS_LIMIT
Maximum number of suppressed exceptions to preserve. |
| private Set | suppressedExceptions
Collection of suppressed Exceptions, available for associating related causes. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| protected void | addSingleton(String
the name of the bean beanName, Object the singleton object singletonObject)Add the given singleton object to the singleton cache of this factory. |
| protected void | addSingletonFactory(String
the name of the bean beanName, ObjectFactory<?> the factory for the singleton object singletonFactory)Add the given singleton factory for building the specified singleton if necessary. |
| protected void | afterSingletonCreation(String
the name of the singleton that has been created beanName)Callback after singleton creation. |
| protected void | beforeSingletonCreation(String
the name of the singleton about to be created beanName)Callback before singleton creation. |
| protected void | |
| public boolean | containsSingleton(String
the name of the bean to look for beanName)Implements org. Check if this registry contains a singleton instance with the given name. |
| protected void | destroyBean(String
the name of the bean beanName, DisposableBean the bean instance to destroy bean)Destroy the given bean. |
| public void | |
| public void | |
| public String[] | Returns: the array of names of beans which the bean depends on, or an empty array if nonethe name of the bean beanName)Return the names of all beans that the specified bean depends on, if any. |
| public String[] | Returns: the array of dependent bean names, or an empty array if nonethe name of the bean beanName)Return the names of all beans which depend on the specified bean, if any. |
| public Object | getSingleton(String
the name of the bean to look for beanName)Implements org. Return the (raw) singleton object registered under the given name. |
| protected Object | Returns: the registered singleton object, ornull if none foundthe name of the bean to look for beanName, boolean whether early references should be created or not allowEarlyReference)Return the (raw) singleton object registered under the given name. |
| public Object | Returns: the registered singleton objectthe name of the bean beanName, ObjectFactory<?> the ObjectFactory to lazily create the singleton
with, if necessary singletonFactory)Return the (raw) singleton object registered under the given name, creating and registering a new one if none registered yet. |
| public int | getSingletonCount()
Implements org. Return the number of singleton beans registered in this registry. |
| public final Object | getSingletonMutex()
Implements org. Exposes the singleton mutex to subclasses and external collaborators. |
| public String[] | getSingletonNames()
Implements org. Return the names of singleton beans registered in this registry. |
| protected boolean | hasDependentBean(String
the name of the bean to check beanName)Determine whether a dependent bean has been registered for the given name. |
| protected boolean | |
| public boolean | |
| protected boolean | isDependent(String
the name of the bean to check beanName, String the name of the dependent bean dependentBeanName)Determine whether the specified dependent bean has been registered as dependent on the given bean or on any of its transitive dependencies. |
| private boolean | |
| public boolean | isSingletonCurrentlyInCreation(String
the name of the bean beanName)Return whether the specified singleton bean is currently in creation (within the entire factory). |
| protected void | onSuppressedException(Exception
the Exception to register ex)Register an exception that happened to get suppressed during the creation of a singleton bean instance, e.g. a temporary circular reference resolution problem. |
| public void | registerContainedBean(String
the name of the contained (inner) bean containedBeanName, String the name of the containing (outer) bean containingBeanName)Register a containment relationship between two beans, e.g. between an inner bean and its containing outer bean. |
| public void | registerDependentBean(String
the name of the bean beanName, String the name of the dependent bean dependentBeanName)Register a dependent bean for the given bean, to be destroyed before the given bean is destroyed. |
| public void | registerDisposableBean(String
the name of the bean beanName, DisposableBean the bean instance bean)Add the given bean to the list of disposable beans in this registry. |
| public void | registerSingleton(String
the name of the bean beanName, Object the existing singleton object singletonObject)Implements org. Register the given existing object as singleton in the bean registry, under the given bean name. |
| protected void | removeSingleton(String
the name of the bean 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. |
| public void |
| containedBeanMap | back to summary |
|---|---|
| private final Map<String, Set<String>> containedBeanMap Map between containing bean names: bean name to Set of bean names that the bean contains. | |
| dependenciesForBeanMap | back to summary |
|---|---|
| private final Map<String, Set<String>> dependenciesForBeanMap Map between depending bean names: bean name to Set of bean names for the bean's dependencies. | |
| dependentBeanMap | back to summary |
|---|---|
| private final Map<String, Set<String>> dependentBeanMap Map between dependent bean names: bean name to Set of dependent bean names. | |
| disposableBeans | back to summary |
|---|---|
| private final Map<String, DisposableBean> disposableBeans Disposable bean instances: bean name to disposable instance. | |
| earlySingletonObjects | back to summary |
|---|---|
| private final Map<String, Object> earlySingletonObjects Cache of early singleton objects: bean name to bean instance. | |
| inCreationCheckExclusions | back to summary |
|---|---|
| private final Set<String> inCreationCheckExclusions Names of beans currently excluded from in creation checks. | |
| registeredSingletons | back to summary |
|---|---|
| private final Set<String> registeredSingletons Set of registered singletons, containing the bean names in registration order. | |
| singletonFactories | back to summary |
|---|---|
| private final Map<String, ObjectFactory<?>> singletonFactories Cache of singleton factories: bean name to ObjectFactory. | |
| singletonObjects | back to summary |
|---|---|
| private final Map<String, Object> singletonObjects Cache of singleton objects: bean name to bean instance. | |
| singletonsCurrentlyInCreation | back to summary |
|---|---|
| private final Set<String> singletonsCurrentlyInCreation Names of beans that are currently in creation. | |
| singletonsCurrentlyInDestruction | back to summary |
|---|---|
| private boolean singletonsCurrentlyInDestruction Flag that indicates whether we're currently within destroySingletons. | |
| SUPPRESSED_EXCEPTIONS_LIMIT | back to summary |
|---|---|
| private static final int SUPPRESSED_EXCEPTIONS_LIMIT Maximum number of suppressed exceptions to preserve. | |
| suppressedExceptions | back to summary |
|---|---|
| private Set<Exception> suppressedExceptions Collection of suppressed Exceptions, available for associating related causes. | |
| DefaultSingletonBeanRegistry | back to summary |
|---|---|
| public DefaultSingletonBeanRegistry() | |
| addSingleton | back to summary |
|---|---|
| protected void addSingleton(String beanName, Object singletonObject) Add the given singleton object to the singleton cache of this factory. To be called for eager registration of singletons. | |
| addSingletonFactory | back to summary |
|---|---|
| protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) 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.
| |
| afterSingletonCreation | back to summary |
|---|---|
| protected void afterSingletonCreation(String beanName) Callback after singleton creation. The default implementation marks the singleton as not in creation anymore.
| |
| beforeSingletonCreation | back to summary |
|---|---|
| protected void beforeSingletonCreation(String beanName) Callback before singleton creation. The default implementation register the singleton as currently in creation.
| |
| clearSingletonCache | back to summary |
|---|---|
| protected void clearSingletonCache() Clear all cached singleton instances in this registry.
| |
| containsSingleton | back to summary |
|---|---|
| public boolean containsSingleton(String beanName) Implements org. Doc from org. Check if this registry contains a singleton instance with the given name. Only checks already instantiated singletons; does not return The main purpose of this method is to check manually registered singletons
(see To check whether a bean factory contains a bean definition with a given name,
use ListableBeanFactory's Use BeanFactory's 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. | |
| destroyBean | back to summary |
|---|---|
| protected void destroyBean(String beanName, DisposableBean bean) Destroy the given bean. Must destroy beans that depend on the given bean before the bean itself. Should not throw any exceptions.
| |
| destroySingleton | back to summary |
|---|---|
| public void destroySingleton(String beanName) Destroy the given bean. Delegates to
| |
| destroySingletons | back to summary |
|---|---|
| public void destroySingletons() | |
| getDependenciesForBean | back to summary |
|---|---|
| public String[] getDependenciesForBean(String beanName) Return the names of all beans that the specified bean depends on, if any. | |
| getDependentBeans | back to summary |
|---|---|
| public String[] getDependentBeans(String beanName) Return the names of all beans which depend on the specified bean, if any. | |
| getSingleton | back to summary |
|---|---|
| public Object getSingleton(String beanName) Implements org. Doc from org. 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 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 | back to summary |
|---|---|
| protected Object getSingleton(String beanName, boolean allowEarlyReference) 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). | |
| getSingleton | back to summary |
|---|---|
| public 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.
| |
| getSingletonCount | back to summary |
|---|---|
| public int getSingletonCount() Implements org. Doc from org. Return 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
| |
| getSingletonMutex | back to summary |
|---|---|
| public final Object getSingletonMutex() Implements org. 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.
| |
| getSingletonNames | back to summary |
|---|---|
| public String[] getSingletonNames() Implements org. Doc from org. 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 | |
| hasDependentBean | back to summary |
|---|---|
| protected boolean hasDependentBean(String beanName) Determine whether a dependent bean has been registered for the given name.
| |
| isActuallyInCreation | back to summary |
|---|---|
| protected boolean isActuallyInCreation(String beanName) | |
| isCurrentlyInCreation | back to summary |
|---|---|
| public boolean isCurrentlyInCreation(String beanName) | |
| isDependent | back to summary |
|---|---|
| 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. | |
| isDependent | back to summary |
|---|---|
| private boolean isDependent(String beanName, String dependentBeanName, Set<String> alreadySeen) | |
| isSingletonCurrentlyInCreation | back to summary |
|---|---|
| public boolean isSingletonCurrentlyInCreation(String beanName) Return whether the specified singleton bean is currently in creation (within the entire factory).
| |
| onSuppressedException | back to summary |
|---|---|
| protected void onSuppressedException(Exception ex) Register an exception that happened to get suppressed during the creation of a singleton bean instance, e.g. a temporary circular reference resolution problem. 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
| |
| registerContainedBean | back to summary |
|---|---|
| public void registerContainedBean(String containedBeanName, String containingBeanName) 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.
| |
| registerDependentBean | back to summary |
|---|---|
| public void registerDependentBean(String beanName, String dependentBeanName) Register a dependent bean for the given bean, to be destroyed before the given bean is destroyed. | |
| registerDisposableBean | back to summary |
|---|---|
| public void registerDisposableBean(String beanName, DisposableBean 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).
| |
| registerSingleton | back to summary |
|---|---|
| public void registerSingleton(String beanName, Object singletonObject) throws IllegalStateException Implements org. Doc from org. 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 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. | |
| removeSingleton | back to summary |
|---|---|
| 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.
| |
| setCurrentlyInCreation | back to summary |
|---|---|
| public void setCurrentlyInCreation(String beanName, boolean inCreation) | |