Spring源码学习--AbstractXmlApplicationContext抽象类

本文主要是介绍Spring源码学习--AbstractXmlApplicationContext抽象类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章来源:

1 https://blog.csdn.net/qq924862077/article/details/58656150
2 https://blog.csdn.net/qq924862077/article/details/58650318
3 https://blog.csdn.net/qq924862077/article/details/58653218

这里写图片描述

AbstractXmlApplicationContext中做的操作就是对applicationContext.xml的解析操作,主要是由两个函数

loadBeanDefinitions(DefaultListableBeanFactory beanFactory)

loadBeanDefinitions(XmlBeanDefinitionReader reader)

中实现。

// 主要是对spring注入配置文件的解析,主要使用到BeanDefinitionReader和BeanDefinitionDocumentReader两个接口相关的实现类  
@Override  
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {  // Create a new XmlBeanDefinitionReader for the given BeanFactory.  //创建XmlBeanDefinitionReader,即创建Bean读取器,并通过回调设置到容器中去,容  器使用该读取器读取Bean定义资源  XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);  // Configure the bean definition reader with this context's  // resource loading environment.  //为Bean读取器设置Spring资源加载器,AbstractXmlApplicationContext的    //祖先父类AbstractApplicationContext继承DefaultResourceLoader,因此,容器本身也是一个资源加载器   beanDefinitionReader.setEnvironment(this.getEnvironment());  beanDefinitionReader.setResourceLoader(this);  beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));  // Allow a subclass to provide custom initialization of the reader,  // then proceed with actually loading the bean definitions.  //当Bean读取器读取Bean定义的Xml资源文件时,启用Xml的校验机制  initBeanDefinitionReader(beanDefinitionReader);  //Bean读取器真正实现加载的方法  loadBeanDefinitions(beanDefinitionReader);  
}  

loadBeanDefinitions中的操作实际是XmlBeanDefinitionReader来解析applicationContext.xml.

// Xml Bean读取器加载Bean定义资源   
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {  //获取Bean定义资源的定位  Resource[] configResources = getConfigResources();  if (configResources != null) {  //Xml Bean读取器调用其父类AbstractBeanDefinitionReader读取定位    //的Bean定义资源  reader.loadBeanDefinitions(configResources);  }  String[] configLocations = getConfigLocations();  if (configLocations != null) {  //Xml Bean读取器调用其父类AbstractBeanDefinitionReader读取定位    //的Bean定义资源  reader.loadBeanDefinitions(configLocations);  }  
}  

简单来说AbstractXmlApplicationContext中包含了applicationContext.xml的解析操作。




完整的AbstractXmlApplicationContext源码如下:

/** *提供的几个方法主要是用来进行spring注入bean的配置文件进行解析,主要的操作都在父类中 */  
public abstract class AbstractXmlApplicationContext extends AbstractRefreshableConfigApplicationContext {  private boolean validating = true;  public AbstractXmlApplicationContext() {  }  public AbstractXmlApplicationContext(ApplicationContext parent) {  super(parent);  }  public void setValidating(boolean validating) {  this.validating = validating;  }  //主要是对spring注入配置文件的解析,主要使用到BeanDefinitionReader和BeanDefinitionDocumentReader两个接口相关的实现类  @Override  protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {  // Create a new XmlBeanDefinitionReader for the given BeanFactory.  //创建XmlBeanDefinitionReader,即创建Bean读取器,并通过回调设置到容器中去,容  器使用该读取器读取Bean定义资源  XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);  // Configure the bean definition reader with this context's  // resource loading environment.  //为Bean读取器设置Spring资源加载器,AbstractXmlApplicationContext的    //祖先父类AbstractApplicationContext继承DefaultResourceLoader,因此,容器本身也是一个资源加载器   beanDefinitionReader.setEnvironment(this.getEnvironment());  beanDefinitionReader.setResourceLoader(this);  beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));  // Allow a subclass to provide custom initialization of the reader,  // then proceed with actually loading the bean definitions.  //当Bean读取器读取Bean定义的Xml资源文件时,启用Xml的校验机制  initBeanDefinitionReader(beanDefinitionReader);  //Bean读取器真正实现加载的方法  loadBeanDefinitions(beanDefinitionReader);  }  protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {  reader.setValidating(this.validating);  }  //Xml Bean读取器加载Bean定义资源   protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {  //获取Bean定义资源的定位  Resource[] configResources = getConfigResources();  if (configResources != null) {  //Xml Bean读取器调用其父类AbstractBeanDefinitionReader读取定位    //的Bean定义资源  reader.loadBeanDefinitions(configResources);  }  String[] configLocations = getConfigLocations();  if (configLocations != null) {  //Xml Bean读取器调用其父类AbstractBeanDefinitionReader读取定位    //的Bean定义资源  reader.loadBeanDefinitions(configLocations);  }  }  //这里又使用了一个委托模式,调用子类的获取Bean定义资源定位的方法    //该方法在ClassPathXmlApplicationContext中进行实现,对于我们    //举例分析源码的FileSystemXmlApplicationContext没有使用该方法  protected Resource[] getConfigResources() {  return null;  }  }  

这篇关于Spring源码学习--AbstractXmlApplicationContext抽象类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1143572

相关文章

Java实现在Word文档中添加文本水印和图片水印的操作指南

《Java实现在Word文档中添加文本水印和图片水印的操作指南》在当今数字时代,文档的自动化处理与安全防护变得尤为重要,无论是为了保护版权、推广品牌,还是为了在文档中加入特定的标识,为Word文档添加... 目录引言Spire.Doc for Java:高效Word文档处理的利器代码实战:使用Java为Wo

SpringBoot日志级别与日志分组详解

《SpringBoot日志级别与日志分组详解》文章介绍了日志级别(ALL至OFF)及其作用,说明SpringBoot默认日志级别为INFO,可通过application.properties调整全局或... 目录日志级别1、级别内容2、调整日志级别调整默认日志级别调整指定类的日志级别项目开发过程中,利用日志

Java中的抽象类与abstract 关键字使用详解

《Java中的抽象类与abstract关键字使用详解》:本文主要介绍Java中的抽象类与abstract关键字使用详解,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、抽象类的概念二、使用 abstract2.1 修饰类 => 抽象类2.2 修饰方法 => 抽象方法,没有

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

Spring 中的切面与事务结合使用完整示例

《Spring中的切面与事务结合使用完整示例》本文给大家介绍Spring中的切面与事务结合使用完整示例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录 一、前置知识:Spring AOP 与 事务的关系 事务本质上就是一个“切面”二、核心组件三、完

Java实现远程执行Shell指令

《Java实现远程执行Shell指令》文章介绍使用JSch在SpringBoot项目中实现远程Shell操作,涵盖环境配置、依赖引入及工具类编写,详解分号和双与号执行多指令的区别... 目录软硬件环境说明编写执行Shell指令的工具类总结jsch(Java Secure Channel)是SSH2的一个纯J

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

HTTP 与 SpringBoot 参数提交与接收协议方式

《HTTP与SpringBoot参数提交与接收协议方式》HTTP参数提交方式包括URL查询、表单、JSON/XML、路径变量、头部、Cookie、GraphQL、WebSocket和SSE,依据... 目录HTTP 协议支持多种参数提交方式,主要取决于请求方法(Method)和内容类型(Content-Ty

深度解析Java @Serial 注解及常见错误案例

《深度解析Java@Serial注解及常见错误案例》Java14引入@Serial注解,用于编译时校验序列化成员,替代传统方式解决运行时错误,适用于Serializable类的方法/字段,需注意签... 目录Java @Serial 注解深度解析1. 注解本质2. 核心作用(1) 主要用途(2) 适用位置3