天天看點

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 = ;
/**
- 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 = ;
/**
- 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 = ;

//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定義等操作。