BeanDefinition接口定義詳解

BeanDefinition接口定義詳解

public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {

    /**
     * 這兩個參數應該一看就明白了,singleton和prototype,這個可以看出spring默認提供這兩種模式
     * @see #setScope
     */
    String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON;
    String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;
/**
* Role hint indicating that a {@code BeanDefinition} is a major part
* of the application. Typically corresponds to a user-defined bean.
* 表示一個應用的主要組成部分,比如一個用戶定義的bean。
*/
int ROLE_APPLICATION = 0;
/**
- Role hint indicating that a {@code BeanDefinition} is a supporting
- part of some larger configuration, typically an outer
- {@link org.springframework.beans.factory.parsing.ComponentDefinition}.
- {@code SUPPORT} beans are considered important enough to be aware
- of when looking more closely at a particular
- {@link org.springframework.beans.factory.parsing.ComponentDefinition},
- but not when looking at the overall configuration of an application.
- 表示一個支持一些比較大的配置的bean定義,如一個外部的組件定義
*/
int ROLE_SUPPORT = 1;
/**
- Role hint indicating that a {@code BeanDefinition} is providing an
- entirely background role and has no relevance to the end-user. This hint is
- used when registering beans that are completely part of the internal workings
- of a {@link org.springframework.beans.factory.parsing.ComponentDefinition}.
- 表示一個內部使用的註冊的bean組件定義,與終端用戶無關。
*/
int ROLE_INFRASTRUCTURE = 2;

//attributes元素

/**

- 設置bean定義的父name,如果有的話。
*/
void setParentName(@Nullable String parentName);
/**
 * 獲取bean定義的父name,如果有的話
 */
@Nullable
String getParentName();

/**
 * Specify the bean class name of this bean definition.
 * <p>The class name can be modified during bean factory post-processing,
 * typically replacing the original class name with a parsed variant of it.
 * 設置bean定義的bean class name。在bean工廠後處理的過程中,類名可以被修改,典型的情況下
 * 將原始class的name,替換成一個可解析的變量。
 * @see #setParentName
 * @see #setFactoryBeanName
 * @see #setFactoryMethodName
 */
void setBeanClassName(@Nullable String beanClassName);

/**
* 獲取bean的class name 對應class的:class="spring.service.impl.MessageServiceImpl"
 */
@Nullable
String getBeanClassName();

/**
 * 重寫bean定義的作用域,對應xml中的 scope="session"
 * @see #SCOPE_SINGLETON
 * @see #SCOPE_PROTOTYPE
 */
void setScope(@Nullable String scope);

/**
 * 獲取bean的作用域 
 * or {@code null} if not known yet.
 */
@Nullable
String getScope();

/**
 * 設置懶加載 對應xml中的 : lazy-init="true"
 */
void setLazyInit(boolean lazyInit);

/**
 * 返回bean是否爲懶加載模式,不能用於在啓動過程中的懶加載初始化,僅僅可以用於單例bean。
 */
boolean isLazyInit();

/**
 * 設置此bean依賴於初始化的bean的名稱。
 * bean factory將確保這些bean首先被初始化。
 */
void setDependsOn(@Nullable String... dependsOn);

/**
 * 返回這些依賴的bean的名稱
 */
@Nullable
String[] getDependsOn();

/**
 * Set whether this bean is a candidate for getting autowired into some other bean.
 * <p>Note that this flag is designed to only affect type-based autowiring.
 * It does not affect explicit references by name, which will get resolved even
 * if the specified bean is not marked as an autowire candidate. As a consequence,
 * autowiring by name will nevertheless inject a bean if the name matches.
 * 設置該 Bean 是否可以注入到其他 Bean 中,只對根據類型注入有效
 * 如果根據名稱注入,即使這邊設置了 false,也是可以的
 */
void setAutowireCandidate(boolean autowireCandidate);

/**
 * Return whether this bean is a candidate for getting autowired into some other bean.
 *判斷bean是否可以作爲其他bean的自動注入對象
 */
boolean isAutowireCandidate();

/**
 * Set whether this bean is a primary autowire candidate.
 * <p>If this value is {@code true} for exactly one bean among multiple
 * matching candidates, it will serve as a tie-breaker.
 *設置bean是否爲主要的自動注入候選者。如果多個類型匹配返回true,則以primary bean注入。
 */
void setPrimary(boolean primary);

/**
 *返回該bean是否是primary
 */
boolean isPrimary();

/**
 * Specify the factory bean to use, if any.
 * This the name of the bean to call the specified factory method on.
 *設置工廠bean的name,bean的name可以被工廠方法使用。
 * @see #setFactoryMethodName
 */
void setFactoryBeanName(@Nullable String factoryBeanName);

/**
 *返回bean的name 如果有的話
 */
@Nullable
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.
 * 設置工廠bean方法。此方法調用將會使用構造參數,如果沒有方法,則沒有參數。
 * 此方法將會被特定的工廠bean調用,或者本地bean類型的靜態方法調用。
 * @see #setFactoryBeanName
 * @see #setBeanClassName
 */
void setFactoryMethodName(@Nullable String factoryMethodName);

/**
 * 返回工廠bena方法的name
 */
@Nullable
String getFactoryMethodName();

/**
 * Return the constructor argument values for this bean.
 * <p>The returned instance can be modified during bean factory post-processing.
 * @return the ConstructorArgumentValues object (never {@code null})
 * 獲取構造器中參數
 */
ConstructorArgumentValues getConstructorArgumentValues();

/**
 *如果這個bean定義的構造器有參數值,則返回true。
 * @since 5.0.2
 */
default boolean hasConstructorArgumentValues() {
    return !getConstructorArgumentValues().isEmpty();
}

/**
 * Return the property values to be applied to a new instance of the bean.
 * <p>The returned instance can be modified during bean factory post-processing.
 * @return the MutablePropertyValues object (never {@code null})
 * 返回屬性值,對應xml中的
 */
MutablePropertyValues getPropertyValues();

/**
 * 如果定義的bean有屬性值,則返回ture
 * @since 5.0.2
 */
default boolean hasPropertyValues() {
    return !getPropertyValues().isEmpty();
}

    // Read-only attributes

    /**
     * Return whether this a <b>Singleton</b>, with a single, shared instance
     * returned on all calls.
     * 返回作用域是否是Singleton
     * @see #SCOPE_SINGLETON
     */
    boolean isSingleton();

    /**
     * 返回作用域是否是Prototype
     * @since 3.0
     * @see #SCOPE_PROTOTYPE
     */
    boolean isPrototype();

    /**
     * 返回是否該bean是“抽象的”,也就是說,不需要實例化。
     */
    boolean isAbstract();

    /**
     * 獲取bean定義的角色。
     * @see #ROLE_APPLICATION
     * @see #ROLE_SUPPORT
     * @see #ROLE_INFRASTRUCTURE
     */
    int getRole();

    /**
     * 返回bean定義的描述
     */
    @Nullable
    String getDescription();

    /**
     * 返回bean定義資源的描述
     */
    @Nullable
    String getResourceDescription();

    /**
     * Return the originating BeanDefinition, or {@code null} if none.
     * Allows for retrieving the decorated bean definition, if any.
     * <p>Note that this method returns the immediate originator. Iterate through the
     * originator chain to find the original BeanDefinition as defined by the user.
     * 返回原始的bean定義,如果沒有,則爲null。
     * 此方法將返回即時的originator,通過迭代originator鏈,可以獲取用戶定義的原始bean定義。
     */
    @Nullable
    BeanDefinition getOriginatingBeanDefinition();

BeanDefinition接口用於描述一個bean實例的屬性及構造參數等元數據;主要提供了父beanname,bean類型名,作用域,懶加載, bean依賴,自動注入候選bean,自動注入候選主要bean熟悉的設置與獲取操作。同時提供了判斷bean是否爲單例、原型模式、抽象bean的操作,及獲取bean的描述,資源描述,屬性源,構造參數,原始bean定義等操作。

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