本文主要分析 springBeanDefinition 的加载,对于其解析我们在后面的文章中专门分析。

BeanDefinition 是属于 Spring Bean 模块的,它是对 spring bean 的统一抽象描述定义接口,我们知道在spring中定义bean的方式有很多种,如XML、注解以及自定义标签,同事Bean的类型也有很多种,如常见的工厂Bean、自定义对象、Advisor等等,我们在分析加载BeanDefinition之前,首先来了解它的定义和注册设计。



上面类图我们做一个简单介绍,具体详细介绍在后面的相关文章说明

  • AliasRegistry 为 Bean注册一个别名的顶级接口

  • BeanDefinitionRegistry 主要用来把bean的描述信息注册到容器中,spring在注册bean时一般是获取到bean后通过 BeanDefinitionRegistry 来注册当当前的 BeanFactory

  • BeanDefinition 是用来定义描述 Bean的名字、作用域、角色、依赖、懒加载等基础信息,以及包含与spring容器运行和管理Bean信息相关的属性。spring中通过它实现了对bean的定制化统一,这也是一个核心接口层

  • AnnotatedBeanDefinition 是一个接口,继承了 BeanDefinition , 对其做了一定的扩展,主要用来描述注解Bean的定义信息

  • AttributeAccessor 主要用来设置 Bean配置信息中的属性和属性值的接口,实现key-value的映射关系

  • AbstractBeanDefinition 是对 BeanDefintion 的一个抽象化实现,是一个模板,具体的详细实现交给子类

2. BeanDefinition

ClassPathResource resource = new ClassPathResource("bean.xml"); // <1>
DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); // <2>
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory); // <3>
reader.loadBeanDefinitions(resource);

上面这段代码是 spring 中从资源的定位到加载过程,我们可以简单分析一下:

  1. 通过 ClassPathResource 进行资源的定位,获取到资源
  2. 获取 BeanFactory ,即上下文
  3. 通过工厂创建一个特定的 XmlBeanDefinitionReader 对象,该 Reader 是一个资源解析器, 实现了 BeanDefinitionReader 接口
  4. 装载资源

整个过程分为三个大步骤,示意图:



我们文章主要分析的就是第二步,装载的过程,

3.loadBeanDefinitions

资源的定位我们之前文章分析过了,不在阐述,这里我们关心 reader.loadBeanDefinitions(resource); 这句的具体实现,

通过代码追踪我们可以知道方法 #loadBeanDefinitions(...) 是定义在 BeanDefinitionReader 中的,而他的具体实现是在 XmlBeanDefinitionReader 类中,代码如下:

/**
* 从指定的xml文件中加载bean的定义
* Load bean definitions from the specified XML file.
* @param resource the resource descriptor for the XML file
* @return the number of bean definitions found
* @throws BeanDefinitionStoreException in case of loading or parsing errors
*/
@Override
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
//调用私有方法处理 这里将resource进行了编码处理,保证了解析的正确性
return loadBeanDefinitions(new EncodedResource(resource));
}
/**
* 装载bean定义的真实处理方法
* Load bean definitions from the specified XML file.
* @param encodedResource the resource descriptor for the XML file,
* allowing to specify an encoding to use for parsing the file
* @return the number of bean definitions found
* @throws BeanDefinitionStoreException in case of loading or parsing errors
*/
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
//1.对资源判空
Assert.notNull(encodedResource, "EncodedResource must not be null");
if (logger.isTraceEnabled()) {
logger.trace("Loading XML bean definitions from " + encodedResource);
}
//2.获取当前线程中的 EncodedResource 集合 -> 已经加载过的资源
Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
//3.若当前已加载资源为空,则创建并添加
if (currentResources == null) {
currentResources = new HashSet<>(4);
this.resourcesCurrentlyBeingLoaded.set(currentResources);
}
//4.添加资源到集合如果已加载资源中存在 则抛出异常
if (!currentResources.add(encodedResource)) {
throw new BeanDefinitionStoreException(
"Detected cyclic loading of " + encodedResource + " - check your import definitions!");
}
//5.获取 encodedResource 中的 Resource ,在获取 intputSteram 对象
try (InputStream inputStream = encodedResource.getResource().getInputStream()) {
InputSource inputSource = new InputSource(inputStream);
if (encodedResource.getEncoding() != null) {
inputSource.setEncoding(encodedResource.getEncoding());
}
//6. 真实执行加载beanDefinition业务逻辑的方法
return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
}
catch (IOException ex) {
throw new BeanDefinitionStoreException(
"IOException parsing XML document from " + encodedResource.getResource(), ex);
}
finally {
//7.从已加载集合中去除资源
currentResources.remove(encodedResource);
if (currentResources.isEmpty()) {
this.resourcesCurrentlyBeingLoaded.remove();
}
}
}
  • 通过 resourcesCurrentlyBeingLoaded.get() 代码,来获取已经加载过的资源,然后将 encodedResource 加入其中,如果 resourcesCurrentlyBeingLoaded 中已经存在该资源,则抛出 BeanDefinitionStoreException 异常。

  • 为什么需要这么做呢?答案在 "Detected cyclic loading" ,避免一个 EncodedResource 在加载时,还没加载完成,又加载自身,从而导致死循环。也因此,,当一个 EncodedResource 加载完成后,需要从缓存中剔除。

  • encodedResource 获取封装的 Resource 资源,并从 Resource 中获取相应的 InputStream ,然后将 InputStream 封装为 InputSource ,最后调用 #doLoadBeanDefinitions(InputSource inputSource, Resource resource) 方法,执行加载 BeanDefinition 的真正逻辑

protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
throws BeanDefinitionStoreException {
try {
//1. 获取到 Document 实例
Document doc = doLoadDocument(inputSource, resource);
//2. 注册bean实列,通过document
int count = registerBeanDefinitions(doc, resource);
if (logger.isDebugEnabled()) {
logger.debug("Loaded " + count + " bean definitions from " + resource);
}
return count;
}
catch (BeanDefinitionStoreException ex) {
throw ex;
}
catch (SAXParseException ex) {
throw new XmlBeanDefinitionStoreException(resource.getDescription(),
"Line " + ex.getLineNumber() + " in XML document from " + resource + " is invalid", ex);
}
catch (SAXException ex) {
throw new XmlBeanDefinitionStoreException(resource.getDescription(),
"XML document from " + resource + " is invalid", ex);
}
catch (ParserConfigurationException ex) {
throw new BeanDefinitionStoreException(resource.getDescription(),
"Parser configuration exception parsing XML from " + resource, ex);
}
catch (IOException ex) {
throw new BeanDefinitionStoreException(resource.getDescription(),
"IOException parsing XML document from " + resource, ex);
}
catch (Throwable ex) {
throw new BeanDefinitionStoreException(resource.getDescription(),
"Unexpected exception parsing XML document from " + resource, ex);
}
}

上面 #registerBeanDefinitions(...) 方法是 beanDefinition 的具体加载过程, #doLoadDocument(...) 是解析 document 的方法内部包含 spring 的验证模型与 document 解析两块,这些我们在后面专门进行分析

本文由AnonyStar 发布,可转载但需声明原文出处。

仰慕「优雅编码的艺术」 坚信熟能生巧,努力改变人生

欢迎关注微信公账号 :云栖简码 获取更多优质文章

更多文章关注笔者博客 :云栖简码

深入Spring之IOC之加载BeanDefinition的更多相关文章

  1. 【死磕 Spring】—— IoC 之加载 BeanDefinition

    本文主要基于 Spring 5.0.6.RELEASE 摘要: 原创出处 http://cmsblogs.com/?p=2658 「小明哥」,谢谢! 作为「小明哥」的忠实读者,「老艿艿」略作修改,记录 ...

  2. Spring之IOC容器加载初始化的方式

    引言 我们知道IOC容器时Spring的核心,可是如果我们要依赖IOC容器对我们的Bean进行管理,那么我们就需要告诉IOC容易他需要管理哪些Bean而且这些Bean有什么要求,这些工作就是通过通过配 ...

  3. 【死磕 Spring】----- IOC 之 加载 Bean

    原文出自:http://cmsblogs.com 先看一段熟悉的代码: ClassPathResource resource = new ClassPathResource("bean.xm ...

  4. Spring源码加载BeanDefinition过程

    本文主要讲解Spring加载xml配置文件的方式,跟踪加载BeanDefinition的全过程. 源码分析 源码的入口 ClassPathXmlApplicationContext构造函数 new C ...

  5. spring bean的重新加载

    架构体系 在谈spring bean的重新加载前,首先我们来看看spring ioc容器. spring ioc容器主要功能是完成对bean的创建.依赖注入和管理等功能,而这些功能的实现是有下面几个组 ...

  6. interface21 - web - ContextLoaderListener(Spring Web Application Context加载流程)

    前言 最近打算花点时间好好看看spring的源码,然而现在Spring的源码经过迭代的版本太多了,比较庞大,看起来比较累,所以准备从最初的版本(interface21)开始入手,仅用于学习,理解其设计 ...

  7. Spring bean是如何加载的

    Spring bean是如何加载的 加载bean的主要逻辑 在AbstractBeanFactory中doGetBean对加载bean的不同情况进行拆分处理,并做了部分准备工作 具体如下 获取原始be ...

  8. Spring boot 国际化自动加载资源文件问题

    Spring boot 国际化自动加载资源文件问题 最近在做基于Spring boot配置的项目.中间遇到一个国际化资源加载的问题,正常来说只要在application.properties文件中定义 ...

  9. Spring Boot的属性加载顺序

        伴随着团队的不断壮大,往往不需要开发人员知道测试或者生产环境的全部配置细节,比如数据库密码,帐号信息等.而是希望由运维或者指定的人员去维护配置信息,那么如果要修改某项配置信息,就不得不去修改项 ...

随机推荐

  1. 用SQL查询分析实现类似金蝶K3的收发存明细表

    使用SQL查询分析实现类收发存的报表,原始需求在 另外一篇文章 的第四部分.下图是实现需求. 一.准备 删除临时表 [buy]判断是否存在临时表,存在则删除[/buy] if OBJECT_ID('t ...

  2. 15分钟从零开始搭建支持10w+用户的生产环境(三)

    上一篇文章介绍了这个架构中,选择MongoDB做为数据库的原因,及相关的安装操作. 原文地址:15分钟从零开始搭建支持10w+用户的生产环境(二)   三.WebServer 在SOA和gRPC大行其 ...

  3. WFS: postgresql(postgis)和shp文件查询效率对比

    对GeoServer上的WFS的各种数据源查询效率感兴趣,做个测试.本次测试了Postgresql.geopackage.shp文件三种数据源的查询效率,无论是本机还是服务器环境,pg存储查询效率都比 ...

  4. swoole学习--登录模块

    使用swoole+thinkphp6.0+redis 结合开发的登录模块,做完之后有几点感悟: 1.不要相信任务数据,包括请求的外部接口,特别是超时者部分,尽可能的交给task完成. 2.原来可以在入 ...

  5. python学习18类4之静态类

    '''''''''类的静态方法.普通方法.类方法 静态方法: 用 @staticmethod 装饰的不带 self 参数的方法叫做静态方法,类的静态方法可以没有参数,可以直接使用类名调用. 普通方法: ...

  6. How to change the header background color of a QTableView

    You can set the style sheet on the QTableView ui->tableView->setStyleSheet("QHeaderView:: ...

  7. Spring5参考指南:SpringAOP简介

    文章目录 AOP的概念 Spring AOP简介 Spring AOP通知类型 写过程序的都知道OOP即面向对象编程. 从最开始的面向过程编程,到后面的面向对象编程,程序的编写方式发生了重大的变化,面 ...

  8. 6.Python中内存是如何管理的?

    Python中内存是如何管理的? Python memory is managed by Python private heap space. All Python objects and data ...

  9. BootStrap的栅格式布局

    1.栅格系统(布局) Bootstrap内置了一套响应式.移动设备优先的流式栅格系统,随着屏幕设备或视口(viewport)尺寸的增加,系统会自动分为最多12列. 我在这里是把Bootstrap中的栅 ...

  10. .user.ini 无法修改/删除 怎么办?

    首先 了解chattr命令: Linux chattr命令用于改变文件属性. 这项指令可改变存放在ext2文件系统上的文件或目录属性,这些属性共有以下8种模式: a:让文件或目录仅供附加用途.b:不更 ...