spring源码学习笔记之BeanDefinition接口

package org.springframework.beans.factory.config;

import org.springframework.beans.BeanMetadataElement;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.core.AttributeAccessor;
import org.springframework.lang.Nullable;

/**
 * A BeanDefinition describes a bean instance, which has property values,
 * constructor argument values, and further information supplied by
 * concrete implementations.
 
 * 一个 BeanDefinition 描述了一个bean实例的属性值,构造参数值,它的具体实现还会描述更多的信息。
   可以理解为: BeanDefinition主要是用于描述bean实例信息)
 * <p>This is just a minimal interface: The main intention is to allow a
 * {@link BeanFactoryPostProcessor} such as {@link PropertyPlaceholderConfigurer}
 * to introspect and modify property values and other bean metadata.
 *
 * BeanDefinition是最小的接口。
 * 可以使用BeanFactoryPostProcessor(例如PropertyPlaceholderConfigurer)去检测和修改bean实例的属性值或者它的元数据信息
 *
 * @author Juergen Hoeller
 * @author Rob Harrop
 * @since 19.03.2004
 * @see ConfigurableListableBeanFactory#getBeanDefinition
 * @see org.springframework.beans.factory.support.RootBeanDefinition
 * @see org.springframework.beans.factory.support.ChildBeanDefinition
 */
public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {

    
     //单例标识符, 可以使用setScope方法修改作用域
    String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON;

    // 多例标示符,可以使用setScope方法修改作用域
    String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;


    //表示该BeanDefinition的角色是应用角色,说白了就是程序员注入到IOC容器中的bean
    int ROLE_APPLICATION = 0;

    //没读懂。。。
    int ROLE_SUPPORT = 1;

    // 应该是框架自带的bean,反正不是程序员手动注入的bean
    int ROLE_INFRASTRUCTURE = 2;


    // 可更改的属性设置

    /**
     * 给当前beanDefinition设置一个parent BeanDefinition
     */
    void setParentName(@Nullable String parentName);

    /**
     *返回当前beanDefinition的parent BeanDefinition的名字
     */
    @Nullable
    String getParentName();

    /**
     * 设置当前bean definition的class类名。
     * 可以使用BeanFactoryPostProcessor这个接口去修改class类名
     * @see #setParentName
     * @see #setFactoryBeanName
     * @see #setFactoryMethodName
     */
    void setBeanClassName(@Nullable String beanClassName);

    /**
     * 返回当前bean definition的类名。
     * 不过,需要注意的是,这个类名并非程序运行时的真正类名,比如有可能是覆写或者说是继承自parent bean definiton的名字。
     * @see #getParentName()
     * @see #getFactoryBeanName()
     * @see #getFactoryMethodName()
     */
    @Nullable
    String getBeanClassName();

    /**
     * Override the target scope of this bean, specifying a new scope name.
     * @see #SCOPE_SINGLETON
     * @see #SCOPE_PROTOTYPE
     */
    void setScope(@Nullable String scope);

    /**
     * Return the name of the current target scope for this bean,
     * or {@code null} if not known yet.
     */
    @Nullable
    String getScope();

    /**
     * 设置是否懒加载,如果设置false,在程序启动的时候就是会实例化,也就是说不会懒加载 
     * 注意: 只有单例模式,也有是否懒加载一说呀
     */
    void setLazyInit(boolean lazyInit);


    boolean isLazyInit();

    /**
     * 设置当前bean 初始化时需要依赖那些bean的名字,而且bean工厂会优化初始化那些bean
     */
    void setDependsOn(@Nullable String... dependsOn);

    /**
     * 返回当前bean依赖的那些bean的名字(集合)
     */
    @Nullable
    String[] getDependsOn();

    /**
     * 设置当前bean是否可以autowire的方式注入到其它的bean中去
     * 注意:该设置仅对基于类型方式的注入有效;也就是说如果是基于名字的autowire注册该设置没啥用。
     */
    void setAutowireCandidate(boolean autowireCandidate);


    boolean isAutowireCandidate();

    /**
     * 设置该bean是否是自动注入的primary bean。 
     * 何为primary bean? 也就是相同注入条件下,该bean优先注入
     * 举个例子,某接口有两个实现,其中一个实现打了@Primary注册,那么在注入接口时,优先注入这个Primary类
     */
    void setPrimary(boolean primary);

    
    boolean isPrimary();

    /**
     * 指定当前bean的工厂类名,有啥用,不明白
     * @see #setFactoryMethodName
     */
    void setFactoryBeanName(@Nullable String factoryBeanName);

    /**
     * Return the factory bean name, if any.
     */
    @Nullable
    String getFactoryBeanName();

    /**
     * 指定一个工厂方法,在调用bean的有参构造器或者是无参构造器时会调用这个工厂方法。
     * 该方法将在指定的工厂bean(如果有的话)上调用,或者作为本地bean类的静态方法调用。
     * @see #setFactoryBeanName
     * @see #setBeanClassName
     */
    void setFactoryMethodName(@Nullable String factoryMethodName);

    /**
     * 如果有的话,就返回这个工厂方法
     */
    @Nullable
    String getFactoryMethodName();

    /**
     * 返回bean构造器参数值, 这些值可以在BeanFactoryPostProcessor中进行修改
     */
    ConstructorArgumentValues getConstructorArgumentValues();

    /**
     * Return if there are constructor argument values defined for this bean.
     * @since 5.0.2
     */
    default boolean hasConstructorArgumentValues() {
        return !getConstructorArgumentValues().isEmpty();
    }

    /**
     * 返回当前bean的属性值,而且这些值也可以在BeanFactoryPostProcessor中进行修改
     */
    MutablePropertyValues getPropertyValues();

    /**
     * Return if there are property values values defined for this bean.
     * @since 5.0.2
     */
    default boolean hasPropertyValues() {
        return !getPropertyValues().isEmpty();
    }

    /**
     * 设置初始化方法名称 
     * @since 5.1
     */
    void setInitMethodName(@Nullable String initMethodName);

    /**
     * Return the name of the initializer method.
     * @since 5.1
     */
    @Nullable
    String getInitMethodName();

    /**
     * 设置销毁这个bean的方法名称 
     * @since 5.1
     */
    void setDestroyMethodName(@Nullable String destroyMethodName);

    /**
     * Return the name of the destroy method.
     * @since 5.1
     */
    @Nullable
    String getDestroyMethodName();

    /**
     * Set the role hint for this {@code BeanDefinition}. The role hint
     * provides the frameworks as well as tools with an indication of
     * the role and importance of a particular {@code BeanDefinition}.
     * @since 5.1
     * @see #ROLE_APPLICATION
     * @see #ROLE_SUPPORT
     * @see #ROLE_INFRASTRUCTURE
     */
    void setRole(int role);

    /**
     * Get the role hint for this {@code BeanDefinition}. The role hint
     * provides the frameworks as well as tools with an indication of
     * the role and importance of a particular {@code BeanDefinition}.
     * @see #ROLE_APPLICATION
     * @see #ROLE_SUPPORT
     * @see #ROLE_INFRASTRUCTURE
     */
    int getRole();

    /**
     * 不重要,设置当前bean的描述信息
     * @since 5.1
     */
    void setDescription(@Nullable String description);

    /**
     * Return a human-readable description of this bean definition.
     */
    @Nullable
    String getDescription();


    // Read-only attributes

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

    /**
     * Return whether this a <b>Prototype</b>, with an independent instance
     * returned for each call.
     * @since 3.0
     * @see #SCOPE_PROTOTYPE
     */
    boolean isPrototype();

    /**
     * 当前bean是否是个abstract抽象的bean ,如果是,不实例化
     */
    boolean isAbstract();

    /**
     * Return a description of the resource that this bean definition
     * came from (for the purpose of showing context in case of errors).
     */
    @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的起源bean definition, 如果没有,就返回null.
     * 如果是装饰当前bean的bean definition,也行呀。 
     * 不懂。。。。。
     */
    @Nullable
    BeanDefinition getOriginatingBeanDefinition();

}

能力问题,大体翻译了一下BeanDefinition的注释,对其有个初略的了解。

未完待续。。。。

猜你喜欢

转载自www.cnblogs.com/z-qinfeng/p/12219880.html