Spring BeanDefinition接口

news/2025/11/9 19:46:23/文章来源:https://www.cnblogs.com/dddy/p/19204749

[[Spring IOC 源码学习总笔记]]

BeanDefinition

BeanDefinition 主要是用来描述Bean,主要包括:Bean的属性、是否单例、延迟加载、Bean的名称、构造方法等。

简而言之: 在容器中的 一个Bean 对应一个 BeanDefinition 的封装描述; Spring 使用 BeanDefinitionRegistry 管理BeanDefinition

org.springframework.beans.factory.config.BeanDefinition

/** Copyright 2002-2020 the original author or authors.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      https://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/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;/****  BeanDefinition 描述一个bean实例,该实例具有属性值、构造函数参数值以及由具体实现提供的进一步信息。*  简而言之: 在容器中的Bean 对应一个 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.* @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.* Bean 的作用域标识: 单例标记, 可通过setScope方法设置* MVC的Session Request 等不在这里..* @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.* Bean 的作用域标识: 原型标记, 可通过setScope方法设置* MVC的Session Request 等不在这里..* @see #setScope* @see ConfigurableBeanFactory#SCOPE_PROTOTYPE*/String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;/*** 以下三个属性, 标识 Bean 的角色, 可通过 setRole 方法设置* ROLE_APPLICATION = 0;//应用程序定义, 或用户定义的bean* ROLE_SUPPORT = 1;   //比较复杂的配置, 值得注意的?* ROLE_INFRASTRUCTURE = 2;//Spring内部定义的Bean*//*** 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.* 设置 父Bean Definition 的名称, 如果有的话* 对应XML定义: `<bean parent="">`*/void setParentName(@Nullable String parentName);/*** Return the name of the parent definition of this bean definition, if any.*/@NullableString 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.**  bean的类名, 对应XML定义: `<bean class="">`* @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()*/@NullableString getBeanClassName();/*** Override the target scope of this bean, specifying a new scope name.* @see #SCOPE_SINGLETON* @see #SCOPE_PROTOTYPE* 设置bean作用域*/void setScope(@Nullable String scope);/*** Return the name of the current target scope for this bean,* or {@code null} if not known yet.*/@NullableString 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.**   dependsOn 一般用于两个bean之间没有显式依赖, 但后一个Bean需要用到前一个Bean执行初始方法后的结果;*     例如在 `< bean id="a" dependsOn="b"/> ` 时, 在初始化a时首先先初始化b, 在销毁b之前会先销毁a;*/void setDependsOn(@Nullable String... dependsOn);/*** Return the bean names that this bean depends on.*/@NullableString[] 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.** 是否 能被自动装配 <bean autowire-candidate="true/false">** 设置该对象是否可以被其他对象自动装配(可以作为自动注入的候选);  spring通过配置bean的autowire属性设置自动装配方式*/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.* `@Primary` 注解就是通过它去设置的; 如果存在同类型的 bean 是否这个bean优先*/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.*定义创建该 Bean对象的工厂类, 对应XML定义: `<bean factory-bean="">`* @see #setFactoryMethodName*/void setFactoryBeanName(@Nullable String factoryBeanName);/*** Return the factory bean name, if any.*/@NullableString 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.** 定义创建该Bean对象的工厂方法, 对应XML定义 ` <bean factory-method="">`* @see #setFactoryBeanName* @see #setBeanClassName*/void setFactoryMethodName(@Nullable String factoryMethodName);/*** Return a factory method, if any.*/@NullableString getFactoryMethodName();/*** Return the constructor argument values for this bean.* <p>The returned instance can be modified during bean factory post-processing.* 返回此bean的构造函数参数值;* @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.* <p>The returned instance can be modified during bean factory post-processing.** 普通属性的集合, 如:** <bean id="student" class="com.rc.sp.Student">*   <property name="id" value="1"/>*   <property name="dao" ref="dao" />*   <property name="map">*     <map>*       <entry key="math" value="90"/>*       <entry key="english" value="85"/>*     </map>*   </property>* </bean>** @return the MutablePropertyValues object (never {@code null})*/MutablePropertyValues getPropertyValues();/*** Return if there are property values defined for this bean.* @since 5.0.2*/default boolean hasPropertyValues() {return !getPropertyValues().isEmpty();}/*** Set the name of the initializer method.* 初始化方法名 对应XML 定义: `<bean id="address" class="org.yang.learn.spring.dto.Address" init-method="" destroy-method="">`* @since 5.1*/void setInitMethodName(@Nullable String initMethodName);/*** Return the name of the initializer method.* @since 5.1*/@NullableString getInitMethodName();/*** Set the name of the destroy method.* 销毁方法名 对应XML 定义: `<bean id="address" class="org.yang.learn.spring.dto.Address" init-method="" destroy-method="">`* @since 5.1*/void setDestroyMethodName(@Nullable String destroyMethodName);/*** Return the name of the destroy method.* @since 5.1*/@NullableString 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.*/@NullableString 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.** 这个 ResolvableType 作用, 大概是封装java的类型描述* ResolvableTypes may be obtained from fields, method parameters, method returns or classes.* 	  For example:* 		private HashMap<Integer, List<String>> myMap;* 		public void example() {* 			ResolvableType t = ResolvableType.forField(getClass().getDeclaredField("myMap"));* 			t.getSuperType(); // AbstractMap<Integer, List<String>>* 			t.asMap(); // Map<Integer, List<String>>* 			t.getGeneric(0).resolve(); // Integer* 			t.getGeneric(1).resolve(); // List* 			t.getGeneric(1); // List<String>* 			t.resolveGeneric(1, 0); // String* 		}* @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.*/boolean isAbstract();/*** Return a description of the resource that this bean definition* came from (for the purpose of showing context in case of errors).** 返回该bean定义来自的资源的描述(用于在出现错误时显示上下文)*/@NullableString 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.* 返回原始 BeanDefinition 定义; 可null*/@NullableBeanDefinition getOriginatingBeanDefinition();}

关于 role 属性

getRole 属性 ; Spring将bean分为三种角色

  1. ROLE_APPLICATION = 0;//应用程序定义, 或用户定义的bean
  2. ROLE_SUPPORT = 1; //比较复杂的配置, 值得注意的? 目前没搞明白
  3. ROLE_INFRASTRUCTURE = 2;//Spring内部定义的Bean, 与用户完全无关

BeanDefinition 的常见实现

  1. GenericBeanDefinition
    BeanDefinition 接口的通用实现,可以用于大多数 bean 定义情况。它提供了许多属性和方法,可以用来配置 bean 的各种属性和行为。

  2. RootBeanDefinition
    GenericBeanDefinition 的子类,提供了更多针对父子 bean 配置的支持。它可以用于定义根 bean,同时允许其他 bean 对其进行扩展。

  3. ChildBeanDefinition
    同样是 GenericBeanDefinition 的子类,用于定义子 bean。它可以继承另一个 bean 的配置,并可以进行进一步的定制和覆盖。

  4. AnnotatedBeanDefinition
    这是用于 基于注解的配置的 BeanDefinition 实现。它可以用来描述通过注解(如 @Component@Service@Repository 等)定义的 bean。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/960681.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

pythontip 计算字符串中的音节数

编写一个程序来计算一个单词中的音节数。 音节之间由连字符分隔。例如: beau-ti-ful => 三个音节 cat => 一个音节 re-frig-er-a-tor => 五个音节 定义函数count_syllables()的函数,该函数接受一个参数word…

深入解析:26-基于STM32的小区智能井盖监测系统设计与实现

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

2025/11/09 LGNOIpR23

如果不是直接写 100pts 做法,就是赛时不会,或者对 AC 做法有很大启发。 T1 题意简述 求 \(a(1+p+pq)=n\) 整数解数量,其中 \(n\) 是给定的, \(p,q\) 均不能取 \(1\)。 sol 考虑试除法,一遍试出所有的 \(a\),然后…

Python “值层面” 该怎么说?别再混淆 “字面量” 与 “不可变对象”

为帮助Python入门者厘清“值/字面量/对象”的概念混淆,我将围绕“为什么‘a的值是10’表述不准确”这一痛点,结合内存模型与代码示例,详细解析字面量和不可变对象的区别,最终总结认知误区与正确表述方式。# Python…

11.7 联考总结

11.7 联考总结 T1比较简单,随便找找支配关系就做完了。T2之前听 nkp 讲过题意比较畏惧,简单思考了一下就放弃了。T3是很好的计数题,很容易想到 \(O(n^2)\) 做法。但是有个组合数下指标求和难以优化掉,尝试换别的做…

pythontip 返回字典的键值

编写一个程序,分别按字母顺序返回字典的键和值。 定义函数get_sorted_keys_values(),参数为dict_obj(字典类型)。 在函数内部,返回一个由两个列表组成的列表:一个列表是按字母顺序排列的字典键,另一个列表是它们对…

详细介绍:SQL入门:行列转换实战-从基础到高级

详细介绍:SQL入门:行列转换实战-从基础到高级pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", &…

折腾笔记[36]-调用海康SDK实现相机拍照

使用c#调用海康SDK实现相机拍照.摘要 使用c#调用海康SDK实现相机拍照. 前言 本文目的是分享人工踩坑经验, AI搜索引擎可以更快给出正确结果(用于投喂AI😂). 关键信息海康SDK版本: 4.6.x .net8框架 封装: HikVisionCo…

CF840C On the Bench 分析

题目概述 题目链接:https://www.luogu.com.cn/problem/CF840C。 给你 \(n\) 给数,将他们排列成一个序列并满足相邻两项 \(a_i,a_{i+1}\) 相乘不为平方数。问方案。 分析 我只说明一种解法,其他类的解法总结见:http…

HubSpot如何构建MCP服务器实现AI代理集成

HubSpot产品执行副总裁分享公司如何基于Model Context Protocol构建远程MCP服务器,详细解析技术架构选择、认证实现、与现有系统集成等核心挑战,以及AI代理在CRM领域的实际应用场景。HubSpot的MCP实现:CRM公司的AI代…

CSP-S 2025 趋势记

有个人 CSP-S 寄飞了,我不说是谁。感觉模拟赛也没啥动力打了。 T1 写了 \(10\) min 切了,非常自信。T2 写了 \(30\) min 被卡成 \(80\) 了,趋势。 然后开始传统艺能发呆,T3 看半天看错题了,看成可以替换无限次了,…

苹果手机iOS15.8.2 – 16.7.8最新越狱方法

支持设备 越狱支持所有 A9-A11 片上系统 (SoC)。您可以在下面找到兼容的 Palera1n 越狱 iPhone 和 iPad 的实际列表。越狱经过测试,可以在运行 iOS 15 的 iPhone X (GSM)、iPhone 8、iPhone 7 和 iPhone 6s上运行和工…

威联通NAS部署umami - 详解

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

后端八股之Redis - 详解

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

AGC052 VP 记录

场切 \(0\) 道题,被猜性质猜结论题气笑了. A 后来回想起来,这个题好像确实在北京集训讲过. 有 \(4\) 种合法的构造:\(0\overbrace{1\cdots1}^{n个} \overbrace{0\cdots0}^{n个}\),\(1\overbrace{0\cdots0}^{n个} \…

结合400行mini-react代码,图文解说React原理

引言: 在我学习React原理的时候,一上来看的非常全而细节的书/博客(头大),或者是看的教你实现一个简单mini-react(还是一知半解),最终学的痛苦又效果不好。所以,写了这篇博客,希望能帮助您入门React原理。此外…

UE:告别加载卡顿!一键合并StaticMeshActor方案

© mengzhishanghun 原创文章 首发于 博客园 禁止未经授权转载前言 大型UE场景打包后首次加载时,经常遇到长时间卡顿甚至假死现象。究其原因,成百上千个独立的StaticMeshActor在序列化、实例化时产生了巨大开销…

在Visual Studio使用Qt的插件机制进行开发 - 指南

在Visual Studio使用Qt的插件机制进行开发 - 指南pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas",…