关于springboot:深入学习-Spring-Web-开发-BeanDefinition上

0次阅读

共计 4837 个字符,预计需要花费 13 分钟才能阅读完成。

本文次要探讨什么是 BeanDefinition,下一篇文章,咱们将探讨 BeanDefinition 是如何起作用的。

BeanDefinition 是什么

上面,咱们先看一下 BeanDefinition 的源码:

public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {

    String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON;
    String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;

    int ROLE_APPLICATION = 0;
    int ROLE_SUPPORT = 1;
    int ROLE_INFRASTRUCTURE = 2;

    void setParentName(@Nullable String parentName);

    @Nullable
    String getParentName();

    void setBeanClassName(@Nullable String beanClassName);

    @Nullable
    String getBeanClassName();

    void setScope(@Nullable String scope);

    @Nullable
    String getScope();

    void setLazyInit(boolean lazyInit);

    boolean isLazyInit();

    void setDependsOn(@Nullable String... dependsOn);

    @Nullable
    String[] getDependsOn();

    void setAutowireCandidate(boolean autowireCandidate);

    boolean isAutowireCandidate();

    void setPrimary(boolean primary);

    boolean isPrimary();

    void setFactoryBeanName(@Nullable String factoryBeanName);

    @Nullable
    String getFactoryBeanName();

    void setFactoryMethodName(@Nullable String factoryMethodName);

    @Nullable
    String getFactoryMethodName();

    ConstructorArgumentValues getConstructorArgumentValues();

    default boolean hasConstructorArgumentValues() {return !getConstructorArgumentValues().isEmpty();}

    MutablePropertyValues getPropertyValues();

    default boolean hasPropertyValues() {return !getPropertyValues().isEmpty();}

    void setInitMethodName(@Nullable String initMethodName);

    @Nullable
    String getInitMethodName();

    void setDestroyMethodName(@Nullable String destroyMethodName);

    @Nullable
    String getDestroyMethodName();

    void setRole(int role);

    int getRole();

    void setDescription(@Nullable String description);

    @Nullable
    String getDescription();

    ResolvableType getResolvableType();

    boolean isSingleton();

    boolean isPrototype();

    boolean isAbstract();

    @Nullable
    String getResourceDescription();

    @Nullable
    BeanDefinition getOriginatingBeanDefinition();}

能够看到 BeanDefinition 是一个接口,它继承了 AttributeAccessor 和 BeanMetadataElement,其中 AttributeAccessor 的源码如下:

public interface AttributeAccessor {void setAttribute(String name, @Nullable Object value);

    @Nullable
    Object getAttribute(String name);

    @SuppressWarnings("unchecked")
    default <T> T computeAttribute(String name, Function<String, T> computeFunction) {Assert.notNull(name, "Name must not be null");
        Assert.notNull(computeFunction, "Compute function must not be null");
        Object value = getAttribute(name);
        if (value == null) {value = computeFunction.apply(name);
            Assert.state(value != null,
                    () -> String.format("Compute function must not return null for attribute named'%s'", name));
            setAttribute(name, value);
        }
        return (T) value;
    }

    @Nullable
    Object removeAttribute(String name);

    boolean hasAttribute(String name);

    String[] attributeNames();

}

AttributeAccessor 次要提供了拜访和批改属性的办法。

须要留神的是,这里的属性是额定加给 BeanDefinition 的属性,而不是它所代表的对象自身的属性。

BeanMetadataElement 的源码如下:

public interface BeanMetadataElement {

    @Nullable
    default Object getSource() {return null;}

}

BeanMetadataElement 只提供了一个办法,它是用来获取 Bean 元数据的配置源的(个别指向 Bean 对象的 class 文件的磁盘目录)。

理解完这两个接口,咱们再看回到 BeanDefinition 的源码,其中蕴含了几个动态变量:

    String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON;
    String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;

    int ROLE_APPLICATION = 0;
    int ROLE_SUPPORT = 1;
    int ROLE_INFRASTRUCTURE = 2;

SCOPE_SINGLETON、SCOPE_PROTOTYPE 用于示意对象是单例还是多例的,ROLE_ 结尾的几个常量跟 ComponentDefinition 无关,具体的作用在此不做剖析,有趣味的读者能够自行钻研。

接下来,咱们能够看到上面的办法,这些办法跟咱们如何定义这个 BeanDefinition 无关,咱们后面讲到的用来定义 Bean 个性的一些内容,如:@Scope、@Lazy、@DependsOn、@Primary、initMethod、destroyMethod、是否是候选类等,在这里都能够看到。

    void setScope(@Nullable String scope);

    @Nullable
    String getScope();

    void setLazyInit(boolean lazyInit);

    boolean isLazyInit();

    void setDependsOn(@Nullable String... dependsOn);

    @Nullable
    String[] getDependsOn();

    void setAutowireCandidate(boolean autowireCandidate);

    boolean isAutowireCandidate();

    void setPrimary(boolean primary);

    boolean isPrimary();

    void setInitMethodName(@Nullable String initMethodName);

    @Nullable
    String getInitMethodName();

    void setDestroyMethodName(@Nullable String destroyMethodName);

    @Nullable
    String getDestroyMethodName();

    boolean isSingleton();

    boolean isPrototype();

再接着,咱们看到上面的办法,基本上是一些辅助性办法和对 Bean 对象自身的根本内容进行治理的办法(如:对类名、构造函数参数值、属性值等进行治理的办法)。

    void setParentName(@Nullable String parentName);

    @Nullable
    String getParentName();

    void setBeanClassName(@Nullable String beanClassName);

    @Nullable
    String getBeanClassName();

    void setFactoryBeanName(@Nullable String factoryBeanName);

    @Nullable
    String getFactoryBeanName();

    void setFactoryMethodName(@Nullable String factoryMethodName);

    @Nullable
    String getFactoryMethodName();

    ConstructorArgumentValues getConstructorArgumentValues();

    default boolean hasConstructorArgumentValues() {return !getConstructorArgumentValues().isEmpty();}

    MutablePropertyValues getPropertyValues();

    default boolean hasPropertyValues() {return !getPropertyValues().isEmpty();}

    void setDescription(@Nullable String description);

    @Nullable
    String getDescription();

    void setRole(int role);

    int getRole();

    ResolvableType getResolvableType();

    boolean isAbstract();

    @Nullable
    String getResourceDescription();

    @Nullable
    BeanDefinition getOriginatingBeanDefinition();

总结

本文,咱们简略介绍了 BeanDefinition 的根本内容,下一篇文章,咱们将探讨它是如何起作用的。

正文完
 0