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图片压缩三种高效压缩方案详细解析

《Java图片压缩三种高效压缩方案详细解析》图片压缩通常涉及减少图片的尺寸缩放、调整图片的质量(针对JPEG、PNG等)、使用特定的算法来减少图片的数据量等,:本文主要介绍Java图片压缩三种高效... 目录一、基于OpenCV的智能尺寸压缩技术亮点:适用场景:二、JPEG质量参数压缩关键技术:压缩效果对比

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

Java利用docx4j+Freemarker生成word文档

《Java利用docx4j+Freemarker生成word文档》这篇文章主要为大家详细介绍了Java如何利用docx4j+Freemarker生成word文档,文中的示例代码讲解详细,感兴趣的小伙伴... 目录技术方案maven依赖创建模板文件实现代码技术方案Java 1.8 + docx4j + Fr

SpringBoot首笔交易慢问题排查与优化方案

《SpringBoot首笔交易慢问题排查与优化方案》在我们的微服务项目中,遇到这样的问题:应用启动后,第一笔交易响应耗时高达4、5秒,而后续请求均能在毫秒级完成,这不仅触发监控告警,也极大影响了用户体... 目录问题背景排查步骤1. 日志分析2. 性能工具定位优化方案:提前预热各种资源1. Flowable

基于SpringBoot+Mybatis实现Mysql分表

《基于SpringBoot+Mybatis实现Mysql分表》这篇文章主要为大家详细介绍了基于SpringBoot+Mybatis实现Mysql分表的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录基本思路定义注解创建ThreadLocal创建拦截器业务处理基本思路1.根据创建时间字段按年进

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La