spring的BeanDefinition

Bean模型定義

在這裏插入圖片描述
上的類中都是定義瞭解析bean的定義存儲的的dto模型

BeanMetadataElement

  • BeanMetadataElement中只有一個方法,用來獲取元數據元素的配置源對象:
     public interface BeanMetadataElement {
     	Object getSource(); // 可以從xml等
     } 
    

DefaultsDefinition

  • DefaultsDefinition 默認屬性接口 他的實現是 DocumentDefaultsDefinition 就是一個DTO
    public class DocumentDefaultsDefinition implements DefaultsDefinition {
       private String lazyInit;
       private String merge;
       private String autowire;
       private String dependencyCheck;
       private String autowireCandidates;
       private String initMethod;
       private String destroyMethod;
       private Object source;
    }	
    

AliasDefinition

AliasDefinition 別名DTO

ImportDefinition

ImportDefinition 解析import

MethodOverride

MethodOverride 存儲 method-override
ReplaceOverride 標籤 replace-method
LookupOverride 標籤lookup-method @Lookup

BeanDefinition

在這裏插入圖片描述

  • AttributeAccessor 屬性訪問者提供了訪問屬性的能力
    public interface AttributeAccessor {
     void setAttribute(String name, Object value);
     Object getAttribute(String name);
     Object removeAttribute(String name);
     boolean hasAttribute(String name);
     String[] attributeNames();
    }
    
  • AttributeAccessorSupport 抽象類 屬性訪問器加強 ,有private final Map<String, Object> attributes = new LinkedHashMap<String, Object>(0);可以存取屬性

組合ComponentDefinition
AbstractComponentDefinition
CompositeComponentDefinitionBeanComponentDefinition`

BeanDefinition

public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {
	/**singleton和prototype 常量定義 spring默認提供這兩種模式*/
	String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON;
	String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;
	/** Bean 的角色   可通過 @Role 定義*/
	// 表示一個應用主要的組成部分 用戶定義的bean 
	int ROLE_APPLICATION = 0;
	// 表示一個支持一些較大的配置的bean定義,如一個外部的組件定義
	int ROLE_SUPPORT = 1;
	// 表示一個內部使用的註冊的bean組件定義,與終端用戶無關
	int ROLE_INFRASTRUCTURE = 2;
	// Modifiable attributes 屬性元素
	// 設置(獲取)bean定義的父name,如果有的話
	void setParentName(String parentName);
	String getParentName();
	// 設置bean定義的bean class name。在bean工廠後處理的過程中,類名可以被修改,典型的情況下
 	// 將原始class的name,替換成一個可解析的變量。
	void setBeanClassName(String beanClassName);
	/**
* 獲取bean的class name 對應class的:class="spring.service.impl.MessageServiceImpl"
 */
	String getBeanClassName();
 	
 	// bean 的作用於 可用scope標籤 @Scope指定 
	void setScope(String scope);
	String getScope();
	/**
	 * 設置懶加載 對應xml中的 : lazy-init="true" @Lazy
	 */
	void setLazyInit(boolean lazyInit);
	boolean isLazyInit();
	/**
	 * 設置此bean依賴於初始化的bean的名稱。
	 * bean factory將確保這些bean首先被初始化。@DependsOn
	 */
	void setDependsOn(String... dependsOn);
	String[] getDependsOn();
	// 設置該Bean是否可以注入其他Bean中,只對根據類型注入有效
	// 如果根據名稱注入,即使這邊設置false,也是可以的
	void setAutowireCandidate(boolean autowireCandidate);
	// 判斷bean是否可以做爲其他bean的自動注入對象
	// Return whether this bean is a candidate for getting autowired into some other bean.
	boolean isAutowireCandidate();
	// @Primary 設置bean是否爲主要的自動入候選者。
	// 如果多個類型匹配返回true,則以primary bean 注入
	void setPrimary(boolean primary);
	// 返回該bean是否是primary
	boolean isPrimary();
	// 設置工廠bean的name,bean的name可以被工廠方法使用
	void setFactoryBeanName(String factoryBeanName);
	// 返回factoryBean的name
	String getFactoryBeanName();
	/**
	 * Specify a factory method, if any. This method will be invoked with
	 * constructor arguments, or with no arguments if none are specified.
	 * The method will be invoked on the specified factory bean, if any,
	 * or otherwise as a static method on the local bean class.
	 * @see #setFactoryBeanName
	 * @see #setBeanClassName
	 */
	// 設置工廠bean方法。此方法調用將會使用構造參數,如果沒有方法,則沒有參數.
	// 此方法將會被特定的工廠bean調用,或者本地bean類型的靜態方法調用
	void setFactoryMethodName(String factoryMethodName);
	// 返回工廠bean方法的name
	String getFactoryMethodName();
	// 獲取構造器中的參數
	ConstructorArgumentValues getConstructorArgumentValues();
	/**
	 * Return if there are constructor argument values defined for this bean.
	 * @since 5.0.2
	 */
	// 如果這個bean定義的構造器有參數值,則返回true
	default boolean hasConstructorArgumentValues() {
		return !getConstructorArgumentValues().isEmpty();
	}
	// 返回屬性值
	MutablePropertyValues getPropertyValues();
	/**
	 * Return if there are property values values defined for this bean.
	 * @since 5.0.2
	 */
	 // 如果定義的bean有屬性值,則返回true
	default boolean hasPropertyValues() {
		return !getPropertyValues().isEmpty();
	}
	/**
	 * Set the name of the initializer method.
	 * @since 5.1
	 */
	void setInitMethodName(@Nullable String initMethodName);
		/**
	 * Return the name of the initializer method.
	 * @since 5.1
	 */
	@Nullable
	String getInitMethodName();
	/**
	 * Set the name of the destroy method.
	 * @since 5.1
	 */
	void setDestroyMethodName(@Nullable String destroyMethodName);
	/**
	 * Return the name of the destroy method.
	 * @since 5.1
	 */
	@Nullable
	String getDestroyMethodName();
	void setRole(int role)
	// @Role
	int getRole();
	/**
	 * Set a human-readable description of this bean definition.
	 * @since 5.1
	 */
	void setDescription(@Nullable String description);
	String getDescription();
	// Read-only attributes
	ResolvableType getResolvableType();
	// 是單例類型
	boolean isSingleton();
	// 是原型類型,返回true
	boolean isPrototype();
	// 是否是抽象類型
	boolean isAbstract();
	// 返回bean定義資源的描述
	String getResourceDescription();
	// 返回原始的bean定義,如果沒有,返回null
	// 此方法將返回即時的originator,通過迭代originator鏈,可以獲取用戶定義的原始bean定義
	BeanDefinition getOriginatingBeanDefinition();
}

BeanDefinition常見實現類

AnnotatedBeanDefinition:包含有註解的Bean定義。一般情況下,通過註解的方式得到Bean定義,類型都是該接口的實現類。

AnnotatedGenericBeanDefinition:有@Configuration註解的類,會生成AnnotatedGenericBeanDefinition類型的Bean定義。
ScannedGenericBeanDefinition:有@Component註解的類,會生成ScannedGenericBeanDefinition類型的Bean定義。注意其它繼承了@Component的註解,同@Component。

ConfigurationClassBeanDefinitionConfigurationClassBeanDefinitionReader中定義的類

RootBeanDefinition,ChildBeanDefinition,GenericBeanDefinition:這個類均繼承了AbstractBeanDefinition。從官方文檔裏,我們得到了以下一些信息:

RootBeanDefinition是最常用的實現類,它對應一般性的元素標籤,GenericBeanDefinition是自2.5以後新加入的bean文件配置屬性定義類,是一站式服務類。在配置文件中可以定義父和子,父用RootBeanDefinition表示,而子用ChildBeanDefiniton表示,而沒有父的就使用RootBeanDefinition表示。AbstractBeanDefinition對兩者共同的類信息進行抽象

BeanDefinitionBuilder

BeanDefinitionBuilder是Builder模式的應用。通過這個類我們可以方便的構建BeanDefinition的實例對象

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章