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 Jackson核心注解使用详解

《JavaJackson核心注解使用详解》:本文主要介绍JavaJackson核心注解的使用,​​Jackson核心注解​​用于控制Java对象与JSON之间的序列化、反序列化行为,简化字段映射... 目录前言一、@jsonProperty-指定JSON字段名二、@JsonIgnore-忽略字段三、@Jso

Spring Validation中9个数据校验工具使用指南

《SpringValidation中9个数据校验工具使用指南》SpringValidation作为Spring生态系统的重要组成部分,提供了一套强大而灵活的数据校验机制,本文给大家介绍了Spring... 目录1. Bean Validation基础注解常用注解示例在控制器中应用2. 自定义约束验证器定义自

Java对接Dify API接口的完整流程

《Java对接DifyAPI接口的完整流程》Dify是一款AI应用开发平台,提供多种自然语言处理能力,通过调用Dify开放API,开发者可以快速集成智能对话、文本生成等功能到自己的Java应用中,本... 目录Java对接Dify API接口完整指南一、Dify API简介二、准备工作三、基础对接实现1.

9个SpringBoot中的自带实用过滤器使用详解

《9个SpringBoot中的自带实用过滤器使用详解》在SpringBoot应用中,过滤器(Filter)是处理HTTP请求和响应的重要组件,SpringBoot自带了许多实用的过滤器,如字符编码,跨... 目录1. CharacterEncodingFilter - 字符编码过滤器功能和配置手动配置示例2

Spring Boot Controller处理HTTP请求体的方法

《SpringBootController处理HTTP请求体的方法》SpringBoot提供了强大的机制来处理不同Content-Type​的HTTP请求体,这主要依赖于HttpMessageCo... 目录一、核心机制:HttpMessageConverter​二、按Content-Type​处理详解1.

Spring Boot 常用注解详解与使用最佳实践建议

《SpringBoot常用注解详解与使用最佳实践建议》:本文主要介绍SpringBoot常用注解详解与使用最佳实践建议,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录一、核心启动注解1. @SpringBootApplication2. @EnableAutoConfi

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

Springboot实现推荐系统的协同过滤算法

《Springboot实现推荐系统的协同过滤算法》协同过滤算法是一种在推荐系统中广泛使用的算法,用于预测用户对物品(如商品、电影、音乐等)的偏好,从而实现个性化推荐,下面给大家介绍Springboot... 目录前言基本原理 算法分类 计算方法应用场景 代码实现 前言协同过滤算法(Collaborativ

Python与Java交互出现乱码的问题解决

《Python与Java交互出现乱码的问题解决》在现代软件开发中,跨语言系统的集成已经成为日常工作的一部分,特别是当Python和Java之间进行交互时,编码问题往往会成为导致数据传输错误、乱码以及难... 目录背景:为什么会出现乱码问题产生的场景解决方案:确保统一的UTF-8编码完整代码示例总结在现代软件

Java 如何创建和使用ExecutorService

《Java如何创建和使用ExecutorService》ExecutorService是Java中用来管理和执行多线程任务的一种高级工具,可以有效地管理线程的生命周期和任务的执行过程,特别是在需要处... 目录一、什么是ExecutorService?二、ExecutorService的核心功能三、如何创建