接口 SingletonBeanRegistry
- 所有已知实现类:
AbstractAutowireCapableBeanFactory
,AbstractBeanFactory
,DefaultListableBeanFactory
,DefaultSingletonBeanRegistry
,FactoryBeanRegistrySupport
,XmlBeanFactory
public interface SingletonBeanRegistry
Interface that defines a registry for shared bean instances. Can be implemented byBeanFactory
implementations in order to expose their singleton management facility in a uniform manner.The
ConfigurableBeanFactory
interface extends this interface.- 从以下版本开始:
- 2.0
- 作者:
- Juergen Hoeller
- 另请参阅:
ConfigurableBeanFactory
,DefaultSingletonBeanRegistry
,AbstractBeanFactory
方法概要
所有方法 实例方法 抽象方法 修饰符和类型 方法 说明 boolean
containsSingleton(String beanName)
Check if this registry contains a singleton instance with the given name.Object
getSingleton(String beanName)
Return the (raw) singleton object registered under the given name.int
getSingletonCount()
Return the number of singleton beans registered in this registry.Object
getSingletonMutex()
Return the singleton mutex used by this registry (for external collaborators).String[]
getSingletonNames()
Return the names of singleton beans registered 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.
方法详细资料
registerSingleton
void registerSingleton(String beanName, Object singletonObject)
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'sdestroy
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.
- 参数:
beanName
- the name of the beansingletonObject
- the existing singleton object- 另请参阅:
InitializingBean.afterPropertiesSet()
,DisposableBean.destroy()
,BeanDefinitionRegistry.registerBeanDefinition(java.lang.String, org.springframework.beans.factory.config.BeanDefinition)
getSingleton
Object getSingleton(String beanName)
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(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.
- 参数:
beanName
- the name of the bean to look for- 返回:
- the registered singleton object, or
null
if none found - 另请参阅:
ConfigurableListableBeanFactory.getBeanDefinition(java.lang.String)
containsSingleton
boolean containsSingleton(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(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 bothcontainsBeanDefinition
andcontainsSingleton
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.
- 参数:
beanName
- the name of the bean to look for- 返回:
- if this bean factory contains a singleton instance with the given name
- 另请参阅:
registerSingleton(java.lang.String, java.lang.Object)
,ListableBeanFactory.containsBeanDefinition(java.lang.String)
,BeanFactory.containsBean(java.lang.String)
getSingletonNames
String[] getSingletonNames()
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(java.lang.String, java.lang.Object)
). Can also be used to check which singletons defined by a bean definition have already been created.- 返回:
- the list of names as a String array (never
null
) - 另请参阅:
registerSingleton(java.lang.String, java.lang.Object)
,BeanDefinitionRegistry.getBeanDefinitionNames()
,ListableBeanFactory.getBeanDefinitionNames()
getSingletonCount
int getSingletonCount()
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(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.- 返回:
- the number of singleton beans
- 另请参阅:
registerSingleton(java.lang.String, java.lang.Object)
,BeanDefinitionRegistry.getBeanDefinitionCount()
,ListableBeanFactory.getBeanDefinitionCount()
getSingletonMutex
Object getSingletonMutex()
Return the singleton mutex used by this registry (for external collaborators).- 返回:
- the mutex object (never
null
) - 从以下版本开始:
- 4.2