Spring应用手册-BeanDefinition接口

x33g5p2x  于2021-09-26 转载在 Spring  
字(2.1k)|赞(0)|评价(0)|浏览(355)

BeanDefinition接口

spring应用手册(第五部分)

要了解BeanDefinition接口,你必须了解springBean的加载流程。

springBean加载流程:

①spring会加载所有符合要求的class(就是我们配置或者注解的类)。

②解析这些类相关信息

③根据解析的信息生成注册信息。这里的注册信息就是封装在BeanDefinition对象中。

④根据bean的scope属性进行实例化。

⑤执行其他的接口,比如Aware等等。

所以BeanDefinition就是来分装springBean的注册信息的,spring生成的springBean就是根据BeanDefinition对象生成的。

看看BeanDefinition源码:

package org.springframework.beans.factory.config;

public interface BeanDefinition extends AttributeAccessor,BeanMetadataElement {
    String SCOPE_SINGLETON = "singleton";
    String SCOPE_PROTOTYPE = "prototype";
    int ROLE_APPLICATION = 0;
    int ROLE_SUPPORT = 1;
    int ROLE_INFRASTRUCTURE = 2;
	//parent属性的值
    void setParentName(@Nullable String s);

    @Nullable
   	String getParentName();
	//实际的类的类名
    void setBeanClassName(@Nullable String s);

    @Nullable
   	String getBeanClassName();
	//当前类scope
    void setScope(@Nullable String s);

    @Nullable
    String getScope();
	//是否延迟加载属性
    void setLazyInit(boolean b);

    boolean isLazyInit();
	//dependsOn 属性值
    void setDependsOn(@Nullable String... strings);

    @Nullable
    String[] getDependsOn();
	//自动注入是否忽略属性值
    void setAutowireCandidate(boolean b);

    boolean isAutowireCandidate();
	//自动注入时是否优先选择属性
    void setPrimary(boolean b);

    boolean isPrimary();
	//factoryBean的名字配置(工厂方式生成Bean的时候使用)
    void setFactoryBeanName(@Nullable String s);

    @Nullable
    String getFactoryBeanName();
	//factoryMethod的名字配置(工厂方式生成Bean的时候使用)
    void setFactoryMethodName(@Nullable String s);

    @Nullable
    String getFactoryMethodName();
	//构造方法参数
    ConstructorArgumentValues getConstructorArgumentValues();

    default boolean hasConstructorArgumentValues() { /* compiled code */ }

    MutablePropertyValues getPropertyValues();

    default boolean hasPropertyValues() { /* compiled code */ }
	//初始化方法配置
    void setInitMethodName(@Nullable String s);

    @Nullable
    String getInitMethodName();
	//资源释放方法名称
    void setDestroyMethodName(@Nullable String s);

    @Nullable
    String getDestroyMethodName();
	
    void setRole(int i);

    int getRole();
	//描述
    void setDescription(@Nullable String s);

    @Nullable
    String getDescription();
	//是否单利
    boolean isSingleton();
	//是否多利
    boolean isPrototype();
	//是否抽象
    boolean isAbstract();
	//资源描述
    @Nullable
    String getResourceDescription();
	//获取原始的BeanDefinition对象
    @Nullable
    BeanDefinition getOriginatingBeanDefinition();
}

源码中重要的属性我已经在代码中进行注释,从中我们可以发现BeanDefintion中几乎包含了所有我们配置bean的所有属性。

以上就是BeanDefintion的简介。

相关文章

微信公众号

最新文章

更多