1. 程式人生 > >Spring源碼分析(八)AbstractBeanDefinition屬性

Spring源碼分析(八)AbstractBeanDefinition屬性

strac code 出現 shm main candidate 靜態變量 col aof

摘要:本文結合《Spring源碼深度解析》來分析Spring 5.0.6版本的源代碼。若有描述錯誤之處,歡迎指正。

在上一篇中已經完成了XML文檔到GenericBeanDefiniton的轉化,也就是說,XML中所有的配置都在GenericBeanDefinition的實例類中找到了對應的位置。

GenericBeanDefinition只是子類實現,大部分的屬性都保存在了AbstractBeanDefinition中,下面我們看下這個類:

public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccessor
        
implements BeanDefinition, Cloneable { // 此處省略靜態變量以及final變量 @Nullable private volatile Object beanClass; /** * bean的作用範圍,對應bean屬性scope */ @Nullable private String scope = SCOPE_DEFAULT; /** * 是否是抽象,對應bean屬性abstract */ private boolean abstractFlag = false
; /** * 是否延遲加載,對應bean屬性lazy-init */ private boolean lazyInit = false; /** * 自動註入模式,對應bean屬性autowire */ private int autowireMode = AUTOWIRE_NO; /** * 依賴檢查,Spring 3.0後棄用這個屬性 */ private int dependencyCheck = DEPENDENCY_CHECK_NONE; /** * 用來表示一個bean的實例化依靠另一個bean先實例化,對應bean屬性depend-on
*/ @Nullable private String[] dependsOn; /** * autowire-candidate屬性設置為false,這樣容器在查找自動裝配對象時, * 將不考慮該bean,即它不會被考慮作為其他bean自動裝配的候選者, * 但是該bean本身還是可以使用自動裝配來註入其他bean的 */ private boolean autowireCandidate = true; /** * 自動裝配時出現多個bean候選者時,將作為首選者,對應bean屬性primary */ private boolean primary = false; /** * 用於記錄Qualifier,對應子元素qualifier */ private final Map<String, AutowireCandidateQualifier> qualifiers = new LinkedHashMap<>(0); @Nullable private Supplier<?> instanceSupplier; /** * 允許訪問非公開的構造器和方法,程序設置 */ private boolean nonPublicAccessAllowed = true; /** * 是否以一種寬松的模式解析構造函數,默認為true, * 如果為false,則在以下情況 * interface ITest{} * class ITestImpl implements ITest{}; * class Main { * Main(ITest i) {} * Main(ITestImpl i) {} * } * 拋出異常,因為Spring無法準確定位哪個構造函數程序設置 */ private boolean lenientConstructorResolution = true; /** * 對應bean屬性factory-bean,用法: * <bean id = "instanceFactoryBean" class = "example.chapter3.InstanceFactoryBean" /> * <bean id = "currentTime" factory-bean = "instanceFactoryBean" factory-method = "createTime" /> */ @Nullable private String factoryBeanName; /** * 對應bean屬性factory-method */ @Nullable private String factoryMethodName; /** * 記錄構造函數註入屬性,對應bean屬性constructor-arg */ @Nullable private ConstructorArgumentValues constructorArgumentValues; /** * 普通屬性集合 */ @Nullable private MutablePropertyValues propertyValues; /** * 方法重寫的持有者,記錄lookup-method、replaced-method元素 */ @Nullable private MethodOverrides methodOverrides; /** * 初始化方法,對應bean屬性init-method */ @Nullable private String initMethodName; /** * 銷毀方法,對應bean屬性destroy-method */ @Nullable private String destroyMethodName; /** * 是否執行init-method,程序設置 */ private boolean enforceInitMethod = true; /** * 是否執行destroy-method,程序設置 */ private boolean enforceDestroyMethod = true; /** * 是否是用戶定義的而不是應用程序本身定義的,創建AOP時候為true,程序設置 */ private boolean synthetic = false; /** * 定義這個bean的應用,APPLICATION:用戶,INFRASTRUCTURE:完全內部使用,與用戶無關, * SUPPORT:某些復雜配置的一部分 * 程序設置 */ private int role = BeanDefinition.ROLE_APPLICATION; /** * bean的描述信息 */ @Nullable private String description; /** * 這個bean定義的資源 */ @Nullable private Resource resource; }

Spring源碼分析(八)AbstractBeanDefinition屬性