BeanDefinition作用

news/2024/7/5 6:51:00

BeanDefinition接口

BeanDefinition 描述一个 Bean 实例,这个实例有哪些属性值、构造函数以及一些其他信息,就是描述Bean实例的信息。

BeanDefinition是一个接口,允许BeanFactoryPostProcessor 内省和修改属性值和其他 Bean 元数据。
点击了解BeanFactoryPostProcessor

意义

在这里插入图片描述
在spring流程中是现有BeanDefinition再有bean,bean是根据BeanDefinition中的信息去创建的,当然在还没创建bean的时候还可以修改BeanDefinition中的信息,这个就是BeanFactoryPostProcessor 接口的主要功能,如我们在xml中定义的${jdbc.name}最终替换为真正的值就是在实现BeanFactoryPostProcessor 接口完成的。

源码

package org.springframework.beans.factory.config;

import org.springframework.beans.BeanMetadataElement;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.core.AttributeAccessor;
import org.springframework.core.ResolvableType;
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} 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 {

	/**
	 * Scope identifier for the standard singleton scope: {@value}.
	 * <p>Note that extended bean factories might support further scopes.
	 * @see #setScope
	 * @see ConfigurableBeanFactory#SCOPE_SINGLETON
	 */
	String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON;

	/**
	 * Scope identifier for the standard prototype scope: {@value}.
	 * <p>Note that extended bean factories might support further scopes.
	 * @see #setScope
	 * @see ConfigurableBeanFactory#SCOPE_PROTOTYPE
	 */
	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.
	 */
	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.
	 */
	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}.
	 */
	int ROLE_INFRASTRUCTURE = 2;


	// Modifiable attributes

	/**
	 * 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.
	 * <p>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.
	 * <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();

	/**
	 * 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.
	 * <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();

	/**
	 * Set the names of the beans that this bean depends on being initialized.
	 * The bean factory will guarantee that these beans get initialized first.
	 * <p>Note that dependencies are normally expressed through bean properties or
	 * constructor arguments. This property should just be necessary for other kinds
	 * of dependencies like statics (*ugh*) or database preparation on startup.
	 */
	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.
	 * <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();

	/**
	 * 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.
	 * @see #setFallback
	 */
	void setPrimary(boolean primary);

	/**
	 * Return whether this bean is a primary autowire candidate.
	 */
	boolean isPrimary();

	/**
	 * Set whether this bean is a fallback autowire candidate.
	 * <p>If this value is {@code true} for all beans but one among multiple
	 * matching candidates, the remaining bean will be selected.
	 * @since 6.2
	 * @see #setPrimary
	 */
	void setFallback(boolean fallback);

	/**
	 * Return whether this bean is a fallback autowire candidate.
	 * @since 6.2
	 */
	boolean isFallback();

	/**
	 * Specify the factory bean to use, if any.
	 * This is the name of the bean to call the specified factory method on.
	 * <p>A factory bean name is only necessary for instance-based factory methods.
	 * For static factory methods, the method will be derived from the bean class.
	 * @see #setFactoryMethodName
	 * @see #setBeanClassName
	 */
	void setFactoryBeanName(@Nullable String factoryBeanName);

	/**
	 * Return the factory bean name, if any.
	 * <p>This will be {@code null} for static factory methods which will
	 * be derived from the bean class instead.
	 * @see #getFactoryMethodName()
	 * @see #getBeanClassName()
	 */
	@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.
	 * @see #getFactoryBeanName()
	 * @see #getBeanClassName()
	 */
	@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
	 * @see #getConstructorArgumentValues()
	 */
	default boolean hasConstructorArgumentValues() {
		return !getConstructorArgumentValues().isEmpty();
	}

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

	/**
	 * Return if there are property values defined for this bean.
	 * @since 5.0.2
	 * @see #getPropertyValues()
	 */
	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 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();

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

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

	/**
	 * Return whether this bean is "abstract", that is, not meant to be instantiated
	 * itself but rather just serving as parent for concrete child bean definitions.
	 */
	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.
	 * <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();

}

方法签名中文解释
void setParentName(@Nullable String parentName)设置该 bean 定义的父定义的名称(如果有)。
@Nullable String getParentName()返回该 bean 定义的父定义的名称(如果有)。
void setBeanClassName(@Nullable String beanClassName)指定该 bean 定义的 bean 类名称。
@Nullable String getBeanClassName()返回该 bean 定义的当前 bean 类名称。
void setScope(@Nullable String scope)重写该 bean 的目标范围,指定新的范围名称。
@Nullable String getScope()返回该 bean 当前目标范围的名称,或如果尚不清楚则返回 null。
void setLazyInit(boolean lazyInit)设置该 bean 是否应延迟初始化。
boolean isLazyInit()返回该 bean 是否应延迟初始化,即不在启动时急切实例化。仅适用于单例 bean。
void setDependsOn(@Nullable String… dependsOn)设置该 bean 依赖于初始化的 bean 的名称。
@Nullable String[] getDependsOn()返回该 bean 依赖的 bean 名称。
void setAutowireCandidate(boolean autowireCandidate)设置该 bean 是否是某些其他 bean 自动装配的候选者。
boolean isAutowireCandidate()返回该 bean 是否是某些其他 bean 自动装配的候选者。
void setPrimary(boolean primary)设置该 bean 是否是主要自动装配候选者。
boolean isPrimary()返回该 bean 是否是主要自动装配候选者。
void setFallback(boolean fallback)设置该 bean 是否是后备自动装配候选者。
boolean isFallback()返回该 bean 是否是后备自动装配候选者。
void setFactoryBeanName(@Nullable String factoryBeanName)指定要使用的工厂 bean(如果有)。这是调用指定工厂方法的 bean 的名称。
@Nullable String getFactoryBeanName()返回工厂 bean 名称(如果有)。
void setFactoryMethodName(@Nullable String factoryMethodName)指定工厂方法(如果有)。此方法将使用构造函数参数调用,或者如果没有指定参数则不带参数调用。
@Nullable String getFactoryMethodName()返回工厂方法(如果有)。
ConstructorArgumentValues getConstructorArgumentValues()返回该 bean 的构造函数参数值。返回的实例可以在 bean 工厂后处理期间进行修改。
boolean hasConstructorArgumentValues()返回是否定义了该 bean 的构造函数参数值。
MutablePropertyValues getPropertyValues()返回应用于该 bean 的新实例的属性值。返回的实例可以在 bean 工厂后处理期间进行修改。
boolean hasPropertyValues()返回是否为该 bean 定义了属性值。
void setInitMethodName(@Nullable String initMethodName)设置初始化方法的名称。
@Nullable String getInitMethodName()返回初始化方法的名称。
void setDestroyMethodName(@Nullable String destroyMethodName)设置销毁方法的名称。
@Nullable String getDestroyMethodName()返回销毁方法的名称。
void setRole(int role)设置该 BeanDefinition 的角色提示。
int getRole()获取该 BeanDefinition 的角色提示。
void setDescription(@Nullable String description)设置该 bean 定义的人类可读描述。
@Nullable String getDescription()返回该 bean 定义的人类可读描述。
ResolvableType getResolvableType()返回基于 bean 类或其他特定元数据的可解析类型。
boolean isSingleton()返回此 bean 是否为单例,所有调用都返回单个共享实例。
boolean isPrototype()返回此 bean 是否为原型,每次调用返回一个独立的实例。
boolean isAbstract()返回此 bean 是否为抽象,即不是用于实例化自身,而是作为具体子 bean 定义的父类。
@Nullable String getResourceDescription()返回该 bean 定义的资源描述(用于在出现错误时显示上下文)。
@Nullable BeanDefinition getOriginatingBeanDefinition()返回原始的 BeanDefinition,如果没有则返回 null。

http://lihuaxi.xjx100.cn/news/2165400.html

相关文章

React开发必须掌握这些es6语法-03

箭头函数 其实就是java的lamda编程&#xff0c;它的特点是单向无环流&#xff0c;没有变量&#xff0c;源数据状态不能被改变。 基本语法 ()> {} //表示一个空函数&#xff0c;和function(){}功能一样&#xff0c;如果只有一行语句则&#xff0c;{}可省略 lef fn arg &g…

2024-5-24

今日安排&#xff1a; 继续审计 nf_tables 源码 && iptables 相关学习♥♥♥♥♥复现 CTF 相关题目♥♥♥♥学习 winpwn♥♥♥♥mount 的使用&#xff0c;学习 namespace (昨昨昨昨昨昨昨昨昨昨昨昨昨昨天残留的任务)&#xff08;&#xff1a;看我能搁到什么时候♥♥♥…

魔众文库系统v6.6.0分销功能,后台日志重构,文档转换优化

分销功能&#xff0c;后台日志重构&#xff0c;文档转换优化 [新功能] 升级支持支付宝授权登录最新方式 [新功能] 后台左上角标题支持自定义&#xff0c;修改 modstart.php 中 admin.title 配置 [新功能] 日志界面重构&#xff0c;全新日志查看体验 [新功能] 链接选择弹窗增…

【C++风云录】生物物理模型仿真平台:生态学模型与环境变化预测

重塑生态系统&#xff1a;从模型建立到演化模拟 前言 本文将深入探讨六种用于不同领域的C库。这些库分别应用于生态学模型开发、环境变化预测、个体为基础的生态系统建模、演化模拟、大规模动态系统模型仿真、模型探索&#xff0c;以及对"量化"金融模型的支持和生物…

display(a,b)什么意思

在Python中&#xff0c;如果你看到display(a,b)这样的代码&#xff0c;它通常意味着有人正在使用IPython.display模块中的display函数来同时显示两个对象。 IPython.display是Jupyter Notebook和JupyterLab等交互式计算环境的一部分&#xff0c;它提供了一种在笔记本中显示各种…

QLExpress入门及实战总结

文章目录 1.背景2.简介3.QLExpress实战3.1 基础例子3.2 低代码实战3.2.1 需求描述3.2.1 使用规则引擎3.3.2 运行结果 参考文档 1.背景 最近研究低代码实现后端业务逻辑相关功能&#xff0c;使用LiteFlow作为流程编排后端service服务, 但是LiteFlow官方未提供图形界面编排流程。…

k8s 声明式资源管理

一、资源配置清单的管理 1.1 查看资源配置清单 声明式管理方法&#xff1a; 1.适合于对资源的修改操作 2.声明式资源管理方法依赖于资源配置清单文件对资源进行管理 资源配置清单文件有两种格式&#xff1a;yaml&#xff08;人性化&#xff0c;易读&#xff09;&#xff0c;j…

深入分析 Android Activity (四)

深入分析 Android Activity (四) 1. Activity 的生命周期详解 Activity 的生命周期方法提供了一组回调&#xff0c;使开发者能够在不同状态下执行相应的逻辑。了解这些方法有助于开发者管理资源和确保应用程序的行为一致。 1.1 onCreate onCreate 是 Activity 的入口点&…