Spring 概念模型 -- SingletonBeanRegistry 单例bean对象注册表

概述

Spring容器对单例bean实例的登记管理主要通过接口SingletonBeanRegistry建模抽象,我们可以称之为"单例bean实例注册表"。该接口约定了实现者(一般指容器)具有如下能力 :

  1. 注册登记将创建完成的单例bean实例;
  2. 获取某个指定名称的单例bean实例;
  3. 检测是否已经注册登记了某个指定名称的单例bean实例;
  4. 获取所有单例bean实例的名称;
  5. 获取注册登记的所有单例bean实例的数量;
  6. 获取当前单例bean实例注册表的互斥量(mutex),供外部使用者协作(collaborator)使用;

DefaultSingletonBeanRegistrySingletonBeanRegistry的实现类,也是Spring bean容器的基类,Spring bean容器对单例bean的注册管理能力正是来源于此 : SingletonBeanRegistry + DefaultSingletonBeanRegistry

Spring 概念模型 -- SingletonBeanRegistry 单例bean对象注册表_第1张图片

源代码

public interface SingletonBeanRegistry {

	/**
	 * Register the given existing object as singleton in the bean registry,
	 * under the given bean name.
	 * 使用给定的名称将一个已经存在的对象作为单例注册到该bean注册表。
	 * 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).
	 * 所给定的实例应该是已经被完全初始化了的;注册表不会再调用任何初始化回调
	 * (具体地讲,它不会调用InitializingBean接口所定义的方法afterPropertiesSet )。
	 * 
	 * The given instance will not receive any destruction callbacks
	 * (like DisposableBean's destroy method) either.
	 * 所给定的实例也不会受到任何析构回调(比如DisposableBean约定的destroy方法)。
	 * 
	 * 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.
	 * @param beanName the name of the bean
	 * @param singletonObject the existing singleton object
	 */
	void registerSingleton(String beanName, Object singletonObject);

	/**
	 * 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}. 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.
	 * @param beanName the name of the bean to look for
	 * @return the registered singleton object, or null if none found
	 */
	@Nullable
	Object getSingleton(String beanName);

	/**
	 * 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}. 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.
	 * @param beanName the name of the bean to look for
	 * @return if this bean factory contains a singleton instance with the given name
	 * @see #registerSingleton
	 * @see org.springframework.beans.factory.ListableBeanFactory#containsBeanDefinition
	 * @see org.springframework.beans.factory.BeanFactory#containsBean
	 */
	boolean containsSingleton(String beanName);

	/**
	 * 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}. Can also be used to check which singletons
	 * defined by a bean definition have already been created.
	 * @return the list of names as a String array (never null}
	 */
	String[] getSingletonNames();

	/**
	 * 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 #registerSingleton). Can also be used to count the number of
	 * singletons defined by a bean definition that have already been created.
	 * @return the number of singleton beans
	 */
	int getSingletonCount();

	/**
	 * Return the singleton mutex used by this registry (for external collaborators).
	 * @return the mutex object (never null)
	 * @since 4.2
	 */
	Object getSingletonMutex();

}
方法 介绍
void registerSingleton(String beanName, Object singletonObject) 将指定bean对象singletonObject作为单例bean对象注册,使用bean名称beanNamesingletonObject需要是已经完全初始化的。
Object getSingleton(String beanName) 获取指定名称的某个单例bean对象。仅仅检查已经完全初始化的单例对象,而不会检查和返回尚未初始化的单例bean定义对象。该方法的主要目的是手工访问所注册的单例bean
boolean containsSingleton(String beanName) 是否包含某个指定名称的单例bean
String[] getSingletonNames() 获取所有单例bean的名称
int getSingletonCount() 获取所有单例bean的数量
Object getSingletonMutex() 获取当前单例bean注册表的互斥量(mutex),使用者通过该互斥量协同访问当前注册表

你可能感兴趣的:(spring,Spring,Core)