引自 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. this关键字剖析

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. 如何在DOS下以管理员身份执行命令?

    原创 普通User的DOS窗口: 以管理员身份运行的DOS窗口: 转换(Windows10系统下): 13:11:55 2018-10-18

  3. Windows下配置Visualsvn Server时需要注意的几点事项

    1配置用户组与用户 用户组的权限高于用户的权限, 如果一个用户只有只读权限,同时被加入了拥有写权限的用户组中,此用户可以执行写操作. 2在Pre-commit hook下增加 强制添加注释的钩子脚本 ...

  4. angular 重定向路由

    const routes: Routes = [ { path: '', redirectTo: '/home', pathMatch: 'full' }, { path: 'home', compo ...

  5. 如何使用 channel

    如何使用 Channel 例子来自于Concurrency is not parallelism Google Search: A fake framework v1.0 var ( Web = fa ...

  6. Node简单服务器开发

    运用的知识:http,fs,get,post 接口定义:/user?act=reg$user=aaa&pass=bbb后台返回格式:{"ok":false,"ms ...

  7. openstack组件服务的入口寻找方法

    在centos7系统上,安装openstack服务以后,可以通过以下命令,查找到该系统上,已经安装的openstack服务 [root@xzto01n010027244133 ~]# systemct ...

  8. C#下Excel的普通处理和报表设计

    一.准备:想要操作Excel,先要在添加2个COM引用: 1.Microsoft Excel 14.0 Object Library (版本不同14.0也可能是12.0等) 2.Microsoft O ...

  9. Django REST framework序列化

    一.简介 Django REST framework是基于Django实现的一个RESTful风格API框架,能够帮助我们快速开发RESTful风格的API. 官网:https://www.djang ...

  10. POJ-3468-A Simple Problem with Integers(线段树 区间更新 区间和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 139191 ...