Spring源码学习(一)资源加载
这里先从最简单的一个Spring例子开始。
下面是Spring的context的配置
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:context="http://www.springframework.org/schema/context"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://www.springframework.org/schema/context
6 http://www.springframework.org/schema/context/spring-context.xsd
7 http://www.springframework.org/schema/beans
8 http://www.springframework.org/schema/beans/spring-beans.xsd">
9
10 <!-- 自动扫描web包 ,将带有注解的类 纳入spring容器管理 -->
11 <context:component-scan base-package="com.tuhooo.practice"></context:component-scan>
12 </beans>
一个简单的Service类,MyBean.java
1 @Service
2 public class MyBean {
3 public void printStr() {
4 System.out.println("你好吗......");
5 }
6 }
main方法中获取bean,并调用MyBean.java中的方法。
1 public class App {
2 public static void main( String[] args ) {
3 XmlBeanFactory applicationContext = new XmlBeanFactory(new ClassPathResource("spring-config.xml"));
4 MyBean myBean = (MyBean) applicationContext.getBean("myBean");
5 myBean.printStr();
6 }
7 }
当我在使用XmlBeanFactory这个类的时候,已经被标记为@deprecated,也就是这个类已经被抛弃了,但是并不妨碍它作为我们滴入口类进行分析呀。
XmlBeanFactory.java
1 @Deprecated
2 @SuppressWarnings({"serial", "all"})
3 public class XmlBeanFactory extends DefaultListableBeanFactory {
4
5 /**
6 * 加载Bean定义的Reader
7 */
8 private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);
9
10 /**
11 * 通过给定的可通过DOM解析的资源来创建一个XmlBeanFactory
12 * @param resource 用来加载bean定义的XML资源
13 * @throws BeansException 加载或者解析时候的错误
14 */
15 public XmlBeanFactory(Resource resource) throws BeansException {
16 this(resource, null);
17 }
18
19 /**
20 * 通过给定的可通过DOM解析的输入流来创建一个XmlBeanFactory
21 * @param resource 用来加载bean定义的XML资源
22 * @param parentBeanFactory 父代的bean factory
23 * @throws BeansException 加载或者解析时候的错误
24 */
25 public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
26 super(parentBeanFactory);
27 this.reader.loadBeanDefinitions(resource); /*这个是我们要进入, 并分析的方法*/
28 }
29 }

接下来看一下XmlBeanDefinitionReader中的loadBeanDefinitions(resource)的逻辑是怎样的。
XmlBeanDefinitionReader.loadBeanDefinitions(resource)
XmlBeanDefinitionReader的类图如下:

下面是XmlBeanDefinitionReader.java这个类截取的一部分:
public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
/**
* Load bean definitions from the specified XML file.
* @param resource the resource descriptor for the XML file
* @return the number of bean definitions found
* @throws BeanDefinitionStoreException in case of loading or parsing errors
*/
@Override
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
return loadBeanDefinitions(new EncodedResource(resource));
}
/**
* Load bean definitions from the specified XML file.
* @param encodedResource the resource descriptor for the XML file,
* allowing to specify an encoding to use for parsing the file
* @return the number of bean definitions found
* @throws BeanDefinitionStoreException in case of loading or parsing errors
*/
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
Assert.notNull(encodedResource, "EncodedResource must not be null");
if (logger.isInfoEnabled()) {
logger.info("Loading XML bean definitions from " + encodedResource.getResource());
}
Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
if (currentResources == null) {
currentResources = new HashSet<>(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();
try {
InputSource inputSource = new InputSource(inputStream);
if (encodedResource.getEncoding() != null) {
inputSource.setEncoding(encodedResource.getEncoding());
}
return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
}
finally {
inputStream.close();
}
} catch (IOException ex) {
throw new BeanDefinitionStoreException(
"IOException parsing XML document from " + encodedResource.getResource(), ex);
} finally {
currentResources.remove(encodedResource);
if (currentResources.isEmpty()) {
this.resourcesCurrentlyBeingLoaded.remove();
}
}
}
}
可以对着这个时序图,看来下面的代码:


下面我们进入到XmlBeanDefinitionReader.loadBeanDefinitions(EncodedResource encodedResource)方法内部看一下。

这里EncodedResource验重的逻辑是:
public class EncodedResource implements InputStreamSource {
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof EncodedResource)) {
return false;
}
EncodedResource otherResource = (EncodedResource) other;
return (this.resource.equals(otherResource.resource) &&
ObjectUtils.nullSafeEquals(this.charset, otherResource.charset) &&
ObjectUtils.nullSafeEquals(this.encoding, otherResource.encoding));
}
}
这个loadBeanDefinitions方法还是很清晰的,你说像我这种制杖都看懂了,就想说一句,还有谁看不懂~~~
但仍有几个地方需要后续再跟进看一下:
1. 这里有个局部线程变量来存放正在加载的资源,this.resourceCurrentlyBeingLoaded.get(),可以我们看到线程在哪儿?
2. 你说搞encodedResource我理解,但是为什么在doLoadBeanDefinitions方法中既有inputSource,又有encodedResource,不会显得很多余么?
XmlBeanDefinitionReader.doLoadBeanDefinitions(InputSource, Resource)
1 public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
2
3 /**
4 * Actually load bean definitions from the specified XML file.
5 * @param inputSource the SAX InputSource to read from
6 * @param resource the resource descriptor for the XML file
7 * @return the number of bean definitions found
8 * @throws BeanDefinitionStoreException in case of loading or parsing errors
9 * @see #doLoadDocument
10 * @see #registerBeanDefinitions
11 */
12 protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
13 throws BeanDefinitionStoreException {
14 try {
15 Document doc = doLoadDocument(inputSource, resource);
16 return registerBeanDefinitions(doc, resource);
17 }
18 catch (BeanDefinitionStoreException ex) {
19 throw ex;
20 }
21 catch (SAXParseException ex) {
22 throw new XmlBeanDefinitionStoreException(resource.getDescription(),
23 "Line " + ex.getLineNumber() + " in XML document from " + resource + " is invalid", ex);
24 }
25 catch (SAXException ex) {
26 throw new XmlBeanDefinitionStoreException(resource.getDescription(),
27 "XML document from " + resource + " is invalid", ex);
28 }
29 catch (ParserConfigurationException ex) {
30 throw new BeanDefinitionStoreException(resource.getDescription(),
31 "Parser configuration exception parsing XML from " + resource, ex);
32 }
33 catch (IOException ex) {
34 throw new BeanDefinitionStoreException(resource.getDescription(),
35 "IOException parsing XML document from " + resource, ex);
36 }
37 catch (Throwable ex) {
38 throw new BeanDefinitionStoreException(resource.getDescription(),
39 "Unexpected exception parsing XML document from " + resource, ex);
40 }
41 }
42 }
最终把硬盘上的配置文件以Document的形式读到了内存中。

XmlBeanDefinitionReader.registerBeanDefinitions(Document, Resource)
1 public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
2
3 public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
4 BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
5 int countBefore = getRegistry().getBeanDefinitionCount();
6 documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
7 return getRegistry().getBeanDefinitionCount() - countBefore;
8 }
9
10 /**
11 * Create the {@link BeanDefinitionDocumentReader} to use for actually
12 * reading bean definitions from an XML document.
13 * <p>The default implementation instantiates the specified "documentReaderClass".
14 * @see #setDocumentReaderClass
15 */
16 protected BeanDefinitionDocumentReader createBeanDefinitionDocumentReader() {
17 return BeanDefinitionDocumentReader.class.cast(BeanUtils.instantiateClass(this.documentReaderClass));
18 }
19
20 /**
21 * Create the {@link XmlReaderContext} to pass over to the document reader.
22 */
23 public XmlReaderContext createReaderContext(Resource resource) {
24 return new XmlReaderContext(resource, this.problemReporter, this.eventListener,
25 this.sourceExtractor, this, getNamespaceHandlerResolver());
26 }
27 }
28
首先不看别的,直接看这段代码就行:
1. 构造一个BeanDefinitionDocumentReader,然后用它来注册BeanDefinition,它还需要一个读取时候的上下文;
2. 返回的是新加入Registry的BeanDefinition的个数
3. createBeanDefinitionDocumentReader的作用还没看太明白

DefaultBeanDefinitionDocumentReader.registerBeanDefinitions

再去看一下doRegisterBeanDefinitions方法

真正处理的方法又流转到了parseBeanDefinitions中,但是我没明白delegate是干啥啊?
Resource的类图

public class ClassPathResource extends AbstractFileResolvingResource {
/**
* Create a new {@code ClassPathResource} for {@code ClassLoader} usage.
* A leading slash will be removed, as the ClassLoader resource access
* methods will not accept it.
* <p>The thread context class loader will be used for
* loading the resource.
* @param path the absolute path within the class path
* @see java.lang.ClassLoader#getResourceAsStream(String)
* @see org.springframework.util.ClassUtils#getDefaultClassLoader()
*/
public ClassPathResource(String path) {
this(path, (ClassLoader) null);
}
/**
* Create a new {@code ClassPathResource} for {@code ClassLoader} usage.
* A leading slash will be removed, as the ClassLoader resource access
* methods will not accept it.
* @param path the absolute path within the classpath
* @param classLoader the class loader to load the resource with,
* or {@code null} for the thread context class loader
* @see ClassLoader#getResourceAsStream(String)
*/
public ClassPathResource(String path, @Nullable ClassLoader classLoader) {
Assert.notNull(path, "Path must not be null");
String pathToUse = StringUtils.cleanPath(path);
if (pathToUse.startsWith("/")) {
pathToUse = pathToUse.substring(1);
}
this.path = pathToUse;
this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
}
}
在代码new ClassPathResource("beanFactoryTest.xml");中其实没有啥动作,就是对ClassPathResource的几个属性赋值了,尤其是类加载器。也就是说这个时候其实并没有读取文件内容。
Spring源码学习(一)资源加载的更多相关文章
- Mybatis源码学习之资源加载(六)
类加载器简介 Java虚拟机中的类加载器(ClassLoader)负责加载来自文件系统.网络或其他来源的类文件.Java虚拟机中的类加载器默认使用的是双亲委派模式,如图所示,其中有三种默认使用的类加载 ...
- 【Spring源码分析】Bean加载流程概览
代码入口 之前写文章都会啰啰嗦嗦一大堆再开始,进入[Spring源码分析]这个板块就直接切入正题了. 很多朋友可能想看Spring源码,但是不知道应当如何入手去看,这个可以理解:Java开发者通常从事 ...
- 【Spring源码分析】Bean加载流程概览(转)
转载自:https://www.cnblogs.com/xrq730/p/6285358.html 代码入口 之前写文章都会啰啰嗦嗦一大堆再开始,进入[Spring源码分析]这个板块就直接切入正题了. ...
- Spring源码分析:Bean加载流程概览及配置文件读取
很多朋友可能想看Spring源码,但是不知道应当如何入手去看,这个可以理解:Java开发者通常从事的都是Java Web的工作,对于程序员来说,一个Web项目用到Spring,只是配置一下配置文件而已 ...
- 【Spring源码分析系列】加载Bean
/** * Create a new XmlBeanFactory with the given input stream, * which must be parsable using DOM. * ...
- flutter源码学习笔记-图片加载流程
本文基于1.12.13+hotfix.8版本源码分析. 0.大纲 Image ImageProvider 图片数据加载 ImageStream.ImageStreamCompleter 缓存池 Pai ...
- Spring源码解析-配置文件的加载
spring是一个很有名的java开源框架,作为一名javaer还是有必要了解spring的设计原理和机制,beans.core.context作为spring的三个核心组件.而三个组件中最重要的就是 ...
- 五、spring源码阅读之ClassPathXmlApplicationContext加载beanFactory
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml&q ...
- Spring源码学习-容器BeanFactory(一) BeanDefinition的创建-解析资源文件
写在前面 从大四实习至今已一年有余,作为一个程序员,一直没有用心去记录自己工作中遇到的问题,甚是惭愧,打算从今日起开始养成写博客的习惯.作为一名java开发人员,Spring是永远绕不过的话题,它的设 ...
- Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签
写在前面 上文Spring源码学习-容器BeanFactory(二) BeanDefinition的创建-解析前BeanDefinition的前置操作中Spring对XML解析后创建了对应的Docum ...
随机推荐
- 基于openstack stable queens版本阅读解析
基于openstack stable queens版本阅读解析 基于 centos7.5 的linux系统 架构 如下所示,为cinder的官方架构说明: 这里写图片描述 各个组件介绍如下: - DB ...
- animation总结
1. animation结束后停在最后一帧 animation-fill-mode : forwards | both; /* 或者 */ animation: anim1 1s linear for ...
- Server-Side Rendering(服务端渲染)的优点与缺点
优点 1. SEO 客户端渲染,页面中只有初始的几个html容器,js生成内容填充到容器中,爬虫只能识别到初始的html容器,js生成的内容一般不会被识别,而服务端渲染直接给出html,爬虫可以识别到 ...
- 线程与threading模块
线程 进程内一个相对独立的.可调度的执行单元,是系统独立调度和分派CPU的基本单位.在单个进程中同时运行多个线程完成不同的工作,称为多线程. 同一进程内的多个线程是共享该进程的资源. 创建新的线程开销 ...
- JSON 序列化与弱类型
一.C#中JSON序列化有多种方式: 使用“DataContractJsonSerializer ”类时需要, 1.引用程序集 System.Runtime.Serialization 和 Syste ...
- 从反汇编看待C++ new
首先来看最简单的new操作 int main() { int *temp = new int; delete temp; } 反汇编结果:调用了operator new 00311C9E push 4 ...
- 算法 & 数据结构——任意多边形填充
需求 . 在计算机中,选区是一个很常见的功能,例如windows按住鼠标左键拖动划出矩形选区,Photshop通过钢笔工具任意形状选区.选区本身不过是通过线段闭合的一个几何形状,但是如何填充这个选区, ...
- CodeForces 380.C Sereja and Brackets
题意 一串括号序列,只由(和)组成,然后是m个提问,提问l和r区间内,最大的匹配匹配括号数. 思路 第一,贪心的思想,用最正常的方式去尽量匹配,详细点说就是,先找到所有的(),然后删除这些(),再找所 ...
- 使用Naive Bayes从个人广告中获取区域倾向
RSS源介绍:https://zhidao.baidu.com/question/2051890587299176627.html http://www.rssboard.org/rss-profil ...
- Codeforces Gym101606 A.Alien Sunset (2017 United Kingdom and Ireland Programming Contest (UKIEPC 2017))
2017 United Kingdom and Ireland Programming Contest (UKIEPC 2017) 寒假第一次组队训练赛,和学长一起训练,题目难度是3颗星,我和猪队友写 ...