|
| 1 | +/** |
| 2 | + * Interface that defines a registry for shared bean instances. |
| 3 | + * Can be implemented by {@link org.springframework.beans.factory.BeanFactory} |
| 4 | + * implementations in order to expose their singleton management facility |
| 5 | + * in a uniform manner. |
| 6 | + * |
| 7 | + * <p>The {@link ConfigurableBeanFactory} interface extends this interface. |
| 8 | + * |
| 9 | + * @author Juergen Hoeller |
| 10 | + * @since 2.0 |
| 11 | + * @see ConfigurableBeanFactory |
| 12 | + * @see org.springframework.beans.factory.support.DefaultSingletonBeanRegistry |
| 13 | + * @see org.springframework.beans.factory.support.AbstractBeanFactory |
| 14 | + */ |
| 15 | +abstract class SingletonBeanRegistry { |
| 16 | + /** |
| 17 | + * Register the given existing object as singleton in the bean registry, |
| 18 | + * under the given bean name. |
| 19 | + * <p>The given instance is supposed to be fully initialized; the registry |
| 20 | + * will not perform any initialization callbacks (in particular, it won't |
| 21 | + * call InitializingBean's {@code afterPropertiesSet} method). |
| 22 | + * The given instance will not receive any destruction callbacks |
| 23 | + * (like DisposableBean's {@code destroy} method) either. |
| 24 | + * <p>When running within a full BeanFactory: <b>Register a bean definition |
| 25 | + * instead of an existing instance if your bean is supposed to receive |
| 26 | + * initialization and/or destruction callbacks.</b> |
| 27 | + * <p>Typically invoked during registry configuration, but can also be used |
| 28 | + * for runtime registration of singletons. As a consequence, a registry |
| 29 | + * implementation should synchronize singleton access; it will have to do |
| 30 | + * this anyway if it supports a BeanFactory's lazy initialization of singletons. |
| 31 | + * @param beanName the name of the bean |
| 32 | + * @param singletonObject the existing singleton object |
| 33 | + * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet |
| 34 | + * @see org.springframework.beans.factory.DisposableBean#destroy |
| 35 | + * @see org.springframework.beans.factory.support.BeanDefinitionRegistry#registerBeanDefinition |
| 36 | + */ |
| 37 | + void registerSingleton(String beanName, Object singletonObject); |
| 38 | + |
| 39 | + /** |
| 40 | + * Return the (raw) singleton object registered under the given name. |
| 41 | + * <p>Only checks already instantiated singletons; does not return an Object |
| 42 | + * for singleton bean definitions which have not been instantiated yet. |
| 43 | + * <p>The main purpose of this method is to access manually registered singletons |
| 44 | + * (see {@link #registerSingleton}). Can also be used to access a singleton |
| 45 | + * defined by a bean definition that already been created, in a raw fashion. |
| 46 | + * <p><b>NOTE:</b> This lookup method is not aware of FactoryBean prefixes or aliases. |
| 47 | + * You need to resolve the canonical bean name first before obtaining the singleton instance. |
| 48 | + * @param beanName the name of the bean to look for |
| 49 | + * @return the registered singleton object, or {@code null} if none found |
| 50 | + * @see ConfigurableListableBeanFactory#getBeanDefinition |
| 51 | + */ |
| 52 | + Object getSingleton(String beanName); |
| 53 | + |
| 54 | + /** |
| 55 | + * Check if this registry contains a singleton instance with the given name. |
| 56 | + * <p>Only checks already instantiated singletons; does not return {@code true} |
| 57 | + * for singleton bean definitions which have not been instantiated yet. |
| 58 | + * <p>The main purpose of this method is to check manually registered singletons |
| 59 | + * (see {@link #registerSingleton}). Can also be used to check whether a |
| 60 | + * singleton defined by a bean definition has already been created. |
| 61 | + * <p>To check whether a bean factory contains a bean definition with a given name, |
| 62 | + * use ListableBeanFactory's {@code containsBeanDefinition}. Calling both |
| 63 | + * {@code containsBeanDefinition} and {@code containsSingleton} answers |
| 64 | + * whether a specific bean factory contains a local bean instance with the given name. |
| 65 | + * <p>Use BeanFactory's {@code containsBean} for general checks whether the |
| 66 | + * factory knows about a bean with a given name (whether manually registered singleton |
| 67 | + * instance or created by bean definition), also checking ancestor factories. |
| 68 | + * <p><b>NOTE:</b> This lookup method is not aware of FactoryBean prefixes or aliases. |
| 69 | + * You need to resolve the canonical bean name first before checking the singleton status. |
| 70 | + * @param beanName the name of the bean to look for |
| 71 | + * @return if this bean factory contains a singleton instance with the given name |
| 72 | + * @see #registerSingleton |
| 73 | + * @see org.springframework.beans.factory.ListableBeanFactory#containsBeanDefinition |
| 74 | + * @see org.springframework.beans.factory.BeanFactory#containsBean |
| 75 | + */ |
| 76 | + bool containsSingleton(String beanName); |
| 77 | + |
| 78 | + /** |
| 79 | + * Return the names of singleton beans registered in this registry. |
| 80 | + * <p>Only checks already instantiated singletons; does not return names |
| 81 | + * for singleton bean definitions which have not been instantiated yet. |
| 82 | + * <p>The main purpose of this method is to check manually registered singletons |
| 83 | + * (see {@link #registerSingleton}). Can also be used to check which singletons |
| 84 | + * defined by a bean definition have already been created. |
| 85 | + * @return the list of names as a String array (never {@code null}) |
| 86 | + * @see #registerSingleton |
| 87 | + * @see org.springframework.beans.factory.support.BeanDefinitionRegistry#getBeanDefinitionNames |
| 88 | + * @see org.springframework.beans.factory.ListableBeanFactory#getBeanDefinitionNames |
| 89 | + */ |
| 90 | + List<String> getSingletonNames(); |
| 91 | + |
| 92 | + /** |
| 93 | + * Return the number of singleton beans registered in this registry. |
| 94 | + * <p>Only checks already instantiated singletons; does not count |
| 95 | + * singleton bean definitions which have not been instantiated yet. |
| 96 | + * <p>The main purpose of this method is to check manually registered singletons |
| 97 | + * (see {@link #registerSingleton}). Can also be used to count the number of |
| 98 | + * singletons defined by a bean definition that have already been created. |
| 99 | + * @return the number of singleton beans |
| 100 | + * @see #registerSingleton |
| 101 | + * @see org.springframework.beans.factory.support.BeanDefinitionRegistry#getBeanDefinitionCount |
| 102 | + * @see org.springframework.beans.factory.ListableBeanFactory#getBeanDefinitionCount |
| 103 | + */ |
| 104 | + int getSingletonCount(); |
| 105 | + |
| 106 | + /** |
| 107 | + * Return the singleton mutex used by this registry (for external collaborators). |
| 108 | + * @return the mutex object (never {@code null}) |
| 109 | + * @since 4.2 |
| 110 | + */ |
| 111 | + Object getSingletonMutex(); |
| 112 | +} |
0 commit comments