Package org.springframework.beans.factory.support

# Class DefaultSingletonBeanRegistry

java.lang.Object
org.springframework.core.SimpleAliasRegistry
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry

All Implemented Interfaces:
  `SingletonBeanRegistry`, `org.springframework.core.AliasRegistry`

Direct Known Subclasses:
  `FactoryBeanRegistrySupport`

public class DefaultSingletonBeanRegistry extends org.springframework.core.SimpleAliasRegistry implements SingletonBeanRegistry
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.

Since:
  2.0
Author:
  Juergen Hoeller
See Also:
  - `registerSingleton(java.lang.String, java.lang.Object)`
  - `registerDisposableBean(java.lang.String, org.springframework.beans.factory.DisposableBean)`
  - `DisposableBean`
  - `ConfigurableBeanFactory`

## Field Summary

### Fields inherited from class org.springframework.core.SimpleAliasRegistry

`logger`

## Constructor Summary

Constructors
Constructor
Description
`DefaultSingletonBeanRegistry()`

## Method Summary

Modifier and Type
Method
Description
`protected void`
`addSingleton(String beanName, Object singletonObject)`
Add the given singleton object to the singleton registry.
`void`
`addSingletonCallback(String beanName, Consumer<Object> singletonConsumer)`
Add a callback to be triggered when the specified singleton becomes available in the bean registry.
`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.
`final Object`
`getSingletonMutex()`
Deprecated.
`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`
`isCurrentThreadAllowedToHoldSingletonLock()`
Determine whether the current thread is allowed to hold the singleton lock.
`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, for example, a temporary circular reference resolution problem.
`void`
`registerContainedBean(String containedBeanName, String containingBeanName)`
Register a containment relationship between two beans, for example, between an inner bean and its containing outer bean.
`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 registry, either on regular destruction or on cleanup after early exposure when creation failed.
`void`
`setCurrentlyInCreation(String beanName, boolean inCreation)`

### Methods inherited from class org.springframework.core.SimpleAliasRegistry

`allowAliasOverriding, canonicalName, checkForAliasCircle, getAliases, hasAlias, isAlias, registerAlias, removeAlias, resolveAliases`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### DefaultSingletonBeanRegistry

public DefaultSingletonBeanRegistry()

## Method Details

### registerSingleton

public void registerSingleton(String beanName, Object singletonObject) throws IllegalStateException
Description copied from interface: `SingletonBeanRegistry`
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.

Specified by:
  `registerSingleton` in interface `SingletonBeanRegistry`
Parameters:
  `beanName` - the name of the bean
  `singletonObject` - the existing singleton object
Throws:
  `IllegalStateException`
See Also:
  - `InitializingBean.afterPropertiesSet()`
  - `DisposableBean.destroy()`
  - `BeanDefinitionRegistry.registerBeanDefinition(java.lang.String, org.springframework.beans.factory.config.BeanDefinition)`

### addSingleton

protected void addSingleton(String beanName, Object singletonObject)
Add the given singleton object to the singleton registry.

To be called for exposure of freshly registered/created singletons.

Parameters:
  `beanName` - the name of the bean
  `singletonObject` - the singleton object

### addSingletonFactory

protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory)
Add the given singleton factory for building the specified singleton if necessary.

To be called for early exposure purposes, for example, to be able to resolve circular references.

Parameters:
  `beanName` - the name of the bean
  `singletonFactory` - the factory for the singleton object

### addSingletonCallback

public void addSingletonCallback(String beanName, Consumer<Object> singletonConsumer)
Description copied from interface: `SingletonBeanRegistry`
Add a callback to be triggered when the specified singleton becomes available in the bean registry.

Specified by:
  `addSingletonCallback` in interface `SingletonBeanRegistry`
Parameters:
  `beanName` - the name of the bean
  `singletonConsumer` - a callback for reacting to the availability of the freshly registered/created singleton instance (intended for follow-up steps before the bean is actively used by other callers, not for modifying the given singleton instance itself)

### getSingleton

@Nullable public Object getSingleton(String beanName)
Description copied from interface: `SingletonBeanRegistry`
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 `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.

Specified by:
  `getSingleton` in interface `SingletonBeanRegistry`
Parameters:
  `beanName` - the name of the bean to look for
Returns:
  the registered singleton object, or `null` if none found
See Also:
  - `ConfigurableListableBeanFactory.getBeanDefinition(java.lang.String)`

### getSingleton

@Nullable 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).

Parameters:
  `beanName` - the name of the bean to look for
  `allowEarlyReference` - whether early references should be created or not
Returns:
  the registered singleton object, or `null` if none found

### getSingleton

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.

Parameters:
  `beanName` - the name of the bean
  `singletonFactory` - the ObjectFactory to lazily create the singleton with, if necessary
Returns:
  the registered singleton object

### isCurrentThreadAllowedToHoldSingletonLock

@Nullable protected Boolean isCurrentThreadAllowedToHoldSingletonLock()
Determine whether the current thread is allowed to hold the singleton lock.

By default, all threads are forced to hold a full lock through `null`. `DefaultListableBeanFactory` overrides this to specifically handle its threads during the pre-instantiation phase: `true` for the main thread, `false` for managed background threads, and configuration-dependent behavior for unmanaged threads.

Returns:
  `true` if the current thread is explicitly allowed to hold the lock but also accepts lenient fallback behavior, `false` if it is explicitly not allowed to hold the lock and therefore forced to use lenient fallback behavior, or `null` if there is no specific indication (traditional behavior: forced to always hold a full lock)
Since:
  6.2

### onSuppressedException

protected void onSuppressedException(Exception ex)
Register an exception that happened to get suppressed during the creation of a singleton bean instance, for example, 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 `BeanCreationException`.

Parameters:
  `ex` - the Exception to register
See Also:
  - `BeanCreationException.getRelatedCauses()`

### removeSingleton

protected void removeSingleton(String beanName)
Remove the bean with the given name from the singleton registry, either on regular destruction or on cleanup after early exposure when creation failed.

Parameters:
  `beanName` - the name of the bean

### containsSingleton

public boolean containsSingleton(String beanName)
Description copied from interface: `SingletonBeanRegistry`
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 `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.

Specified by:
  `containsSingleton` in interface `SingletonBeanRegistry`
Parameters:
  `beanName` - the name of the bean to look for
Returns:
  if this bean factory contains a singleton instance with the given name
See Also:
  - `SingletonBeanRegistry.registerSingleton(java.lang.String, java.lang.Object)`
  - `ListableBeanFactory.containsBeanDefinition(java.lang.String)`
  - `BeanFactory.containsBean(java.lang.String)`

### getSingletonNames

public String[] getSingletonNames()
Description copied from interface: `SingletonBeanRegistry`
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 `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.

Specified by:
  `getSingletonNames` in interface `SingletonBeanRegistry`
Returns:
  the list of names as a String array (never `null`)
See Also:
  - `SingletonBeanRegistry.registerSingleton(java.lang.String, java.lang.Object)`
  - `BeanDefinitionRegistry.getBeanDefinitionNames()`
  - `ListableBeanFactory.getBeanDefinitionNames()`

### getSingletonCount

public int getSingletonCount()
Description copied from interface: `SingletonBeanRegistry`
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 `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.

Specified by:
  `getSingletonCount` in interface `SingletonBeanRegistry`
Returns:
  the number of singleton beans
See Also:
  - `SingletonBeanRegistry.registerSingleton(java.lang.String, java.lang.Object)`
  - `BeanDefinitionRegistry.getBeanDefinitionCount()`
  - `ListableBeanFactory.getBeanDefinitionCount()`

### setCurrentlyInCreation

public void setCurrentlyInCreation(String beanName, boolean inCreation)

### isCurrentlyInCreation

public boolean isCurrentlyInCreation(String beanName)

### isActuallyInCreation

protected boolean isActuallyInCreation(String beanName)

### isSingletonCurrentlyInCreation

public boolean isSingletonCurrentlyInCreation(@Nullable String beanName)
Return whether the specified singleton bean is currently in creation (within the entire factory).

Parameters:
  `beanName` - the name of the bean

### beforeSingletonCreation

protected void beforeSingletonCreation(String beanName)
Callback before singleton creation.

The default implementation register the singleton as currently in creation.

Parameters:
  `beanName` - the name of the singleton about to be created
See Also:
  - `isSingletonCurrentlyInCreation(java.lang.String)`

### afterSingletonCreation

protected void afterSingletonCreation(String beanName)
Callback after singleton creation.

The default implementation marks the singleton as not in creation anymore.

Parameters:
  `beanName` - the name of the singleton that has been created
See Also:
  - `isSingletonCurrentlyInCreation(java.lang.String)`

### registerDisposableBean

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).

Parameters:
  `beanName` - the name of the bean
  `bean` - the bean instance

### registerContainedBean

public void registerContainedBean(String containedBeanName, String containingBeanName)
Register a containment relationship between two beans, for example, 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.

Parameters:
  `containedBeanName` - the name of the contained (inner) bean
  `containingBeanName` - the name of the containing (outer) bean
See Also:
  - `registerDependentBean(java.lang.String, java.lang.String)`

### registerDependentBean

public void registerDependentBean(String beanName, String dependentBeanName)
Register a dependent bean for the given bean, to be destroyed before the given bean is destroyed.

Parameters:
  `beanName` - the name of the bean
  `dependentBeanName` - the name of the dependent bean

### isDependent

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.

Parameters:
  `beanName` - the name of the bean to check
  `dependentBeanName` - the name of the dependent bean
Since:
  4.0

### hasDependentBean

protected boolean hasDependentBean(String beanName)
Determine whether a dependent bean has been registered for the given name.

Parameters:
  `beanName` - the name of the bean to check

### getDependentBeans

public String[] getDependentBeans(String beanName)
Return the names of all beans which depend on the specified bean, if any.

Parameters:
  `beanName` - the name of the bean
Returns:
  the array of dependent bean names, or an empty array if none

### getDependenciesForBean

public String[] getDependenciesForBean(String beanName)
Return the names of all beans that the specified bean depends on, if any.

Parameters:
  `beanName` - the name of the bean
Returns:
  the array of names of beans which the bean depends on, or an empty array if none

### destroySingletons

public void destroySingletons()

### clearSingletonCache

protected void clearSingletonCache()
Clear all cached singleton instances in this registry.

Since:
  4.3.15

### destroySingleton

public void destroySingleton(String beanName)
Destroy the given bean. Delegates to `destroyBean` if a corresponding disposable bean instance is found.

Parameters:
  `beanName` - the name of the bean
See Also:
  - `destroyBean(java.lang.String, org.springframework.beans.factory.DisposableBean)`

### destroyBean

protected void destroyBean(String beanName, @Nullable DisposableBean bean)
Destroy the given bean. Must destroy beans that depend on the given bean before the bean itself. Should not throw any exceptions.

Parameters:
  `beanName` - the name of the bean
  `bean` - the bean instance to destroy

### getSingletonMutex

@Deprecated(since="6.2") public final Object getSingletonMutex()
Deprecated.
Description copied from interface: `SingletonBeanRegistry`
Return the singleton mutex used by this registry (for external collaborators).

Specified by:
  `getSingletonMutex` in interface `SingletonBeanRegistry`
Returns:
  the mutex object (never `null`)