1. 程式人生 > >springboot原始碼解析-管中窺豹系列之BeanDefinition(八)

springboot原始碼解析-管中窺豹系列之BeanDefinition(八)

# 一、前言 - Springboot原始碼解析是一件大工程,逐行逐句的去研究程式碼,會很枯燥,也不容易堅持下去。 - 我們不追求大而全,而是試著每次去研究一個小知識點,最終聚沙成塔,這就是我們的springboot原始碼管中窺豹系列。 ![ 簡介 ](https://zhangbin1989.gitee.io/blog/picture/zb0018_springsour/springboot_source_0.png) # 二、BeanDefinition - spring幫我們管理bean,就是通過BeanDefinition實現的,要深研原始碼,就繞不開BeanDefinition - 我們通過原始碼來看看BeanDefinition到底做了什麼 # 三、原始碼分析 我們先看看這個類:BeanDefinition ``` public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement { String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON; String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE; int ROLE_APPLICATION = 0; int ROLE_SUPPORT = 1; int ROLE_INFRASTRUCTURE = 2; void setParentName(@Nullable String parentName); @Nullable String getParentName(); void setBeanClassName(@Nullable String beanClassName); @Nullable String getBeanClassName(); void setScope(@Nullable String scope); @Nullable String getScope(); void setLazyInit(boolean lazyInit); boolean isLazyInit(); void setDependsOn(@Nullable String... dependsOn); @Nullable String[] getDependsOn(); void setAutowireCandidate(boolean autowireCandidate); boolean isAutowireCandidate(); void setPrimary(boolean primary); boolean isPrimary(); void setFactoryBeanName(@Nullable String factoryBeanName); @Nullable String getFactoryBeanName(); void setFactoryMethodName(@Nullable String factoryMethodName); @Nullable String getFactoryMethodName(); ConstructorArgumentValues getConstructorArgumentValues(); default boolean hasConstructorArgumentValues() { return !getConstructorArgumentValues().isEmpty(); } MutablePropertyValues getPropertyValues(); default boolean hasPropertyValues() { return !getPropertyValues().isEmpty(); } boolean isSingleton(); boolean isPrototype(); boolean isAbstract(); int getRole(); @Nullable String getDescription(); @Nullable String getResourceDescription(); @Nullable BeanDefinition getOriginatingBeanDefinition(); } ``` 定義了一個bean的各種屬性: - 父類:setParentName - class名稱:setBeanClassName - 作用域:setScope - 懶載入:setLazyInit - 依賴: setDependsOn - 被依賴: setAutowireCandidate - 自動載入:setPrimary - 工廠bean: setFactoryBeanName - 工廠方法:setFactoryMethodName - 單例:isSingleton - 多例:isPrototype - 靜態:isAbstract 我們用idea開啟圖屬性看看: ![ beanDefinition ](https://zhangbin1989.gitee.io/blog/picture/zb0018_springsour/beanDefinition.png) - 繼承了AttributeAccessor,擁有獲取屬性的能力 - 繼承了BeanMetadataElement,擁有了獲取源的能力 - 定義了各種介面,用來描述一個bean 我們在看看它的實現類: ![ beanDefinition ](https://zhangbin1989.gitee.io/blog/picture/zb0018_springsour/beanDefinition2.png) - spring的常用方式:定義介面,寫抽象類實現介面(核心功能寫在這裡),各種實現 - 介面:BeanDefinition - 抽象類: AbstractBeanDefinition - 實現類:RootBeanDefinition,ChildBeanDefinition,GenericBeanDefinition,AnnotatedGenericBeanDefinition,ScannedGenericBeanDefinition - Root和Child是早期使用的,從spring2.5以後統一用Geniric 此外還有一個封裝類:BeanDefinitionHolder ``` public class BeanDefinitionHolder implements BeanMetadataElement { private final BeanDefinition beanDefinition; private final String beanName; @Nullable private final String[] aliases; ... } ``` - 擴充套件別名的時候會用到 - 這一節,我們先對整體的beanDefinition家族有個瞭解,後面講原始碼的時候會用到。 - 目錄: [springboot原始碼解析-管中窺豹系列](https://www.cnblogs.com/zhangbin1989/p/14379131.html) ![ 豐極 ](https://zhangbin1989.gitee.io/blog/picture/zb0000_common/qrcode.jpg) 歡迎關注微信公眾號:豐極,更多技術學習