这里先从最简单的一个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源码学习(一)资源加载的更多相关文章

  1. Mybatis源码学习之资源加载(六)

    类加载器简介 Java虚拟机中的类加载器(ClassLoader)负责加载来自文件系统.网络或其他来源的类文件.Java虚拟机中的类加载器默认使用的是双亲委派模式,如图所示,其中有三种默认使用的类加载 ...

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

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

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

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

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

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

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

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

  6. flutter源码学习笔记-图片加载流程

    本文基于1.12.13+hotfix.8版本源码分析. 0.大纲 Image ImageProvider 图片数据加载 ImageStream.ImageStreamCompleter 缓存池 Pai ...

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

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

  8. 五、spring源码阅读之ClassPathXmlApplicationContext加载beanFactory

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml&q ...

  9. Spring源码学习-容器BeanFactory(一) BeanDefinition的创建-解析资源文件

    写在前面 从大四实习至今已一年有余,作为一个程序员,一直没有用心去记录自己工作中遇到的问题,甚是惭愧,打算从今日起开始养成写博客的习惯.作为一名java开发人员,Spring是永远绕不过的话题,它的设 ...

  10. Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签

    写在前面 上文Spring源码学习-容器BeanFactory(二) BeanDefinition的创建-解析前BeanDefinition的前置操作中Spring对XML解析后创建了对应的Docum ...

随机推荐

  1. 刷题总结——array(ssoj)

    题目: 题目描述 给定 2 个正整数序列 A1, A2,序列长度分别为 L1, L2.你可以进行以下的一次操作:1. 选择两个数 K1,K2(1≤K1≤L1, 1≤K2≤L2):2. 移去 A1 中最 ...

  2. P1260 工程规划 (差分约束)

    题目链接 Solution 差分约束. 差分约束似乎精髓就两句话: 当我们把不等式整理成 \(d[a]+w<=d[b]\) 时,我们求最长路. 整理成 \(d[a]+w>=d[b]\) 时 ...

  3. 【bzoj2440】[中山市选2011]完全平方数 莫比乌斯反演

    Description 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平方数.他觉得这些数看起来很令人难受.由此,他也讨厌所有是完全平方数的正整数倍的数.然而这丝毫不影响他对其他数的热爱.这天是小 ...

  4. TroubleShoot: SPD 2013 工作流模板问题解决办法

    1. 问题描述: SPD 2013 不能使用2013 工作流模板,在创建过程中,下载更新信息时出现以下错误描述: The server has tried to deliver this messag ...

  5. mybatis原理源码大牛连接

    mybatis讲解的非常好的连接: https://www.jianshu.com/nb/5226994 执行流程简介参考:http://www.cnblogs.com/dongying/p/4142 ...

  6. Firmware 加载原理分析【转】

    转自:http://blog.csdn.net/dxdxsmy/article/details/8669840 [-] 原理分析 实现机制 总结   前言 前段时间移植 wifi 驱动到 Androi ...

  7. Linux 虚拟地址与物理地址的映射关系分析【转】

    转自:http://blog.csdn.net/ordeder/article/details/41630945 版权声明:本文为博主(http://blog.csdn.net/ordeder)原创文 ...

  8. hdu 4932 /bestcoder B题 #4 /思维题

    题意:给一个数列(整数),用一些不相交的区间去覆盖(只能是用端点去覆盖,端点可以交).而且区间出度相等.求最大区间长度. 开始一下就敲了,枚举每个区间长度,判断合法,更新最大.但是后来一看小数,感觉不 ...

  9. 牛客网 Wannafly挑战赛9 C.列一列-sscanf()函数

      C.列一列   时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 262144K,其他语言524288K64bit IO Format: %lld 链接:https://www.now ...

  10. ubuntu下打开windows里的txt文件乱码解决

    是编码问题引起的问题: Linux下默认的编码是UTF-8,而Windows下默认的编码是GB2312/GBK.执行如下第一条语句即可 gsettings set org.gnome.gedit.pr ...