引自 spring IoC源码分析 (3)Resource解析

定义好了Resource之后,看到XmlFactoryBean的构造函数

  1. public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
  2. super(parentBeanFactory);
  3. this.reader.loadBeanDefinitions(resource);
  4. }
跟到XmlBeanDefinitionReader 的 loadBeanDefinitions(EncodedResource encodedResource) 方法
  1. public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
  2. Assert.notNull(encodedResource, "EncodedResource must not be null");
  3. if (logger.isInfoEnabled()) {
  4. logger.info("Loading XML bean definitions from " + encodedResource.getResource());
  5. }
  6. //线程安全 ,但这里 currentResources应该本来就是线程安全的,所以推测不是为了线程安全
  7. //应该是为了线程能使用同一个 currentResources  ,从这里可以看出作者对 ThreadLocal 的理解深刻
  8. Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
  9. if (currentResources == null) {
  10. currentResources = new HashSet<EncodedResource>(4);
  11. this.resourcesCurrentlyBeingLoaded.set(currentResources);
  12. }
  13. //这里其实就是为了避免循环加载,如果重复加载了相同的文件就会抛出异常
  14. //这里看了半天才明白这个set的意图,蛋疼啊
  15. if (!currentResources.add(encodedResource)) {
  16. throw new BeanDefinitionStoreException(
  17. "Detected recursive loading of " + encodedResource + " - check your import definitions!");
  18. }
  19. try {
  20. InputStream inputStream = encodedResource.getResource().getInputStream();
  21. try {
  22. InputSource inputSource = new InputSource(inputStream);
  23. if (encodedResource.getEncoding() != null) {
  24. inputSource.setEncoding(encodedResource.getEncoding());
  25. }
  26. return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
  27. }
  28. finally {
  29. inputStream.close();
  30. }
  31. }
  32. catch (IOException ex) {
  33. throw new BeanDefinitionStoreException(
  34. "IOException parsing XML document from " + encodedResource.getResource(), ex);
  35. }
  36. finally {
  37. currentResources.remove(encodedResource);
  38. if (currentResources.isEmpty()) {
  39. this.resourcesCurrentlyBeingLoaded.set(null);
  40. }
  41. }
  42. }

其 实关键方法是 doLoadBeanDefinitions(inputSource, encodedResource.getResource()) ,但是上面的注释绝对花了我将近1个钟头才理解作者想表达的意思,刚开始一看到ThreadLocal 就想到线程安全,然后就想currentResources 永远是线程安全的啊,丫就这么被带坑里去了。从上面可以看到关键方法是doLoadBeanDefinitions,这个方法的关键代码其实就几行

  1. try {
  2. //判断xml文件是DTD还是XSD样式,如果没定义将使用XSD
  3. int validationMode = getValidationModeForResource(resource);
  4. Document doc = this.documentLoader.loadDocument(
  5. inputSource, getEntityResolver(), this.errorHandler, validationMode, isNamespaceAware());
  6. return registerBeanDefinitions(doc, resource);
  7. }

看下getValidationModeForResource 一路跟踪下来到XmlValidationModeDetector的detectValidationMode方法

  1. while ((content = reader.readLine()) != null) {
  2. content = consumeCommentTokens(content);
  3. if (this.inComment || !StringUtils.hasText(content)) {
  4. continue;
  5. }
  6. if (hasDoctype(content)) {
  7. isDtdValidated = true;
  8. break;
  9. }
  10. if (hasOpeningTag(content)) {
  11. // End of meaningful data...
  12. break;
  13. }
  14. }

就是分析下xml文件里有没有DOCTYPE关键字,没有的话就认为是xsd格式的。然后就到了documentLoader.loadDocument(
inputSource, getEntityResolver(), this.errorHandler, validationMode, isNamespaceAware()); 这个方法

  1. public Document loadDocument(InputSource inputSource, EntityResolver entityResolver,
  2. ErrorHandler errorHandler, int validationMode, boolean namespaceAware) throws Exception {
  3. DocumentBuilderFactory factory = createDocumentBuilderFactory(validationMode, namespaceAware);
  4. if (logger.isDebugEnabled()) {
  5. logger.debug("Using JAXP provider [" + factory.getClass().getName() + "]");
  6. }
  7. DocumentBuilder builder = createDocumentBuilder(factory, entityResolver, errorHandler);
  8. return builder.parse(inputSource);
  9. }

其实就是使用DocumentBuilderFactory 去解析xml,这块不怎么熟,查了下网上的介绍也没太详细的。

接着跟踪到registerBeanDefinitions 方法,关键部分:documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
  1. public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) {
  2. this.readerContext = readerContext;
  3. logger.debug("Loading bean definitions");
  4. Element root = doc.getDocumentElement();
  5. BeanDefinitionParserDelegate delegate = createHelper(readerContext, root);
  6. preProcessXml(root);
  7. parseBeanDefinitions(root, delegate);
  8. postProcessXml(root);
  9. }
preProcessXml和postProcessXml是提供扩展用得,这里没有具体实现,从字面上理解也是给之类提供预处理和事后处理用
的。具体解析工作是parseBeanDefinitions,跟踪到private void
parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate)
 这个方法,我们主要关注对bean的解析,所以直接看processBeanDefinition(ele, delegate)
  1. /**
  2. * Process the given bean element, parsing the bean definition
  3. * and registering it with the registry.
  4. */
  5. protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {
  6. /* BeanDefinitionHolder
    是BeanDefinition对象的封装类,封装了 BeanDefinition,Bean的名字和别名。用它来完成向IoC容器注
    册。 BeanDefinitionParserDelegate对XML元素的信息按照Spring的Bean规则进行解析*/
  7. BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
  8. if (bdHolder != null) {
  9. bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
  10. try {
  11. // Register the final decorated instance.
  12. BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
  13. }
  14. catch (BeanDefinitionStoreException ex) {
  15. getReaderContext().error("Failed to register bean definition with name '" +
  16. bdHolder.getBeanName() + "'", ele, ex);
  17. }
  18. // Send registration event.
  19. getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));
  20. }
  21. }


来看delegate.parseBeanDefinitionElement(ele)
方法,BeanDefinitionParserDelegate这个类 里包含了对各种Spring Bean定义规则的处理。比如我们最熟悉
的对Bean元素的处理是怎样完成的,也就是怎样处理在XML定义文件中出现的
<bean></bean>这个最常见的元素信息。在这里会看到对那些熟悉的BeanDefinition定义的处
理,比如id、name、aliase等属性元素。把这些元素的值从XML文件相应的元素的属性中读
取出来以后,设置到生成的BeanDefinitionHolder中去。这些属性的解析还是比较简单的。
对于其他元素配置的解析,比如各种Bean的属性配置,通过一个较为复杂的解析过程,这个
过程是由parseBeanDefinitionElement来完成的。解析完成以后,会把解析结果放到
BeanDefinition对象中并设置到BeanDefinitionHolder中去。其实就是根据spring自己对xml文件的定义进行解析。
这个 BeanDefinition数据对象中封装的数据大多都是与<bean>定义相关的,也有很多就是我们在定义Bean时看到
的那些Spring标记,比如常见的init-method、destroy-method、factory-method,等等,这个
BeanDefinition数据类型是非常重要的,它封装了很多基本数据,这些基本数据都是IoC容器
需要的。有了这些基本数据,IoC容器才能对Bean配置进行处理,才能实现相应的容器特性。spring对资源文件的解析和加载基本到此,下一篇继续分
析spring对bean的获取

贴一张spring 内部调用的图先

spring IoC源码分析 (3)Resource解析的更多相关文章

  1. Spring IOC 源码分析

    Spring 最重要的概念是 IOC 和 AOP,本篇文章其实就是要带领大家来分析下 Spring 的 IOC 容器.既然大家平时都要用到 Spring,怎么可以不好好了解 Spring 呢?阅读本文 ...

  2. Spring IoC 源码分析 (基于注解) 之 包扫描

    在上篇文章Spring IoC 源码分析 (基于注解) 一我们分析到,我们通过AnnotationConfigApplicationContext类传入一个包路径启动Spring之后,会首先初始化包扫 ...

  3. Spring Ioc源码分析系列--Ioc的基础知识准备

    Spring Ioc源码分析系列--Ioc的基础知识准备 本系列文章代码基于Spring Framework 5.2.x Ioc的概念 在Spring里,Ioc的定义为The IoC Containe ...

  4. Spring Ioc源码分析系列--Ioc源码入口分析

    Spring Ioc源码分析系列--Ioc源码入口分析 本系列文章代码基于Spring Framework 5.2.x 前言 上一篇文章Spring Ioc源码分析系列--Ioc的基础知识准备介绍了I ...

  5. Spring Ioc源码分析系列--Ioc容器注册BeanPostProcessor后置处理器以及事件消息处理

    Spring Ioc源码分析系列--Ioc容器注册BeanPostProcessor后置处理器以及事件消息处理 前言 上一篇分析了BeanFactoryPostProcessor的作用,那么这一篇继续 ...

  6. Spring Ioc源码分析系列--前言

    Spring Ioc源码分析系列--前言 为什么要写这个系列文章 首先这是我个人很久之前的一个计划,拖了很久没有实施,现在算是填坑了.其次,作为一个Java开发者,Spring是绕不开的课题.在Spr ...

  7. Spring Ioc源码分析系列--Bean实例化过程(一)

    Spring Ioc源码分析系列--Bean实例化过程(一) 前言 上一篇文章Spring Ioc源码分析系列--Ioc容器注册BeanPostProcessor后置处理器以及事件消息处理已经完成了对 ...

  8. Spring Ioc源码分析系列--Bean实例化过程(二)

    Spring Ioc源码分析系列--Bean实例化过程(二) 前言 上篇文章Spring Ioc源码分析系列--Bean实例化过程(一)简单分析了getBean()方法,还记得分析了什么吗?不记得了才 ...

  9. Spring Ioc源码分析系列--@Autowired注解的实现原理

    Spring Ioc源码分析系列--@Autowired注解的实现原理 前言 前面系列文章分析了一把Spring Ioc的源码,是不是云里雾里,感觉并没有跟实际开发搭上半毛钱关系?看了一遍下来,对我的 ...

随机推荐

  1. 一个Sql备注

    ;WITH CTE AS ( SELECT [RGTCID] ,ltrim(rtrim(T.N.value('.','varchar(100)'))) as Querystr FROM [R_GT_C ...

  2. Job-Show Liang,你来掌管诺基亚王国,可好?

    保留我一向高大上风格,开头当然来一个段子 在即将到来MWC(Mobile World Congress缩写,世界移动通信大会),很高兴能听到小诺来参展,我不得不给它32个赞,因为小诺已经好几届没有浮头 ...

  3. web端访问文件没有权限的问题

    背景 : ftp的PHP项目中的某些文件没有写入的权限..系统报注意错误!!! 原因 : 一般情况下,web端访问网站一般使用的是WWW权限(有限制的权限组)去访问, 但是我们编程开发的时候, 有可能 ...

  4. Vue生命周期函数

    生命周期函数: 组件挂载,以及组件更新,组建销毁的时候出发的一系列方法. beforeCreate:实例创建之前 created:实例创建完成 beforeMount:模板编译之前 mounted:模 ...

  5. BumpMap、NormalMap的区别

    原文:http://linuxtest.blog.163.com/blog/static/199927088201275102145354/   一种是Emboss Bump Map(浮雕凹凸贴图), ...

  6. java -jar jar包,运行报错没有主清单和无法加载主类

    jar: 包名(class 文件) META-INF(MANIFEST.MF ) .classpath 1.从eclipse直接导出的jar包: 2.修改MANIFEST.MF文件:

  7. OC语言自定义打印

    1.为了全文通用,选择在PCH文件中写: // // 版权所有:Copyright © 2018年 Lelight. All rights reserved. // 创 建 者: Lelight // ...

  8. Java的引用和C++的指针de区别

    Java的引用和C++的指针都是指向一块内存地址的,通过引用或指针来完成对内存数据的操作,就好像风筝的线轴一样,通过线轴总是能够找到风筝,但是它们在实现,原理作用等方面却有区别. (1)类型:引用其值 ...

  9. request对象常用方法

    String getParameter(String name)根据表单组件名称获取提交数据 Sring[] getParameterValues(String name)获取表单组件对应多个值时的请 ...

  10. ML理论知识博文列表

    一文弄懂神经网络中的反向传播法——BackPropagation:http://www.cnblogs.com/charlotte77/p/5629865.html 人脸识别图库:http://vis ...