ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");

Spring加载xml数据的切入点是通过ClassPathXmlApplicationContext类进行切入的。该类是面向xml文件。类似的spring还提供了面向注解的解析类AnnotationConfigApplicationContext等。

进入

new ClassPathXmlApplicationContext("spring-config.xml");

之后代码如下:

public ClassPathXmlApplicationContext(
      String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
      throws BeansException {

super(parent);
   setConfigLocations(configLocations);
   if (refresh) {
      refresh();
   }
}

Spring容器的加载、xml文件的解析器以及单例、非懒加载的类等都是由refresh()方法完成。Refresh代码如下所示:

@Override
public void refresh() throws BeansException, IllegalStateException {
   synchronized (this.startupShutdownMonitor) {
      // Prepare this context for refreshing.
      //准备刷新环境列入对系统属性或者环境变量进行准备及验证
      prepareRefresh();

// Tell the subclass to refresh the internal bean factory.
      /* 调用子类AbstractRefreshableApplicationContext 刷新DefaultListableBeanFactory工厂
       * 功能描述  1、创建核心组件ConfigurableListableBeanFactory
       * 2、获取xml、注解等方式的配置,将其转换为原数据放入ConfigurableListableBeanFactory中
       * 3、xml读取
       */
      ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

// Prepare the bean factory for use in this context.
      prepareBeanFactory(beanFactory);

try {
         // Allows post-processing of the bean factory in context subclasses.
         postProcessBeanFactory(beanFactory);

// Invoke factory processors registered as beans in the context.
         invokeBeanFactoryPostProcessors(beanFactory);

// Register bean processors that intercept bean creation.
         registerBeanPostProcessors(beanFactory);

// Initialize message source for this context.
         initMessageSource();

// Initialize event multicaster for this context.
         initApplicationEventMulticaster();

// Initialize other special beans in specific context subclasses.
         onRefresh();

// Check for listener beans and register them.
         registerListeners();

// Instantiate all remaining (non-lazy-init) singletons.
         /* 创建单例或者非懒加载实例*/
         finishBeanFactoryInitialization(beanFactory);

// Last step: publish corresponding event.
         finishRefresh();
      }

catch (BeansException ex) {
         if (logger.isWarnEnabled()) {
            logger.warn("Exception encountered during context initialization - " +
                  "cancelling refresh attempt: " + ex);
         }

// Destroy already created singletons to avoid dangling resources.
         destroyBeans();

// Reset 'active' flag.
         cancelRefresh(ex);

// Propagate exception to caller.
         throw ex;
      }

finally {
         // Reset common introspection caches in Spring's core, since we
         // might not ever need metadata for singleton beans anymore...
         resetCommonCaches();
      }
   }
}

如代码所示xml数据的读取主要是在obtainFreshBeanFactory()方法中进行处理的。而AbstractApplicationContext类中并没有对obtainFreshBeanFactory()方法做具体的实现,而是将其交由子类AbstractRefreshableApplicationContext去实现

Xml的读取主要处理2个问题

1、准备容器,用于存储解析后的数据

2、准备documentReader,用于解析spring配置文件

代码如下:

@Override
protected final void refreshBeanFactory() throws BeansException {
   /**
    * DefaultListableBeanFactory被实例化至少一次时将销毁spring创建的对象,并且关闭DefaultListableBeanFactory实例
    */
   if (hasBeanFactory()) {
      destroyBeans();
      closeBeanFactory();
   }
   try {
      DefaultListableBeanFactory beanFactory = createBeanFactory();//创建spring的核心组件
      //为了序列化指定id
      beanFactory.setSerializationId(getId());
      //定制beanFactory,设置相关属性。
      customizeBeanFactory(beanFactory);
      //初始化xml读取器、读取原数据切入点  交由子类处理
      loadBeanDefinitions(beanFactory);
      synchronized (this.beanFactoryMonitor) {
         this.beanFactory = beanFactory;
      }
   }
   catch (IOException ex) {
      throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
   }
}

该方法主要完成以下几个工作:

1、校验DefaultListableBeanFactory被实例化至少一次时将销毁spring创建的对象,并且关闭DefaultListableBeanFactory实例

2、创建spring的核心组件DefaultListableBeanFactory的具体实例并设置相应参数,也就是beanFactory。

3、初始化xml读取器、读取xml数据切入点  交由子类处理。

4、将beanFactory设定为全局变量。

AbstractRefreshableApplicationContext并没有对loadBeanDefinitions()方法进行具体的实现,而是将其交由子类去做处理,这样做的好处是针对不同的spring配置方式采用不同的解析方式进行解析(策略模式)。

Ps:此处只针对xml文件的配置(AbstractXmlApplicationContext)解析进行说明

上面部分代码完成了对beanFactory的定制,并且对xml文件的解析做了切入loadBeanDefinitions();

@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
   // Create a new XmlBeanDefinitionReader for the given BeanFactory.
   /*依据beanFactory创建新的XmlBeanDefinitionReader */
   XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

// Configure the bean definition reader with this context's
   // resource loading environment.
   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.
   initBeanDefinitionReader(beanDefinitionReader);
   loadBeanDefinitions(beanDefinitionReader);
}

以上代码完成以下功能

1、为特定的beanFactory准备相应的beanDefinitionReader 并完成相应设置

2、切入loadBeanDefinitions();

Xml文件的解析是交由专门的解析类AbstractBeanDefinitionReader进行处理。

protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
   Resource[] configResources = getConfigResources();
   if (configResources != null) {
      reader.loadBeanDefinitions(configResources);
   }
   String[] configLocations = getConfigLocations();
   if (configLocations != null) {
      reader.loadBeanDefinitions(configLocations);
   }
}

Ps:读者可参考此图

五、spring源码阅读之ClassPathXmlApplicationContext加载beanFactory的更多相关文章

  1. 【Spring源码分析】Bean加载流程概览

    代码入口 之前写文章都会啰啰嗦嗦一大堆再开始,进入[Spring源码分析]这个板块就直接切入正题了. 很多朋友可能想看Spring源码,但是不知道应当如何入手去看,这个可以理解:Java开发者通常从事 ...

  2. 【Spring源码分析】Bean加载流程概览(转)

    转载自:https://www.cnblogs.com/xrq730/p/6285358.html 代码入口 之前写文章都会啰啰嗦嗦一大堆再开始,进入[Spring源码分析]这个板块就直接切入正题了. ...

  3. Spring源码分析:Bean加载流程概览及配置文件读取

    很多朋友可能想看Spring源码,但是不知道应当如何入手去看,这个可以理解:Java开发者通常从事的都是Java Web的工作,对于程序员来说,一个Web项目用到Spring,只是配置一下配置文件而已 ...

  4. Spring源码解析-配置文件的加载

    spring是一个很有名的java开源框架,作为一名javaer还是有必要了解spring的设计原理和机制,beans.core.context作为spring的三个核心组件.而三个组件中最重要的就是 ...

  5. 【Spring源码分析系列】加载Bean

    /** * Create a new XmlBeanFactory with the given input stream, * which must be parsable using DOM. * ...

  6. Spring源码阅读-ApplicationContext体系结构分析

    目录 继承层次图概览 ConfigurableApplicationContext分析 AbstractApplicationContext GenericApplicationContext Gen ...

  7. Spring源码阅读 之 配置的读取,解析

    在上文中我们已经知道了Spring如何从我们给定的位置加载到配置文件,并将文件包装成一个Resource对象.这篇文章我们将要探讨的就是,如何从这个Resouce对象中加载到我们的容器?加载到容器后又 ...

  8. 初始化IoC容器(Spring源码阅读)

    初始化IoC容器(Spring源码阅读) 我们到底能走多远系列(31) 扯淡: 有个问题一直想问:各位你们的工资剩下来会怎么处理?已婚的,我知道工资永远都是不够的.未婚的你们,你们是怎么分配工资的? ...

  9. 37 网络相关函数(五)——live555源码阅读(四)网络

    37 网络相关函数(五)——live555源码阅读(四)网络 37 网络相关函数(五)——live555源码阅读(四)网络 简介 10)MAKE_SOCKADDR_IN构建sockaddr_in结构体 ...

随机推荐

  1. $CH5302$ 金字塔 区间$DP$/计数类$DP$

    CH Sol f[l][r]表示l到r这段区间对应的金字塔结构种数 发现是f[l][r]是可以由比它小的区间推出来的 比如已知f[l+1][k],f[k+1][r],不难想到f[l][r]+=f[l+ ...

  2. 「SP122」STEVE - Voracious Steve 解题报告

    SP122 STEVE - Voracious Steve 题意翻译 Problem Steve和他的一个朋友在玩游戏,游戏开始前,盒子里有 n个甜甜圈,两个人轮流从盒子里抓甜甜圈,每次至少抓 1个, ...

  3. Cent OS防火墙配置端口开放

    CentOS 6内置的防火墙为iptables,Cent OS7,内置的防火墙则是firewalld iptables 防火墙设置 1.打开/关闭/重启防火墙 #开启防火墙(重启后永久生效): chk ...

  4. Java AOP的底层实现原理

    Java AOP的底层实现原理 一.什么是AOP 1.AOP:Aspect Oriented Programming(面向切面编程),OOP是面向对象编程,AOP是在OOP基础之上的一种更高级的设计思 ...

  5. 【转】推荐!国外程序员整理的Java资源大全

    构建 这里搜集了用来构建应用程序的工具. Apache Maven:Maven使用声明进行构建并进行依赖管理,偏向于使用约定而不是配置进行构建.Maven优于Apache Ant.后者采用了一种过程化 ...

  6. 案例分析丨H&M用设计冲刺将App研发周期缩短为6个月

    案例背景 H&M是一家来自瑞典的时装公司,1947年成立.截至2018年6月,H&M 分店遍布全球 68 个国家和地区,分店数目为 4338 间. 作为快速服装生产商,H&M的 ...

  7. python 枚举类型

    在python中枚举是一种类(Enum,IntEnum),存放在enum模块中.枚举类型可以给一组标签赋予一组特定的值. 枚举的特点: 枚举类中不能存在相同的标签名 枚举是可迭代的 不同的枚举标签可以 ...

  8. 深夜话题boot2docker还有那些隐藏MENU

    马克思的博士论文:自由意识的集中表达 --字体我设为5(18pt),你们懂的 总有人埋汰,终于我想起一个负负得正的话题 为什么放在深夜,因为希望看到的人越少越好,深夜是时差党的乐园 本篇作为之前几篇围 ...

  9. 《C# 爬虫 破境之道》:第一境 爬虫原理 — 第六节:第一境尾声

    在第一境中,我们主要了解了爬虫的一些基本原理,说原理也行,说基础知识也罢,结果就是已经知道一个小爬虫是如何诞生的了~那么现在,请默默回想一下,在第一境中,您都掌握了哪些内容?哪些还比较模糊?如果还有什 ...

  10. 前端笔记6-js2

    1.break 和continue用法 break结束本次循环,如果想结束外层循环,可以通过这个label来指定要结束的循环. continue可以用来跳过当次循环,如果想跳过外次循环,也可以通过这个 ...