日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring beans源码解读之--BeanFactory的注册

發布時間:2025/4/5 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring beans源码解读之--BeanFactory的注册 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

beanFactory的繼承關系如下圖所示:

? ? ? (圖片來源:http://www.myexception.cn/software-architecture-design/925888.html)

在上節beanFactory的進化史,我們就講到了上圖的左邊部分,這次我們來分析一下圖的右邊部分。AliasRegistry 是一個用于別名管理的通用接口,BeanDefinitionRegistry繼承了該接口。

SimpleAliasRegistry作為一個基類,實現了AliasRegistry接口。

SingletonBeanRegistry是spring 提供的一個共享bean的注冊接口。實現該接口可以方便對單例進行管理。

public interface SingletonBeanRegistry {/*** Register the given existing object as singleton in the bean registry,* under the given bean name.* <p>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 {@code afterPropertiesSet} method).* The given instance will not receive any destruction callbacks* (like DisposableBean's {@code destroy} method) either.* <p>When running within a full BeanFactory: <b>Register a bean definition* instead of an existing instance if your bean is supposed to receive* initialization and/or destruction callbacks.</b>* <p>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* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet* @see org.springframework.beans.factory.DisposableBean#destroy* @see org.springframework.beans.factory.support.BeanDefinitionRegistry#registerBeanDefinition*/void registerSingleton(String beanName, Object singletonObject);/*** Return the (raw) singleton object registered under the given name.* <p>Only checks already instantiated singletons; does not return an Object* for singleton bean definitions which have not been instantiated yet.* <p>The main purpose of this method is to access manually registered singletons* (see {@link #registerSingleton}). Can also be used to access a singleton* defined by a bean definition that already been created, in a raw fashion.* <p><b>NOTE:</b> 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 {@code null} if none found* @see ConfigurableListableBeanFactory#getBeanDefinition*/Object getSingleton(String beanName);/*** Check if this registry contains a singleton instance with the given name.* <p>Only checks already instantiated singletons; does not return {@code true}* for singleton bean definitions which have not been instantiated yet.* <p>The main purpose of this method is to check manually registered singletons* (see {@link #registerSingleton}). Can also be used to check whether a* singleton defined by a bean definition has already been created.* <p>To check whether a bean factory contains a bean definition with a given name,* use ListableBeanFactory's {@code containsBeanDefinition}. Calling both* {@code containsBeanDefinition} and {@code containsSingleton} answers* whether a specific bean factory contains a local bean instance with the given name.* <p>Use BeanFactory's {@code 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.* <p><b>NOTE:</b> 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.* <p>Only checks already instantiated singletons; does not return names* for singleton bean definitions which have not been instantiated yet.* <p>The main purpose of this method is to check manually registered singletons* (see {@link #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 {@code null})* @see #registerSingleton* @see org.springframework.beans.factory.support.BeanDefinitionRegistry#getBeanDefinitionNames* @see org.springframework.beans.factory.ListableBeanFactory#getBeanDefinitionNames*/String[] getSingletonNames();/*** Return the number of singleton beans registered in this registry.* <p>Only checks already instantiated singletons; does not count* singleton bean definitions which have not been instantiated yet.* <p>The main purpose of this method is to check manually registered singletons* (see {@link #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* @see #registerSingleton* @see org.springframework.beans.factory.support.BeanDefinitionRegistry#getBeanDefinitionCount* @see org.springframework.beans.factory.ListableBeanFactory#getBeanDefinitionCount*/int getSingletonCount();}

注冊接口的默認實現為:DefaultSingletonBeanRegistry,主要作用是提供能被所有調用者共享bean的注冊,調用者可用通過bean 名稱獲取到該bean的實例。同時該類也提供了對注冊bean的銷毀方法。

DefaultSingletonBeanRegistry也實現了AliasRegistry接口,增加了對別名的管理。

?FactoryBeanRegistrySupport 集成了DefaultSingletonBeanRegistry對bean的單例bean的管理功能,是一個基類,用來處理FactoryBean實例的單例bean的注冊。

我們重點介紹來研究一下AbstractBeanFactory的代碼:

public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport implements ConfigurableBeanFactory { // to do more }

從繼承關系上來講,AbstractBeanFactory具備了ConfigurableBeanFactory 的完整功能,

轉載于:https://www.cnblogs.com/davidwang456/p/4153756.html

總結

以上是生活随笔為你收集整理的spring beans源码解读之--BeanFactory的注册的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。