BeanDefinitionReader加载过程
BeanDefinitionReader
的作用是读取 Spring 配置文件中的内容,将其转换为 IOC
容器内部的数据结构:BeanDefinition
,就是使用 ResouceLoad
将配置信息解析成一个个 BeanDefinition
, 最终借助 BeanDefinitionRegistry
接口将 BeanDefinition
注册到容器当中。
BeanDefinitionReader.java 接口源代码如下:
/**
* Simple interface for bean definition readers.
* Specifies load methods with Resource and String location parameters.
*
* <p>Concrete bean definition readers can of course add additional
* load and register methods for bean definitions, specific to
* their bean definition format.
*
* <p>Note that a bean definition reader does not have to implement
* this interface. It only serves as suggestion for bean definition
* readers that want to follow standard naming conventions.
*
* @author Juergen Hoeller
* @see org.springframework.core.io.Resource
* @since 1.1
*/
public interface BeanDefinitionReader {
/**
* Return the bean factory to register the bean definitions with.
* <p>The factory is exposed through the BeanDefinitionRegistry interface,
* encapsulating the methods that are relevant for bean definition handling.
* 返回Bean工厂以向其注册Bean定义
*/
BeanDefinitionRegistry getRegistry();
/**
* Return the resource loader to use for resource locations.
* Can be checked for the <b>ResourcePatternResolver</b> interface and cast
* accordingly, for loading multiple resources for a given resource pattern.
* <p>A {@code null} return value suggests that absolute resource loading
* is not available for this bean definition reader.
* <p>This is mainly meant to be used for importing further resources
* from within a bean definition resource, for example via the "import"
* tag in XML bean definitions. It is recommended, however, to apply
* such imports relative to the defining resource; only explicit full
* resource locations will trigger absolute resource loading.
* <p>There is also a {@code loadBeanDefinitions(String)} method available,
* for loading bean definitions from a resource location (or location pattern).
* This is a convenience to avoid explicit ResourceLoader handling.
*
* @see #loadBeanDefinitions(String)
* @see org.springframework.core.io.support.ResourcePatternResolver
* <p>
* 返回资源加载器以用于资源位置
*/
@Nullable
ResourceLoader getResourceLoader();
/**
* Return the class loader to use for bean classes.
* <p>{@code null} suggests to not load bean classes eagerly
* but rather to just register bean definitions with class names,
* with the corresponding Classes to be resolved later (or never).
* 返回用于Bean类的类加载器
*/
@Nullable
ClassLoader getBeanClassLoader();
/**
* Return the BeanNameGenerator to use for anonymous beans
* (without explicit bean name specified).
* 返回BeanNameGenerator用于匿名Bean(未指定显式Bean名称)
*/
BeanNameGenerator getBeanNameGenerator();
/**
* Load bean definitions from the specified resource.
* <p>
* 从指定的资源加载bean定义
*
* @param resource the resource descriptor
* @return the number of bean definitions found
* @throws BeanDefinitionStoreException in case of loading or parsing errors
*/
int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException;
/**
* Load bean definitions from the specified resources.
*
* @param resources the resource descriptors
* @return the number of bean definitions found
* @throws BeanDefinitionStoreException in case of loading or parsing errors
*/
int loadBeanDefinitions(Resource... resources) throws BeanDefinitionStoreException;
/**
* Load bean definitions from the specified resource location.
* <p>The location can also be a location pattern, provided that the
* ResourceLoader of this bean definition reader is a ResourcePatternResolver.
* <p>
* 从指定的资源位置加载bean定义
* 该位置也可以是位置模式,前提是此bean定义读取器的ResourceLoader是ResourcePatternResolver
*
* @param location the resource location, to be loaded with the ResourceLoader
* (or ResourcePatternResolver) of this bean definition reader
* @return the number of bean definitions found
* @throws BeanDefinitionStoreException in case of loading or parsing errors
* @see #getResourceLoader()
* @see #loadBeanDefinitions(org.springframework.core.io.Resource)
* @see #loadBeanDefinitions(org.springframework.core.io.Resource[])
*/
int loadBeanDefinitions(String location) throws BeanDefinitionStoreException;
/**
* Load bean definitions from the specified resource locations.
*
* @param locations the resource locations, to be loaded with the ResourceLoader
* (or ResourcePatternResolver) of this bean definition reader
* @return the number of bean definitions found
* @throws BeanDefinitionStoreException in case of loading or parsing errors
*/
int loadBeanDefinitions(String... locations) throws BeanDefinitionStoreException;
}
Application
会适情况调用,BeanDefinitonReader
其中一个 load
方法,在 BeanDefinitionReader
的 load
方法当中会使用,ResourceLoad
读取资源,将解析出来的 BeanDefinition
注册到容器当中。
AbstractBeanDefinitionReader
:实现了EnvironmentCapable
,提供了获取/设置环境的方法。定义了一些通用方法,使用策略模式,将一些具体方法放到子类实现。XmlBeanDefinitionReader
:读取XML
文件定义的BeanDefinition
。PropertiesBeanDefinitionReader
:可以从属性文件,Resource
,Property
对象等读取BeanDefinition
。
AbstractBeanDefinitionReader
该类是实现了 BeanDefinitionReader
和 EnvironmentCapable
接口的抽象类,提供常见属性:工作的 bean 工厂、资源加载器、用于加载 bean 类的类加载器、环境等。该类中最核心的方法就是 loadBeanDefinitions
方法。
当传入的参数为资源位置数组时,进入上述方法,如果为字符串数组,则挨个遍历调用 loadBeanDefinitions(location)
方法。
关于 loadBeanDefinitions(location)
:根据资源加载器的不同,来处理资源路径,从而返回多个或一个资源,然后再将资源作为参数传递给 loadBeanDefinitions(resources)
方法。在该类中存在一个 loadBeanDefinitions(Resource. .. resources)
方法,该方法用于处理多个资源,归根结底,最后还是调用 loadBeanDefinitions((Resource)resource)
方法,该方法的具体实现在 XmlBeanDefinitionReader
中。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具