天天看点

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