org.springframework.beans.factory.support

## 类 DefaultSingletonBeanRegistry

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

- 所有已实现的接口:
    SingletonBeanRegistry, org.springframework.core.AliasRegistry
  直接已知子类:
    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.

  从以下版本开始:
    2.0
  作者:
    Juergen Hoeller
  另请参阅:
    `registerSingleton(java.lang.String, java.lang.Object)`, `registerDisposableBean(java.lang.String, org.springframework.beans.factory.DisposableBean)`, `DisposableBean`, `ConfigurableBeanFactory`

- - ### 字段概要

    - ### 从类继承的字段 org.springframework.core.SimpleAliasRegistry

      `logger`

  - ### 构造器概要

    构造器
    构造器和说明
    `DefaultSingletonBeanRegistry()`

  - ### 方法概要

    所有方法 实例方法 具体方法
    限定符和类型 | 方法和说明
    --- | ---
    `protected void` | `addSingleton(String beanName, Object singletonObject)` 把对象加入到单例缓存池中(所谓的一级缓存 并且考虑循环依赖和正常情况下,移除二三级缓存)
    `protected void` | `addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory)` 该方法用于把早期对象包装成一个ObjectFactory 暴露到三级缓存中 用于将解决循环依赖...
    `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)` 销毁bean的依赖关系
    `void` | `destroySingleton(String beanName)` 发生移除，销毁当前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)` 该方法是一个空壳方法
    `protected Object` | `getSingleton(String beanName, boolean allowEarlyReference)` 在网上很多很多写源码的大佬或者是一书上,也没有说清楚为啥要使用三级缓存(二级缓存可不可以能够 解决) 答案是：可以, 但是没有很好的扩展性为啥这么说.......
    `Object` | `getSingleton(String beanName, ObjectFactory<?> singletonFactory)` 获取单例对象(该流程用于触发构建bean)
    `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. a temporary circular reference resolution problem.
    `void` | `registerContainedBean(String containedBeanName, String containingBeanName)` Register a containment relationship between two beans, e.g. between an inner bean and its containing outer bean.
    `void` | `registerDependentBean(String beanName, String dependentBeanName)` 注册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)`

    - ### 从类继承的方法 org.springframework.core.SimpleAliasRegistry

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

    - ### 从类继承的方法 java.lang.Object

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

- - ### 构造器详细资料

    - #### DefaultSingletonBeanRegistry

          public DefaultSingletonBeanRegistry()

  - ### 方法详细资料

    - #### registerSingleton

          public void registerSingleton(String beanName,
                                        Object singletonObject)
                                 throws IllegalStateException

      从接口复制的说明: `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.

      指定者:
        `registerSingleton` 在接口中 `SingletonBeanRegistry`
      参数:
        `beanName` - the name of the bean
        `singletonObject` - the existing singleton object
      抛出:
        `IllegalStateException`
      另请参阅:
        `InitializingBean.afterPropertiesSet()`, `DisposableBean.destroy()`, `BeanDefinitionRegistry.registerBeanDefinition(java.lang.String, org.springframework.beans.factory.config.BeanDefinition)`

    - #### addSingleton

          protected void addSingleton(String beanName,
                                      Object singletonObject)

      把对象加入到单例缓存池中(所谓的一级缓存 并且考虑循环依赖和正常情况下,移除二三级缓存)
      参数:
        `beanName` - bean的名称
        `singletonObject` - 创建出来的单实例bean

    - #### addSingletonFactory

          protected void addSingletonFactory(String beanName,
                                             ObjectFactory<?> singletonFactory)

      该方法用于把早期对象包装成一个ObjectFactory 暴露到三级缓存中 用于将解决循环依赖...
      参数:
        `beanName` - the name of the bean
        `singletonFactory` - the factory for the singleton object

    - #### getSingleton

          @Nullable
          public Object getSingleton(String beanName)

      该方法是一个空壳方法
      指定者:
        `getSingleton` 在接口中 `SingletonBeanRegistry`
      参数:
        `beanName` - bean的名称
      返回:
        缓存中的对象(有可能是一个单例完整对象,也有可能是一个早期对象(用于解决循环依赖))
      另请参阅:
        `ConfigurableListableBeanFactory.getBeanDefinition(java.lang.String)`

    - #### getSingleton

          @Nullable
          protected Object getSingleton(String beanName,
                                                   boolean allowEarlyReference)

      在网上很多很多写源码的大佬或者是一书上,也没有说清楚为啥要使用三级缓存(二级缓存可不可以能够 解决) 答案是：可以, 但是没有很好的扩展性为啥这么说....... 原因: 获取三级缓存-----getEarlyBeanReference()经过一系列的后置处理来给我们早期对象进行特殊化处理 //从三级缓存中获取包装对象的时候 ，他会经过一次后置处理器的处理对我们早期对象的bean进行 特殊化处理，但是spring的原生后置处理器没有经过处理，而是留给了我们程序员进行扩展 singletonObject = singletonFactory.getObject(); 把三级缓存移植到二级缓存中 this.earlySingletonObjects.put(beanName, singletonObject); //删除三级缓存中的之 this.singletonFactories.remove(beanName);
      参数:
        `beanName` - bean的名称
        `allowEarlyReference` - 是否允许暴露早期对象 通过该参数可以控制是否能够解决循环依赖的.
      返回:
        这里可能返回一个null（IOC容器加载单实例bean的时候,第一次进来是返回null） 也有可能返回一个单例对象(IOC容器加载了单实例了,第二次来获取当前的Bean) 也可能返回一个早期对象(用于解决循环依赖问题)

    - #### getSingleton

          public Object getSingleton(String beanName,
                                     ObjectFactory<?> singletonFactory)

      获取单例对象(该流程用于触发构建bean)
      参数:
        `beanName` - the name of the bean
        `singletonFactory` - the ObjectFactory to lazily create the singleton with, if necessary
      返回:
        the registered singleton object

    - #### onSuppressedException

          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.
      参数:
        `ex` - the Exception to register

    - #### removeSingleton

          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.
      参数:
        `beanName` - the name of the bean
      另请参阅:
        `getSingletonMutex()`

    - #### containsSingleton

          public boolean containsSingleton(String beanName)

      从接口复制的说明: `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.

      指定者:
        `containsSingleton` 在接口中 `SingletonBeanRegistry`
      参数:
        `beanName` - the name of the bean to look for
      返回:
        if this bean factory contains a singleton instance with the given name
      另请参阅:
        `SingletonBeanRegistry.registerSingleton(java.lang.String, java.lang.Object)`, `ListableBeanFactory.containsBeanDefinition(java.lang.String)`, `BeanFactory.containsBean(java.lang.String)`

    - #### getSingletonNames

          public String[] getSingletonNames()

      从接口复制的说明: `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.

      指定者:
        `getSingletonNames` 在接口中 `SingletonBeanRegistry`
      返回:
        the list of names as a String array (never `null`)
      另请参阅:
        `SingletonBeanRegistry.registerSingleton(java.lang.String, java.lang.Object)`, `BeanDefinitionRegistry.getBeanDefinitionNames()`, `ListableBeanFactory.getBeanDefinitionNames()`

    - #### getSingletonCount

          public int getSingletonCount()

      从接口复制的说明: `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.

      指定者:
        `getSingletonCount` 在接口中 `SingletonBeanRegistry`
      返回:
        the number of singleton beans
      另请参阅:
        `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(String beanName)

      Return whether the specified singleton bean is currently in creation (within the entire factory).
      参数:
        `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.

      参数:
        `beanName` - the name of the singleton about to be created
      另请参阅:
        `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.

      参数:
        `beanName` - the name of the singleton that has been created
      另请参阅:
        `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).

      参数:
        `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, 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
      另请参阅:
        `registerDependentBean(java.lang.String, java.lang.String)`

    - #### registerDependentBean

          public void registerDependentBean(String beanName,
                                            String dependentBeanName)

      注册bean之间的依赖关系 to be destroyed before the given bean is destroyed.
      参数:
        `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.
      参数:
        `beanName` - the name of the bean to check
        `dependentBeanName` - the name of the dependent bean
      从以下版本开始:
        4.0

    - #### hasDependentBean

          protected boolean hasDependentBean(String beanName)

      Determine whether a dependent bean has been registered for the given name.
      参数:
        `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.
      参数:
        `beanName` - the name of the bean
      返回:
        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.
      参数:
        `beanName` - the name of the bean
      返回:
        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.
      从以下版本开始:
        4.3.15

    - #### destroySingleton

          public void destroySingleton(String beanName)

      发生移除，销毁当前bean的所有信息
      参数:
        `beanName` - bean的名称
      另请参阅:
        `destroyBean(java.lang.String, org.springframework.beans.factory.DisposableBean)`

    - #### destroyBean

          protected void destroyBean(String beanName,
                                     @Nullable
                                     DisposableBean bean)

      销毁bean的依赖关系
      参数:
        `beanName` - the name of the bean
        `bean` - the bean instance to destroy

    - #### getSingletonMutex

          public final 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.

      指定者:
        `getSingletonMutex` 在接口中 `SingletonBeanRegistry`
      返回:
        the mutex object (never `null`)