【学习底层原理系列】重读spring源码3-加载beanDefinition的方法obtainFreshBeanFactory
obtainFreshBeanFactory()方法概述
定义BeanFactory,并加载以下两种bean的定义,装配到BeanFactory:
1.配置文件中定义的bean
2.通过<context:component-scan base-package="..." />配置的路径下的,且经过相应注解标注的所有类,注解包括:@Controller、@Service、@Component、@Repository
源码解读
主要流程总结:
1.创建BeanFactory:DefaultListableBeanFactory
2.解析web.xml配置,读取spring配置文件,封装为Resource对象
3.把Resource对象封装为Document对象
4.开始层层遍历Document的节点。
以下是细节:
先来看该方法的实现,注:这里会把无关代码删掉,以方便阅读。
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
//刷新bean工厂
this.refreshBeanFactory();
//创建bean工厂
return this.getBeanFactory();
}
重点看刷新bean工厂部分:
protected final void refreshBeanFactory() throws BeansException {
//创建bean工厂
DefaultListableBeanFactory beanFactory = createBeanFactory();
//这里加载beanDefinition,并赋给bean工厂
loadBeanDefinitions(beanFactory);
}
createBeanFactory()好理解,就是new了个工厂对象。
有了工厂对象后,就需要往里面装载东西,装什么呢?这里是
接下来看loadBeanDefinitions(beanFactory)方法的具体实现:创建xml文件读取器
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
// 以下这一堆内容就是为了准备一个xml文件读取器,仅作了解
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
beanDefinitionReader.setEnvironment(this.getEnvironment());
beanDefinitionReader.setResourceLoader(this);
beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
initBeanDefinitionReader(beanDefinitionReader);
//这里才是核心,加载beanDefinition的工作还没开始
loadBeanDefinitions(beanDefinitionReader);
}
继续跟进去,这里依然“没干正事”:加载spring配置文件
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);
}
}
接着看核心代码
public int loadBeanDefinitions(String location, Set<Resource> actualResources) throws BeanDefinitionStoreException {
ResourceLoader resourceLoader = getResourceLoader();
if (resourceLoader instanceof ResourcePatternResolver) {
// 通配符模式匹配资源,转换为Resource对象。spring提供了多种ResourceLoader,根据通配符匹配,生成对应类型的Resource
Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
//【继续把加载工作往后放】
int loadCount = loadBeanDefinitions(resources);return loadCount;
}
}
else {
// 以绝对路径加载单个资源文件,转换为Resource对象
Resource resource = resourceLoader.getResource(location);
//【继续把加载工作往后放】
int loadCount = loadBeanDefinitions(resource);return loadCount;
}
}
通过上面一步,把配置资源转化为Resource对象,然后作为参数传入loadxxx方法里进行解析。
进入下面的实现发现,依然在做准备工作:将Resource读取为流
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
//
Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
if (currentResources == null) {
currentResources = new HashSet<EncodedResource>(4);
this.resourcesCurrentlyBeingLoaded.set(currentResources);
}
if (!currentResources.add(encodedResource)) {
throw new BeanDefinitionStoreException(
"Detected cyclic loading of " + encodedResource + " - check your import definitions!");
}
try {
InputStream inputStream = encodedResource.getResource().getInputStream();
InputSource inputSource = new InputSource(inputStream);
if (encodedResource.getEncoding() != null) {
inputSource.setEncoding(encodedResource.getEncoding());
}
//终于到do...是不是这里就开始真正的执行加载了?
return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
}
catch (IOException ex) {
throw new BeanDefinitionStoreException(
"IOException parsing XML document from " + encodedResource.getResource(), ex);
}
finally {
currentResources.remove(encodedResource);
if (currentResources.isEmpty()) {
this.resourcesCurrentlyBeingLoaded.remove();
}
}
}
来看下,删除非核心代码,就做了两件事,先读取资源对象Resource,封装成Document对象;再“注册”beanDefinition。
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
throws BeanDefinitionStoreException {
//生成Document对象
Document doc = doLoadDocument(inputSource, resource);
//注册BeanDefinition
return registerBeanDefinitions(doc, resource); }
中间又经历了n个准备环境,最终进入方法parseBeanDefinitions,拿到了Document对象的根节点,开始调用解析方法解析节点:
protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {
if (delegate.isDefaultNamespace(root)) {
NodeList nl = root.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element ele = (Element) node;
if (delegate.isDefaultNamespace(ele)) {
parseDefaultElement(ele, delegate);
}
else {
delegate.parseCustomElement(ele);
}
}
}
}
else {
delegate.parseCustomElement(root);
}
}
具体的解析逻辑,可以参考以下文章:https://blog.csdn.net/v123411739/article/details/86669952
BeanDefinition包含的主要内容:
@todo
解析完成后,依然是注入到BeanFactory中缓存起来,供后续使用,主要的内容是两部分:
1.beanDefinitionNames
2.beanDefinitionMap
总结:
obtainFreshBeanFactory()方法的主要作用:
1.创建beanFactory
2.根据web.xml中contextConfigLocation配置的路径,读取Spring配置文件,封装为Resource
3.根据Resource加载XML配置文件(bean文件)并解析为Document对象
4.遍历Document,解析为beanDefinition。
【学习底层原理系列】重读spring源码3-加载beanDefinition的方法obtainFreshBeanFactory的更多相关文章
- 【学习底层原理系列】重读spring源码1-建立基本的认知模型
开篇闲扯 在工作中,相信很多人都有这种体会,与其修改别人代码,宁愿自己重写. 为什么? 先说为什么愿意自己写: 从0-1的过程,是建立在自己已有认知基础上,去用自己熟悉的方式构建一件作品.也就是说, ...
- 线程池底层原理详解与源码分析(补充部分---ScheduledThreadPoolExecutor类分析)
[1]前言 本篇幅是对 线程池底层原理详解与源码分析 的补充,默认你已经看完了上一篇对ThreadPoolExecutor类有了足够的了解. [2]ScheduledThreadPoolExecut ...
- 深入理解 spring 容器,源码分析加载过程
Spring框架提供了构建Web应用程序的全功能MVC模块,叫Spring MVC,通过Spring Core+Spring MVC即可搭建一套稳定的Java Web项目.本文通过Spring MVC ...
- 读spring源码(二)-XmlBeanDefinitionReader-解析BeanDefinition
上次说到ApplicationContext加载BeanDefinition时会创建一个XmlBeanDefinitionReader,将XML解析.BeanDefinition加载委托给XmlBea ...
- 2.2 spring5源码 -- ioc加载的整体流程
之前我们知道了spring ioc的加载过程, 具体如下图. 下面我们就来对照下图, 看看ioc加载的源代码. 下面在用装修类比, 看看个个组件都是怎么工作的. 接下来是源码分析的整体结构图. 对照上 ...
- 深入Spring之IOC之加载BeanDefinition
本文主要分析 spring 中 BeanDefinition 的加载,对于其解析我们在后面的文章中专门分析. BeanDefinition 是属于 Spring Bean 模块的,它是对 spring ...
- HashMap底层原理及jdk1.8源码解读
一.前言 写在前面:小编码字收集资料花了一天的时间整理出来,对你有帮助一键三连走一波哈,谢谢啦!! HashMap在我们日常开发中可谓经常遇到,HashMap 源码和底层原理在现在面试中是必问的.所以 ...
- jquery源码 DOM加载
jQuery版本:2.0.3 DOM加载有关的扩展 isReady:DOM是否加载完(内部使用) readyWait:等待多少文件的计数器(内部使用) holdReady():推迟DOM触发 read ...
- spring源码 继承AttributeAccessor的BeanDefinition接口
/** * A BeanDefinition describes a bean instance, which has property values, * constructor argument ...
随机推荐
- scala:函数作为值或参数进行传递、作为返回值进行返回
@ 目录 函数可以作为值进行传递 函数可以作为参数进行传递 函数可以作为返回值进行返回 什么是匿名函数 函数可以作为值进行传递 语法var f = 函数名 _ 如果明确了变量的数据类型,那么下划线可以 ...
- 记一个关于std::unordered_map并发访问的BUG
前言 刷题刷得头疼,水篇blog.这个BUG是我大约一个月前,在做15445实现lock_manager的时候遇到的一个很恶劣但很愚蠢的BUG,排查 + 摸鱼大概花了我三天的时间,根本原因是我在使用s ...
- Spark和Spring整合处理离线数据
如果你比较熟悉JavaWeb应用开发,那么对Spring框架一定不陌生,并且JavaWeb通常是基于SSM搭起的架构,主要用Java语言开发.但是开发Spark程序,Scala语言往往必不可少. 众所 ...
- python进阶(7)垃圾回收机制
Python垃圾回收 基于C语言源码底层,让你真正了解垃圾回收机制的实现 引用计数器 标记清除 分代回收 缓存机制 Python的C源码(3.8.2版本) 1.引用计数器 1.1环状双向链表 refc ...
- Mysql通过binlog恢复误update的数据
事件: 在生产库执行update时只添加了STATUS(状态)条件,将所有状态为'E'的数据全部改为了'D' 思路: 操作步骤主要参考自文章:https://blog.csdn.net/weixin_ ...
- 剑指 Offer 50. 第一个只出现一次的字符 + 哈希表 + 有序哈希表
剑指 Offer 50. 第一个只出现一次的字符 Offer_50 题目详情 方法一:使用无序哈希表 package com.walegarrett.offer; /** * @Author Wale ...
- Bullet碰撞检测
DBVT 在bullet 引擎中是很基础且重要的一个数据结构,本质上是一个可以动态更新的AABB树. 碰撞响应的分析 约束分类:可积约束,不可积约束 ,摩擦力(见[1]第四章) 整个bullet在动力 ...
- MySQL时间戳unix_timestamp
函数:FROM_UNIXTIME作用:将MYSQL中以INT(11)存储的时间以"YYYY-MM-DD"格式来显示.语法:FROM_UNIXTIME(unix_timestamp, ...
- C语言中指针和多维数组
指针和多维数组 数组名是特殊的指针 数组是一个特殊的指针,多维数组也是更为复杂的数组,它们的关系是什么样的呢? 我们通过一个简单的例子来比较形象的了解指针和多维数组: int a[2][3]; 这是一 ...
- WBX24T2X CPEX国产化万兆交换板
WBX24T2X是基于盛科CTC5160设计的国产化6U三层万兆CPEX交换板,提供24路千兆电口和2路万兆光口,采用龙芯 2K1000处理器.支持常规的L2/L3协议,支持Telnet.SNMP ...