定义
BeanDefinition接口继承了AttributeAccessor, BeanMetadataElement两个接口,其主要的作用是实现将Bean的定义转换为java对象,是对Bean定义的一个抽象,其中的各种属性对应了xml中的各种配置信息。
类依赖
Spring中最常用的就三个具体的实现类,分类是:RootBeanDefinition,ChildBeanDefinition,GenericBeanDefinition
RootBeanDefinition:最常用的类,用于标定bean的定义。
ChildBeanDefinition:与RootBeanDefinition必定一块出现,表示一个父bean下面有子bean,xml中配置如下:
<bean id="a" class="spring_study_aop.a" init-method="init" ></bean>
<bean id="b" class="spring_study_aop.b" init-method="init" parent="a"></bean>,目前的这种方式已经被GenericBeanDefinition替换了
GenericBeanDefinition: RootBeanDefinition+ ChildBeanDefinition= GenericBeanDefinition
代码以及解析
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. * * <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. * * @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 {
/** * 标记这个bean的作用域是单例 ,可以通过xml中scope属性来设置 */ String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON;
/** * 标记这个bean的作用域是单例 ,可以通过xml中scope属性来设置 */ String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;
/** * bean的角色定义,默认,为应用程序定义。 */ int ROLE_APPLICATION = 0;
/** * bean的角色定义,为应用程序定义的比较大的对象。 */ int ROLE_SUPPORT = 1;
/** * spring内部定义的bean */ int ROLE_INFRASTRUCTURE = 2;
// Modifiable attributes
/** * Set the name of the parent definition of this bean definition, if any. */ void setParentName(@Nullable String parentName);
/** *获取父类bean的名称,父类bean可以通过parent属性来设置父bean. */ @Nullable String getParentName();
/** * Specify the bean class name of this bean definition. */ void setBeanClassName(@Nullable String beanClassName);
/** * Return the current bean class name of this bean definition. * <p>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 <i>not</i> 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();
/** * xml中scope来控制,默认sington */ 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();
/** *xml中lazy-init来控制,默认false */ 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();
/** *xml中depends-on来控制 ,多个以逗号隔开 */ void setDependsOn(@Nullable String... dependsOn);
/** * Return the bean names that this bean depends on. */ @Nullable String[] getDependsOn();
/** *设置该对象是否可以被其他对象自动装配。 */ void setAutowireCandidate(boolean autowireCandidate);
/** * Return whether this bean is a candidate for getting autowired into some other bean. */ boolean isAutowireCandidate();
/** * xml中的primary属性来控制,当A类型的bean需要注入B类型的实现类,并且B类型的实现类有多个,在按类型将B的实现类注入到A中时,优先注入该属性为true的实现类,当然如果同一个类的实现类有多个primary为true,则抛出异常。 */ void setPrimary(boolean primary);
/** * Return whether this bean is a primary autowire candidate. */ boolean isPrimary();
/** * 当bean的创建方式是以工厂进行创建的时候,该方法设置工厂的名称 * 例如通过一下定义来创建bean,<bean id="AppleFactory" class="cn.lee.factory.AppleFactory" factory-method="newInstance"> <constructor-arg index="0" value="red"></constructor-arg> </bean> */ void setFactoryBeanName(@Nullable String factoryBeanName);
/** * Return the factory bean name, if any. */ @Nullable String getFactoryBeanName();
/** * 工厂创建bean时,设置创建bean的方法名称 * @see #setBeanClassName */ void setFactoryMethodName(@Nullable String factoryMethodName);
/** * Return a factory method, if any. */ @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();
/** * Return if there are constructor argument values defined for this bean. * @since 5.0.2 */ default boolean hasConstructorArgumentValues() { return !getConstructorArgumentValues().isEmpty(); }
/** * 获取类的属性和属性值的类PropertyValue */ MutablePropertyValues getPropertyValues();
/** * Return if there are property values values defined for this bean. * @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. * @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,设置为true的时候,该bean的不会被实例化。xml可以通过abstract属性来控制,默认false,一般与parent一起使用,设置abstract的bean定义不需要创建实例,仅仅作为parent来进行一些通用的配置,后面的bean通过设置parent来获取相应的配置信息,从而达到简化配置的目的 */ boolean isAbstract();
/** * 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();
/** * Return a human-readable description of this bean definition. */ @Nullable String getDescription();
/** * 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. */ @Nullable BeanDefinition getOriginatingBeanDefinition();
} |