spring BeanDefinition详尽篇

BeanDefinition接口作为Spring中Bean定义的抽象,它包含了Bean的配置信息。RootBeanDefinition是最常见的实现,用于定义bean。ChildBeanDefinition表示父bean和子bean的关系,而GenericBeanDefinition综合了前两者的特点。本文将详细探讨BeanDefinition的实现和解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定义

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();

 

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值