BeanDefinition 是 Spring 中的一个核心概念,它用于描述一个 Bean 实例的属性和行为,并提供了创建和管理 Bean 实例的基础信息。
在 Spring 中,IOC 容器是负责管理 Bean 实例的创建、初始化和依赖注入等过程的核心组件。BeanDefinition 描述了一个 Bean 实例的基本信息,包括 Bean 的类型、属性、构造函数参数、初始化方法、销毁方法等。IOC 容器根据这些信息来创建和管理 Bean 实例,使得开发者无需手动创建和管理 Bean 实例,从而实现了控制反转。
在 Spring 中,AOP(面向切面编程)框架是实现横切关注点的重要方式之一。在 AOP 中,需要对目标对象进行代理,并在代理对象中增加横切逻辑。BeanDefinition 是实现 AOP 的基础,它定义了需要被代理的 Bean 实例以及代理所需的相关信息,从而实现了 AOP 功能。
在 Spring 中,很多功能都是通过扩展机制实现的。例如,Spring 提供了一系列的 BeanPostProcessor 和 BeanFactoryPostProcessor 接口,用于在 Bean 实例化和依赖注入过程中进行扩展。BeanDefinition 是实现扩展机制的基础,它提供了对 Bean 实例化和依赖注入过程的描述,从而使得开发者可以通过实现相应的接口来扩展 Spring 的功能。
- BeanDefinition
/**
* 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} to introspect and modify property values
* and other bean metadata.
*
public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {}
- setParentName(String parentName) 和 getParentName():设置和获取父 Bean 的名称。
/**
* 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();
- setBeanClassName(String beanClassName) 和 getBeanClassName():设置和获取 Bean 的完全限定类名。
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();
- setScope(String scope) 和 getScope():设置和获取 Bean 的作用域。
/**
* 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();
- setLazyInit(boolean lazyInit) 和 isLazyInit():设置和获取 Bean 是否懒加载。
/**
* Set whether this bean should be lazily initialized.
* <p>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();
- setDependsOn(String... dependsOn) 和 getDependsOn():设置和获取 Bean 的依赖关系,即在实例化该 Bean 之前需要先实例化的其他 Bean 名称。
/**
* 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();
- setAutowireCandidate(boolean autowireCandidate) 和 isAutowireCandidate():设置和获取 Bean 是否可以被自动装配。
/**
* Set whether this bean is a candidate for getting autowired into some other bean.
* <p>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();
- setPrimary(boolean primary) 和 isPrimary():设置和获取 Bean 是否为首选 Bean。
/**
* Set whether this bean is a primary autowire candidate.
* <p>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();
- setFactoryBeanName(String factoryBeanName) 和 getFactoryBeanName():设置和获取 FactoryBean 的名称。
/**
* 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();
- setFactoryMethodName(String factoryMethodName) 和 getFactoryMethodName():设置和获取 FactoryBean 的工厂方法名称。
/**
* 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();
- getConstructorArgumentValues() 和 getPropertyValues():获取构造函数参数值和属性值。
/**
* 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 the property values to be applied to a new instance of the bean.
* <p>The returned instance can be modified during bean factory post-processing.
* @return the MutablePropertyValues object (never {@code null})
*/
MutablePropertyValues getPropertyValues();
- setInitMethodName(String initMethodName) 和 getInitMethodName():设置和获取初始化方法的名称。
/**
* 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();
- setDestroyMethodName(String destroyMethodName) 和 getDestroyMethodName():设置和获取销毁方法的名称。
/**
* 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();
- setRole(int role) 和 getRole():设置和获取 Bean 的角色。
/**
* Set the role hint for this {@code BeanDefinition}. The role hint
* provides the frameworks as well as tools 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 an indication of
* the role and importance of a particular {@code BeanDefinition}.
* @see #ROLE_APPLICATION
* @see #ROLE_SUPPORT
* @see #ROLE_INFRASTRUCTURE
*/
int getRole();
- setDescription(String description) 和 getDescription():设置和获取 Bean 的描述信息。
/**
* 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();
- getResolvableType():获取 Bean 的可解析类型。
/**
* Return a resolvable type for this bean definition,
* based on the bean class or other specific metadata.
* <p>This is typically fully resolved on a runtime-merged bean definition
* but not necessarily on a configuration-time definition instance.
* @return the resolvable type (potentially {@link ResolvableType#NONE})
* @since 5.2
* @see ConfigurableBeanFactory#getMergedBeanDefinition
*/
ResolvableType getResolvableType();
- isSingleton() 和 isPrototype():判断 Bean 是否为单例和原型。
/**
* 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();
- isAbstract():判断 Bean 是否为抽象 Bean。
/**
* Return whether this bean is "abstract", that is, not meant to be instantiated.
*/
boolean isAbstract();
- getResourceDescription():获取 Bean 定义的资源描述。
/**
* 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();
- getOriginatingBeanDefinition():获取 Bean 定义的来源 Bean 定义,如果有的话。
/**
* Return the originating BeanDefinition, or {@code null} if none.
* <p>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();
通过 BeanDefinition 接口提供的各种方法,Spring 容器可以了解到 Bean 的各种属性信息,并据此进行实例化、依赖注入、生命周期管理等操作。