Spring源码学习--ClassPathXmlApplicationContext+FileSystemXmlApplicationContext+XmlWebApplicationContext

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

文章来源:

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

这里写图片描述

一 ClassPathXmlApplicationContext

在实现类ClassPathXmlApplicationContext中其实并没有多少重要的操作,主要是在构造函数中配置Spring配置文件的路径:

public class DaoOperationMain {  public static void main(String[] args) {  ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");  }  
}  

具体的applicationContext.xml解析相关的操作都在父类refresh中进行操作。

/** * 并没有太多具体的操作,主要是初始化构造函数,主要的操作都在父类中 */  
public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext {  private Resource[] configResources;  public ClassPathXmlApplicationContext() {  }  public ClassPathXmlApplicationContext(ApplicationContext parent) {  super(parent);  }  public ClassPathXmlApplicationContext(String configLocation) throws BeansException {  this(new String[] {configLocation}, true, null);  }  public ClassPathXmlApplicationContext(String... configLocations) throws BeansException {  this(configLocations, true, null);  }  public ClassPathXmlApplicationContext(String[] configLocations, ApplicationContext parent) throws BeansException {  this(configLocations, true, parent);  }  public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh) throws BeansException {  this(configLocations, refresh, null);  }  public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)  throws BeansException {  super(parent);  //设置spring的配置文件  setConfigLocations(configLocations);  if (refresh) {  //调用父类的refresh函数,进行一系列初始化  refresh();  }  }  public ClassPathXmlApplicationContext(String path, Class<?> clazz) throws BeansException {  this(new String[] {path}, clazz);  }  public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz) throws BeansException {  this(paths, clazz, null);  }  public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz, ApplicationContext parent)  throws BeansException {  super(parent);  Assert.notNull(paths, "Path array must not be null");  Assert.notNull(clazz, "Class argument must not be null");  this.configResources = new Resource[paths.length];  for (int i = 0; i < paths.length; i++) {  this.configResources[i] = new ClassPathResource(paths[i], clazz);  }  //调用父类的refresh函数,进行一系列初始化  refresh();  }  @Override  protected Resource[] getConfigResources() {  return this.configResources;  }  }  

二 FileSystemXmlApplicationContext

FileSystemXmlApplicationContext的源码如下:

/** * 并没有太多具体的操作,很多初始化操作都在其父类中 */  
public class FileSystemXmlApplicationContext extends AbstractXmlApplicationContext {  public FileSystemXmlApplicationContext() {  }  public FileSystemXmlApplicationContext(ApplicationContext parent) {  super(parent);  }  public FileSystemXmlApplicationContext(String configLocation) throws BeansException {  this(new String[] {configLocation}, true, null);  }  public FileSystemXmlApplicationContext(String... configLocations) throws BeansException {  this(configLocations, true, null);  }  public FileSystemXmlApplicationContext(String[] configLocations, ApplicationContext parent) throws BeansException {  this(configLocations, true, parent);  }  public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh) throws BeansException {  this(configLocations, refresh, null);  }  public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)  throws BeansException {  //调用父类的构造函数  super(parent);  //设置配置文件的地址,如application.xml  setConfigLocations(configLocations);  if (refresh) {  //刷新容器,其实就是重新初始化容器  refresh();  }  }  @Override  protected Resource getResourceByPath(String path) {  if (path != null && path.startsWith("/")) {  path = path.substring(1);  }  return new FileSystemResource(path);  }  
}  

三 XmlWebApplicationContext

XmlWebApplicationContext是用于web容器的应用上下文的,其也没有太多操作,主要还是applicationContext.xml的解析操作,完整源码如下:

public class XmlWebApplicationContext extends AbstractRefreshableWebApplicationContext {  /** Default config location for the root context */  //默认配置文件地址  public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";  /** Default prefix for building a config location for a namespace */  public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/";  /** Default suffix for building a config location for a namespace */  public static final String DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml";  //解析applicationContext.xml  @Override  protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {  // Create a new XmlBeanDefinitionReader for the given BeanFactory.  XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);  // Configure the bean definition reader with this context's  // resource loading environment.  beanDefinitionReader.setEnvironment(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.  initBeanDefinitionReader(beanDefinitionReader);  loadBeanDefinitions(beanDefinitionReader);  }  protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {  }  protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException {  String[] configLocations = getConfigLocations();  if (configLocations != null) {  for (String configLocation : configLocations) {  reader.loadBeanDefinitions(configLocation);  }  }  }  //获取默认配置文件地址  @Override  protected String[] getDefaultConfigLocations() {  if (getNamespace() != null) {  return new String[] {DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX};  }  else {  return new String[] {DEFAULT_CONFIG_LOCATION};  }  }  

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



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

相关文章

java如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

Java进程异常故障定位及排查过程

《Java进程异常故障定位及排查过程》:本文主要介绍Java进程异常故障定位及排查过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、故障发现与初步判断1. 监控系统告警2. 日志初步分析二、核心排查工具与步骤1. 进程状态检查2. CPU 飙升问题3. 内存

java中新生代和老生代的关系说明

《java中新生代和老生代的关系说明》:本文主要介绍java中新生代和老生代的关系说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、内存区域划分新生代老年代二、对象生命周期与晋升流程三、新生代与老年代的协作机制1. 跨代引用处理2. 动态年龄判定3. 空间分

Java设计模式---迭代器模式(Iterator)解读

《Java设计模式---迭代器模式(Iterator)解读》:本文主要介绍Java设计模式---迭代器模式(Iterator),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录1、迭代器(Iterator)1.1、结构1.2、常用方法1.3、本质1、解耦集合与遍历逻辑2、统一