spring源码阅读(5.1.0版本)——BeanDefinition

目录

前言

什么是BeanDefinition

BeanDefinition的继承体系

BeanDefinition源码


前言

spring,对不起,我不知道你这么强.jpg(跪地)

 

什么是BeanDefinition

BeanDefinition用于保存Bean的相关信息,是生产Bean的原材料,BeanDefinition是一个顶端接口,在此基础上,衍生出了保存特定Bean的BeanDefinition

 

BeanDefinition的继承体系

spring源码阅读(5.1.0版本)——BeanDefinition_第1张图片

GenericBeanDefinition:除了指定类、可选的构造函数参数值和属性参数外,还可以通过parenetName属性灵活设置父类的类全限定名

RootBeanDefinition:在注册Bean阶段最常使用的就是RootBeanDefinition,它对应元素标签,在配置文件中可以定义父和子,父bean可以用RootBeanDefinition,子Bean可以用ChildBeanDefinition,从spring2.5开始,对于子bean,更常使用GenericBeanDefinition

AnnotatedGenericBeanDefinition:以@Configuration注解标记的类会解析为AnnotatedGenericBeanDefintion

ConfigurationClassBeanDefinition:以@Bean注解标记的方法对应的Bean会解析为ConfigurationClassBeanDefinition

ScannedGenericBeanDefinition:以@Component、@Service、@Controller注解标记的类会解析为ScannedGenericBeanDefinition

 

BeanDefinition源码

BeanDefinition继承了两个接口

  1. AttributeAccessor:这个接口定义了对对象的元数据(我的理解是未加工的数据,例如直接从xml文件中解析得到的数据)进行操作的基本方法(设置值、获取值),定义的方法如下:
    //将name定义的属性设置为提供的value值。如果value的值为null,则该属性为{@link #removeAttribute removed}。
    //通常,用户应该注意通过使用完全限定的名称(可能使用类或包名称作为前缀)来防止与其他元数据属性重叠。
    void setAttribute(String name, Object value);
    //获取标识为name的属性的值。
    Object getAttribute(String name);
    //删除标识为name的属性,并返回属性值
    Object removeAttribute(String name);
    //如果名为name的属性是否存在,存在返回true,否则返回false。
    boolean hasAttribute(String name);
    //返回所有属性的名称。
    String[] attributeNames();

     

  2. BeanMetadataElement:返回配置源对象,可以返回null:
    	/**
    	 * Return the configuration source {@code Object} for this metadata element
    	 * (may be {@code null}).
    	 */
    	@Nullable
    	Object getSource();

     

源码(不翻译了):

public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {

	/**
	 * Scope identifier for the standard singleton scope: "singleton".
	 * 

Note that extended bean factories might support further scopes. * @see #setScope */ //单例的标识符 String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON; /** * Scope identifier for the standard prototype scope: "prototype". *

Note that extended bean factories might support further scopes. * @see #setScope */ //原型的标识符 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的角色,用户定义的Bean int ROLE_APPLICATION = 0; /** * 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的角色,Bean来自配置文件 int ROLE_SUPPORT = 1; /** * 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的角色,Bean来自spring int ROLE_INFRASTRUCTURE = 2; /** * Set the name of the parent definition of this bean definition, if any. */ void setParentName(@Nullable String parentName); /** * Return the name of the parent definition of this bean definition, if any. */ @Nullable String getParentName(); /** * Specify the bean class name of this bean definition. *

The class name can be modified during bean factory post-processing, * typically replacing the original class name with a parsed variant of it. * @see #setParentName * @see #setFactoryBeanName * @see #setFactoryMethodName */ void setBeanClassName(@Nullable String beanClassName); /** * Return the current bean class name of this bean definition. *

Note that this does not have to be the actual class name used at runtime, in * case of a child definition overriding/inheriting the class name from its parent. * Also, this may just be the class that a factory method is called on, or it may * even be empty in case of a factory bean reference that a method is called on. * Hence, do not consider this to be the definitive bean type at runtime but * rather only use it for parsing purposes at the individual bean definition level. * @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(); /** * Set whether this bean should be lazily initialized. *

If {@code false}, the bean will get instantiated on startup by bean * factories that perform eager initialization of singletons. */ void setLazyInit(boolean lazyInit); /** * Return whether this bean should be lazily initialized, i.e. not * eagerly instantiated on startup. Only applicable to a singleton bean. */ boolean isLazyInit(); /** * Set the names of the beans that this bean depends on being initialized. * The bean factory will guarantee that these beans get initialized first. */ void setDependsOn(@Nullable String... dependsOn); /** * Return the bean names that this bean depends on. */ @Nullable String[] getDependsOn(); /** * Set whether this bean is a candidate for getting autowired into some other bean. *

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. */ void setAutowireCandidate(boolean autowireCandidate); /** * Return whether this bean is a candidate for getting autowired into some other bean. */ boolean isAutowireCandidate(); /** * Set whether this bean is a primary autowire candidate. *

If this value is {@code true} for exactly one bean among multiple * matching candidates, it will serve as a tie-breaker. */ void setPrimary(boolean primary); /** * Return whether this bean is a primary autowire candidate. */ boolean isPrimary(); /** * Specify the factory bean to use, if any. * This the name of the bean to call the specified factory method on. * @see #setFactoryMethodName */ void setFactoryBeanName(@Nullable String factoryBeanName); /** * Return the factory bean name, if any. */ @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. * @see #setFactoryBeanName * @see #setBeanClassName */ void setFactoryMethodName(@Nullable String factoryMethodName); /** * Return a factory method, if any. */ @Nullable String getFactoryMethodName(); /** * Return the constructor argument values for this bean. *

The returned instance can be modified during bean factory post-processing. * @return the ConstructorArgumentValues object (never {@code null}) */ ConstructorArgumentValues getConstructorArgumentValues(); /** * Return if there are constructor argument values defined for this bean. * @since 5.0.2 */ default boolean hasConstructorArgumentValues() { return !getConstructorArgumentValues().isEmpty(); } /** * Return the property values to be applied to a new instance of the bean. *

The returned instance can be modified during bean factory post-processing. * @return the MutablePropertyValues object (never {@code null}) */ //来自.properties文件中的键值对 MutablePropertyValues getPropertyValues(); /** * Return if there are property values values defined for this bean. * @since 5.0.2 */ default boolean hasPropertyValues() { return !getPropertyValues().isEmpty(); } /** * Set the name of the initializer method. * @since 5.1 */ void setInitMethodName(@Nullable String initMethodName); /** * Return the name of the initializer method. * @since 5.1 */ @Nullable String getInitMethodName(); /** * Set the name of the destroy method. * @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(); /** * Set a human-readable description of this bean definition. * @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 Singleton, with a single, shared instance * returned on all calls. * @see #SCOPE_SINGLETON */ boolean isSingleton(); /** * Return whether this a Prototype, with an independent instance * returned for each call. * @since 3.0 * @see #SCOPE_PROTOTYPE */ boolean isPrototype(); /** * Return whether this bean is "abstract", that is, not meant to be instantiated. */ 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. *

Note that this method returns the immediate originator. Iterate through the * originator chain to find the original BeanDefinition as defined by the user. */ @Nullable BeanDefinition getOriginatingBeanDefinition(); }

总结一下,BeanDefinition定义了如下信息:

1、对对象元数据进行操作的方法(设置与获取属性)

2、描述对象画像的操作(一系列的get、set、is方法)

3、定义Bean的角色常量

BeanDefinition只是定义了一系列的操作,描述Bean画像的相关属性交给了子类AbstractBeanDefinition,这个子类实现了BeanDefinition定义的一系列操作

 

如有错误,欢迎指出

你可能感兴趣的:(spring)