BeanFactory

1:BeanFactory什么:

官方解释The root interface for accessing a Spring bean container,翻译成中文spring bean容器的最基本接口,也就是作为一个spring容器,必须实现它。

2:BeanFactory容器乘的要内容是什么

This interface is implemented by objects that hold a number of bean definitions

2.1他能够乘bean definitions。

对于bean definitions是什么东东,应该看BeanDefinition这个类的定义:A BeanDefinition describes a bean instance, which has property values,constructor argument values, and further information supplied byconcrete
implementations.说白了就是对<bean></bean>标签内容的解析,不过不过xml中<bean>的解析。并非实例化的对象。跟踪BeanFactory的实现类XmlBeanFactory代码发现事实上现了接口BeanDefinitionRegistry。定义了对BeanDefinition的增删改查等操作。

2.2他能够乘单例对象

详细实例化对象的注冊位置(通常是单例的。对于原型没有必要保存)。跟踪BeanFactory的实现类XmlBeanFactory代码发现事实上现了接口SingletonBeanRegistry,在这个接口了,专门定义了单例对象的增增删改查等操作。

XmlBeanDefinitionReader

从xml中读取bean definitions。将xml解析成dom文档,并通过一个实现了
BeanDefinitionDocumentReader接口的对象来解析该dom文档,终于将xml的标签解析为BeanDefinition对象。

BeanDefinitionDocumentReader与BeanDefinitionParserDelegate

将dom文档终于解析为BeanDefinition,并加入到BeanDefinition容器中。

详细的操作流程

BeanFactory bf = new XmlBeanFactory(new ClassPathResource("spring.xml"));


1 在XmlBeanFactory的构造函数中运行
this.reader.loadBeanDefinitions(resource);

reader为其一个字段

private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);

2在loadBeanDefinitions中主要运行的为

doLoadBeanDefinitions(inputSource, encodedResource.getResource())
3在doLoadBeanDefinitions中主要运行的为
registerBeanDefinitions(doc, resource);

4获取BeanDefinitionDocumentReader并运行

registerBeanDefinitions(doc, createReaderContext(resource));

5在registerBeanDefinitions方法中主要运行的为

parseBeanDefinitions(root, delegate);

6通过delegate对dom文档进行解析

delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);

解析类型非常多,再次仅仅是为特殊标签进行了解析

7对解析完毕的BeanDefinition进行注冊
BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());

解析的xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<bean id="person" class="model.Person">
<property name="name">
<value>lisi</value>
</property>
<property name="sex">
<value>boy</value>
</property>
</bean>
</beans>

解析流程图

注意:此博客为我第一次阅读spring源代码,源代码内容为spring3.0.当中的错误之处请多包涵并欢迎指正。流程图也存在不规范之处

版权声明:本文博主原创文章,博客,未经同意不得转载。

spring来源理解-BeanFactory子类XmlBeanFactory创建过程的更多相关文章

  1. Spring MVC 原理探秘 - 容器的创建过程

    1.简介 在上一篇文章中,我向大家介绍了 Spring MVC 是如何处理 HTTP 请求的.Spring MVC 可对外提供服务时,说明其已经处于了就绪状态.再次之前,Spring MVC 需要进行 ...

  2. Spring源码解析 – AnnotationConfigApplicationContext容器创建过程

    Spring在BeanFactory基础上提供了一些列具体容器的实现,其中AnnotationConfigApplicationContext是一个用来管理注解bean的容器,从AnnotationC ...

  3. (转)深入理解Java对象的创建过程

    参考来源:http://blog.csdn.net/justloveyou_/article/details/72466416 摘要: 在Java中,一个对象在可以被使用之前必须要被正确地初始化,这一 ...

  4. 死磕Spring之IoC篇 - Bean 的创建过程

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...

  5. 深入理解Java对象的创建过程:类的初始化与实例化

    摘要: 在Java中,一个对象在可以被使用之前必须要被正确地初始化,这一点是Java规范规定的.在实例化一个对象时,JVM首先会检查相关类型是否已经加载并初始化,如果没有,则JVM立即进行加载并调用类 ...

  6. Spring IOC 容器源码分析 - 创建单例 bean 的过程

    1. 简介 在上一篇文章中,我比较详细的分析了获取 bean 的方法,也就是getBean(String)的实现逻辑.对于已实例化好的单例 bean,getBean(String) 方法并不会再一次去 ...

  7. Spring IOC 容器源码分析 - 创建原始 bean 对象

    1. 简介 本篇文章是上一篇文章(创建单例 bean 的过程)的延续.在上一篇文章中,我们从战略层面上领略了doCreateBean方法的全过程.本篇文章,我们就从战术的层面上,详细分析doCreat ...

  8. 0003 - 基于xml的Spring Bean 的创建过程

    一.目录 前言 创建 Bean 容器 加载 Bean 定义 创建 Bean Spring Bean 创建过程中的设计模式 总结 二.前言 2.1 Spring 使用配置 ApplicationCont ...

  9. Spring源码浅析之bean实例的创建过程(二)

    在上一篇内容中,介绍了doGetBean方法的源码内容,知道了bean在创建的过程中,有三个范围,单例.多例.Scope,里面都使用到了createBean.下面本篇文章的主要内容,就是围绕creat ...

随机推荐

  1. The mmap module

    The mmap module The mmap module (New in 2.0) This module provides an interface to the operating syst ...

  2. POJ 3040 Allowance 贪心

    这题目的贪心思路还是有一点细节问题的. 还没有证明,据说是因为题目给的条件是每个价格是比它小的价格的倍数才能这么贪心的. 思路如下: 假设要给奶牛的钱为C 1)从大面值到小面值一次拿钱,能拿多少拿多少 ...

  3. IOS开发之----四舍五入问题

    方法一: -(NSString *)notRounding:(float)price afterPoint:(int)position{ NSDecimalNumberHandler* roundin ...

  4. 基于HOG特征的Adaboost行人检测

    原地址:http://blog.csdn.net/van_ruin/article/details/9166591 .方向梯度直方图(Histogramof Oriented Gradient, HO ...

  5. UVA796- Critical Links(无向图中的桥梁)

    题目链接 题意: 给出一个无向图,按顺序输出桥 思路:求出全部的桥,然后按顺序输出就可以 代码: #include <iostream> #include <cstdio> # ...

  6. DOM的event对象的属性和方法

    属性/方法 类型 是否可读写 描写叙述 altKey Boolean 读写 指示是否按下alt键 bubbles Boolean 读 指示事件是否冒泡 button Intrger 读写 鼠标事件发生 ...

  7. hdu4405(概率dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4405 题意:跳棋有0~n个格子,每个格子X可以摇一次色子,色子有六面p(1=<p<=6), ...

  8. HDU 3639 Hawk-and-Chicken(良好的沟通)

    HDU 3639 Hawk-and-Chicken 题目链接 题意:就是在一个有向图上,满足传递关系,比方a->b, b->c,那么c能够得到2的支持,问得到支持最大的是谁,而且输出这些人 ...

  9. pro-engineer&UG

    Pro/Engineer操作软件是美国参数技术公司(PTC)旗下的CAD/CAM/CAE一体化的三维软件.Pro/Engineer软件以参数化著称,是参数化技术的最早应用者,在目前的三维造型软件领域中 ...

  10. DataInputStream类readLong()引起的思考

    今天无意中看了下jdk中的DataInputStream类,然后看到readLong()方法,如下: private byte readBuffer[] = new byte[8]; public f ...