Spring源码学习(二)

  • A+
所属分类:Spring

上一节我总结了Spring加截bean的大致过程。这一节来总结下Spring解析默认标签的过程。

  1. private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) {
  2. if (delegate.nodeNameEquals(ele, IMPORT_ELEMENT)) {
  3. importBeanDefinitionResource(ele);
  4. }
  5. else if (delegate.nodeNameEquals(ele, ALIAS_ELEMENT)) {
  6. processAliasRegistration(ele);
  7. }
  8. else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) {
  9. processBeanDefinition(ele, delegate);
  10. }
  11. else if (delegate.nodeNameEquals(ele, NESTED_BEANS_ELEMENT)) {
  12. // recurse
  13. doRegisterBeanDefinitions(ele);
  14. }
  15. }
private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) {
  if (delegate.nodeNameEquals(ele, IMPORT_ELEMENT)) {
    importBeanDefinitionResource(ele);
  }
  else if (delegate.nodeNameEquals(ele, ALIAS_ELEMENT)) {
    processAliasRegistration(ele);
  }
  else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) {
    processBeanDefinition(ele, delegate);
  }
  else if (delegate.nodeNameEquals(ele, NESTED_BEANS_ELEMENT)) {
    // recurse
    doRegisterBeanDefinitions(ele);
  }
}

从上面的代码可以看到,分别用四个函数来解析默认标签,分别解析了import标签,alias标签,bean标签,beans标签。

首先看下bean标签的解析。

  1. /**
  2. * Process the given bean element, parsing the bean <a href="http://zpycloud.com/archives/tag/definition/" title="查看与 definition 相关的文章" target="_blank">definition</a>
  3. * and <a href="http://zpycloud.com/archives/tag/register/" title="查看与 register 相关的文章" target="_blank">register</a>ing it with the registry.
  4. */
  5. protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {
  6. BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
  7. if (bdHolder != null) {
  8. bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
  9. try {
  10. // Register the final decorated instance.
  11. BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
  12. }
  13. catch (BeanDefinitionStoreException ex) {
  14. getReaderContext().error("Failed to register bean definition with name '" +
  15. bdHolder.getBeanName() + "'", ele, ex);
  16. }
  17. // Send registration event.
  18. getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));
  19. }
  20. }
/**
   * Process the given bean element, parsing the bean <a href="http://zpycloud.com/archives/tag/definition/" title="查看与 definition 相关的文章" target="_blank">definition</a>
   * and <a href="http://zpycloud.com/archives/tag/register/" title="查看与 register 相关的文章" target="_blank">register</a>ing it with the registry.
   */
  protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {
    BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
    if (bdHolder != null) {
      bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
      try {
        // Register the final decorated instance.
        BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
      }
      catch (BeanDefinitionStoreException ex) {
        getReaderContext().error("Failed to register bean definition with name '" +
            bdHolder.getBeanName() + "'", ele, ex);
      }
      // Send registration event.
      getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));
    }
  }

从上面代码可以看出,整个processBeanDefinition函数做了三件事

  • 解析bean
  • 装饰bean
  • 注册bean
  1. /**
  2. * Parse the bean definition itself, without regard to name or aliases. May return
  3. * {@code null} if problems occurred during the parsing of the bean definition.
  4. */
  5. @Nullable
  6. public AbstractBeanDefinition parseBeanDefinitionElement(
  7. Element ele, String beanName, @Nullable BeanDefinition containingBean) {
  8. this.parseState.push(new BeanEntry(beanName));
  9. String className = null;
  10. if (ele.hasAttribute(CLASS_ATTRIBUTE)) {
  11. className = ele.getAttribute(CLASS_ATTRIBUTE).trim();
  12. }
  13. String parent = null;
  14. if (ele.hasAttribute(PARENT_ATTRIBUTE)) {
  15. parent = ele.getAttribute(PARENT_ATTRIBUTE);
  16. }
  17. try {
  18. // 创建继承于AbstractBeanDefinition 的GenericBeanDefinition对象
  19. AbstractBeanDefinition bd = createBeanDefinition(className, parent);
  20. // 解析bean的各种属性
  21. parseBeanDefinitionAttributes(ele, beanName, containingBean, bd);
  22. bd.setDescription(DomUtils.getChildElementValueByTagName(ele, DESCRIPTION_ELEMENT));
  23. // 解析meta属性
  24. parseMetaElements(ele, bd);
  25. // 解析lookup-method属性
  26. parseLookupOverrideSubElements(ele, bd.getMethodOverrides());
  27. // 解析replace-method属性
  28. parseReplacedMethodSubElements(ele, bd.getMethodOverrides());
  29. // 解析构造函数参数
  30. parseConstructorArgElements(ele, bd);
  31. // 解析property子元素
  32. parsePropertyElements(ele, bd);
  33. // 解析qualifier子元素
  34. parseQualifierElements(ele, bd);
  35. bd.setResource(this.readerContext.getResource());
  36. bd.setSource(extractSource(ele));
  37. return bd;
  38. }
  39. catch (ClassNotFoundException ex) {
  40. error("Bean class [" + className + "] not found", ele, ex);
  41. }
  42. catch (NoClassDefFoundError err) {
  43. error("Class that bean class [" + className + "] depends on not found", ele, err);
  44. }
  45. catch (Throwable ex) {
  46. error("Unexpected failure during bean definition parsing", ele, ex);
  47. }
  48. finally {
  49. this.parseState.pop();
  50. }
  51. return null;
  52. }
/**
   * Parse the bean definition itself, without regard to name or aliases. May return
   * {@code null} if problems occurred during the parsing of the bean definition.
   */
  @Nullable
  public AbstractBeanDefinition parseBeanDefinitionElement(
      Element ele, String beanName, @Nullable BeanDefinition containingBean) {

    this.parseState.push(new BeanEntry(beanName));

    String className = null;
    if (ele.hasAttribute(CLASS_ATTRIBUTE)) {
      className = ele.getAttribute(CLASS_ATTRIBUTE).trim();
    }
    String parent = null;
    if (ele.hasAttribute(PARENT_ATTRIBUTE)) {
      parent = ele.getAttribute(PARENT_ATTRIBUTE);
    }

    try {
      // 创建继承于AbstractBeanDefinition 的GenericBeanDefinition对象
      AbstractBeanDefinition bd = createBeanDefinition(className, parent);

      // 解析bean的各种属性
      parseBeanDefinitionAttributes(ele, beanName, containingBean, bd);
      bd.setDescription(DomUtils.getChildElementValueByTagName(ele, DESCRIPTION_ELEMENT));

      // 解析meta属性
      parseMetaElements(ele, bd);
      
      // 解析lookup-method属性
      parseLookupOverrideSubElements(ele, bd.getMethodOverrides());
     
      // 解析replace-method属性
      parseReplacedMethodSubElements(ele, bd.getMethodOverrides());
  
      // 解析构造函数参数
      parseConstructorArgElements(ele, bd);
      
      // 解析property子元素
      parsePropertyElements(ele, bd);

      // 解析qualifier子元素
      parseQualifierElements(ele, bd);

      bd.setResource(this.readerContext.getResource());
      bd.setSource(extractSource(ele));

      return bd;
    }
    catch (ClassNotFoundException ex) {
      error("Bean class [" + className + "] not found", ele, ex);
    }
    catch (NoClassDefFoundError err) {
      error("Class that bean class [" + className + "] depends on not found", ele, err);
    }
    catch (Throwable ex) {
      error("Unexpected failure during bean definition parsing", ele, ex);
    }
    finally {
      this.parseState.pop();
    }

    return null;
  }

上面这一段则是解析bean的核心代码

BeanDefiniton是一个接口,在Spring中有三种实现,分别是RootBeanDefinition,ChildBeanDefinition,GenericBeanDefinition。三种实现都继承了AbstractBeanDefinition。

而BeanDefiniton是配置文件中<bean>标签在容器中的内部表示形式。与<bean>里的属性是一一对应的.GenericBeanDefinition是2.5版本后新加入的类,是一站式服务类.

下面再来看一下bean的注册过程.

  1. /**
  2. * Register the given bean definition with the given bean factory.
  3. * @param definitionHolder the bean definition including name and aliases
  4. * @param registry the bean factory to register with
  5. * @throws BeanDefinitionStoreException if registration failed
  6. */
  7. public static void registerBeanDefinition(
  8. BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
  9. throws BeanDefinitionStoreException {
  10. // Register bean definition under primary name.
  11. String beanName = definitionHolder.getBeanName();
  12. registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());
  13. // Register aliases for bean name, if any.
  14. String[] aliases = definitionHolder.getAliases();
  15. if (aliases != null) {
  16. for (String alias : aliases) {
  17. registry.registerAlias(beanName, alias);
  18. }
  19. }
  20. }
/**
   * Register the given bean definition with the given bean factory.
   * @param definitionHolder the bean definition including name and aliases
   * @param registry the bean factory to register with
   * @throws BeanDefinitionStoreException if registration failed
   */
  public static void registerBeanDefinition(
      BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
      throws BeanDefinitionStoreException {

    // Register bean definition under primary name.
    String beanName = definitionHolder.getBeanName();
    registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());

    // Register aliases for bean name, if any.
    String[] aliases = definitionHolder.getAliases();
    if (aliases != null) {
      for (String alias : aliases) {
        registry.registerAlias(beanName, alias);
      }
    }
  }

可以看到,整个bean的注册分为了一般bean的注册和别名的注册两个部分。

BeanDefinitionRegistry只是一个接口,真正的实现方法在DefaultListableBeanFactory类里。

  1. //---------------------------------------------------------------------
  2. // Implementation of BeanDefinitionRegistry interface
  3. //---------------------------------------------------------------------
  4. @Override
  5. public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
  6. throws BeanDefinitionStoreException {
  7. Assert.hasText(beanName, "Bean name must not be empty");
  8. Assert.notNull(beanDefinition, "BeanDefinition must not be null");
  9. if (beanDefinition instanceof AbstractBeanDefinition) {
  10. try {
  11. ((AbstractBeanDefinition) beanDefinition).validate();
  12. }
  13. catch (BeanDefinitionValidationException ex) {
  14. throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
  15. "Validation of bean definition failed", ex);
  16. }
  17. }
  18. BeanDefinition existingDefinition = this.beanDefinitionMap.get(beanName);
  19. if (existingDefinition != null) {
  20. if (!isAllowBeanDefinitionOverriding()) {
  21. throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
  22. "Cannot register bean definition [" + beanDefinition + "] for bean '" + beanName +
  23. "': There is already [" + existingDefinition + "] bound.");
  24. }
  25. else if (existingDefinition.getRole() < beanDefinition.getRole()) {
  26. // e.g. was ROLE_APPLICATION, now overriding with ROLE_SUPPORT or ROLE_INFRASTRUCTURE
  27. if (logger.isWarnEnabled()) {
  28. logger.warn("Overriding user-defined bean definition for bean '" + beanName +
  29. "' with a framework-generated bean definition: replacing [" +
  30. existingDefinition + "] with [" + beanDefinition + "]");
  31. }
  32. }
  33. else if (!beanDefinition.equals(existingDefinition)) {
  34. if (logger.isInfoEnabled()) {
  35. logger.info("Overriding bean definition for bean '" + beanName +
  36. "' with a different definition: replacing [" + existingDefinition +
  37. "] with [" + beanDefinition + "]");
  38. }
  39. }
  40. else {
  41. if (logger.isDebugEnabled()) {
  42. logger.debug("Overriding bean definition for bean '" + beanName +
  43. "' with an equivalent definition: replacing [" + existingDefinition +
  44. "] with [" + beanDefinition + "]");
  45. }
  46. }
  47. this.beanDefinitionMap.put(beanName, beanDefinition);
  48. }
  49. else {
  50. if (hasBeanCreationStarted()) {
  51. // Cannot modify startup-time collection elements anymore (for stable iteration)
  52. synchronized (this.beanDefinitionMap) {
  53. this.beanDefinitionMap.put(beanName, beanDefinition);
  54. List<String> updatedDefinitions = new ArrayList<>(this.beanDefinitionNames.size() + 1);
  55. updatedDefinitions.addAll(this.beanDefinitionNames);
  56. updatedDefinitions.add(beanName);
  57. this.beanDefinitionNames = updatedDefinitions;
  58. if (this.manualSingletonNames.contains(beanName)) {
  59. Set<String> updatedSingletons = new LinkedHashSet<>(this.manualSingletonNames);
  60. updatedSingletons.remove(beanName);
  61. this.manualSingletonNames = updatedSingletons;
  62. }
  63. }
  64. }
  65. else {
  66. // Still in startup registration phase
  67. this.beanDefinitionMap.put(beanName, beanDefinition);
  68. this.beanDefinitionNames.add(beanName);
  69. this.manualSingletonNames.remove(beanName);
  70. }
  71. this.frozenBeanDefinitionNames = null;
  72. }
  73. if (existingDefinition != null || containsSingleton(beanName)) {
  74. resetBeanDefinition(beanName);
  75. }
  76. }
//---------------------------------------------------------------------
  // Implementation of BeanDefinitionRegistry interface
  //---------------------------------------------------------------------

  @Override
  public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
      throws BeanDefinitionStoreException {

    Assert.hasText(beanName, "Bean name must not be empty");
    Assert.notNull(beanDefinition, "BeanDefinition must not be null");

    if (beanDefinition instanceof AbstractBeanDefinition) {
      try {
        ((AbstractBeanDefinition) beanDefinition).validate();
      }
      catch (BeanDefinitionValidationException ex) {
        throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
            "Validation of bean definition failed", ex);
      }
    }

    BeanDefinition existingDefinition = this.beanDefinitionMap.get(beanName);
    if (existingDefinition != null) {
      if (!isAllowBeanDefinitionOverriding()) {
        throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
            "Cannot register bean definition [" + beanDefinition + "] for bean '" + beanName +
            "': There is already [" + existingDefinition + "] bound.");
      }
      else if (existingDefinition.getRole() < beanDefinition.getRole()) {
        // e.g. was ROLE_APPLICATION, now overriding with ROLE_SUPPORT or ROLE_INFRASTRUCTURE
        if (logger.isWarnEnabled()) {
          logger.warn("Overriding user-defined bean definition for bean '" + beanName +
              "' with a framework-generated bean definition: replacing [" +
              existingDefinition + "] with [" + beanDefinition + "]");
        }
      }
      else if (!beanDefinition.equals(existingDefinition)) {
        if (logger.isInfoEnabled()) {
          logger.info("Overriding bean definition for bean '" + beanName +
              "' with a different definition: replacing [" + existingDefinition +
              "] with [" + beanDefinition + "]");
        }
      }
      else {
        if (logger.isDebugEnabled()) {
          logger.debug("Overriding bean definition for bean '" + beanName +
              "' with an equivalent definition: replacing [" + existingDefinition +
              "] with [" + beanDefinition + "]");
        }
      }
      this.beanDefinitionMap.put(beanName, beanDefinition);
    }
    else {
      if (hasBeanCreationStarted()) {
        // Cannot modify startup-time collection elements anymore (for stable iteration)
        synchronized (this.beanDefinitionMap) {
          this.beanDefinitionMap.put(beanName, beanDefinition);
          List<String> updatedDefinitions = new ArrayList<>(this.beanDefinitionNames.size() + 1);
          updatedDefinitions.addAll(this.beanDefinitionNames);
          updatedDefinitions.add(beanName);
          this.beanDefinitionNames = updatedDefinitions;
          if (this.manualSingletonNames.contains(beanName)) {
            Set<String> updatedSingletons = new LinkedHashSet<>(this.manualSingletonNames);
            updatedSingletons.remove(beanName);
            this.manualSingletonNames = updatedSingletons;
          }
        }
      }
      else {
        // Still in startup registration phase
        this.beanDefinitionMap.put(beanName, beanDefinition);
        this.beanDefinitionNames.add(beanName);
        this.manualSingletonNames.remove(beanName);
      }
      this.frozenBeanDefinitionNames = null;
    }

    if (existingDefinition != null || containsSingleton(beanName)) {
      resetBeanDefinition(beanName);
    }
  }

从上面的代码可以看出,主要做了以下几件事

  • 对AbstractBeanDefinition做校验,主要是针对AbstractBeanDefinition的methodOverrides属性的。
  • 如果beanName已经注册且设置了不允许覆盖,则抛出异常。
  • 加入map缓存。由于有并发的可能,所以加上了synchronized
  • 消除之前的缓存。

对于别名的注册与上面的代码大同小异,就不看了。

下面再来看下import标签的解析

  1. /**
  2. * Parse an "import" element and load the bean definitions
  3. * from the given resource into the bean factory.
  4. */
  5. protected void importBeanDefinitionResource(Element ele) {
  6. String location = ele.getAttribute(RESOURCE_ATTRIBUTE);
  7. if (!StringUtils.hasText(location)) {
  8. getReaderContext().error("Resource location must not be empty", ele);
  9. return;
  10. }
  11. // Resolve system properties: e.g. "${user.dir}"
  12. location = getReaderContext().getEnvironment().resolveRequiredPlaceholders(location);
  13. Set<Resource> actualResources = new LinkedHashSet<>(4);
  14. // Discover whether the location is an absolute or relative URI
  15. boolean absoluteLocation = false;
  16. try {
  17. absoluteLocation = ResourcePatternUtils.isUrl(location) || ResourceUtils.toURI(location).isAbsolute();
  18. }
  19. catch (URISyntaxException ex) {
  20. // cannot convert to an URI, considering the location relative
  21. // unless it is the well-known Spring prefix "classpath*:"
  22. }
  23. // Absolute or relative?
  24. if (absoluteLocation) {
  25. try {
  26. int importCount = getReaderContext().getReader().loadBeanDefinitions(location, actualResources);
  27. if (logger.isDebugEnabled()) {
  28. logger.debug("Imported " + importCount + " bean definitions from URL location [" + location + "]");
  29. }
  30. }
  31. catch (BeanDefinitionStoreException ex) {
  32. getReaderContext().error(
  33. "Failed to import bean definitions from URL location [" + location + "]", ele, ex);
  34. }
  35. }
  36. else {
  37. // No URL -> considering resource location as relative to the current file.
  38. try {
  39. int importCount;
  40. Resource relativeResource = getReaderContext().getResource().createRelative(location);
  41. if (relativeResource.exists()) {
  42. importCount = getReaderContext().getReader().loadBeanDefinitions(relativeResource);
  43. actualResources.add(relativeResource);
  44. }
  45. else {
  46. String baseLocation = getReaderContext().getResource().getURL().toString();
  47. importCount = getReaderContext().getReader().loadBeanDefinitions(
  48. StringUtils.applyRelativePath(baseLocation, location), actualResources);
  49. }
  50. if (logger.isDebugEnabled()) {
  51. logger.debug("Imported " + importCount + " bean definitions from relative location [" + location + "]");
  52. }
  53. }
  54. catch (IOException ex) {
  55. getReaderContext().error("Failed to resolve current resource location", ele, ex);
  56. }
  57. catch (BeanDefinitionStoreException ex) {
  58. getReaderContext().error("Failed to import bean definitions from relative location [" + location + "]",
  59. ele, ex);
  60. }
  61. }
  62. Resource[] actResArray = actualResources.toArray(new Resource[0]);
  63. getReaderContext().fireImportProcessed(location, actResArray, extractSource(ele));
  64. }
/**
   * Parse an "import" element and load the bean definitions
   * from the given resource into the bean factory.
   */
  protected void importBeanDefinitionResource(Element ele) {
    String location = ele.getAttribute(RESOURCE_ATTRIBUTE);
    if (!StringUtils.hasText(location)) {
      getReaderContext().error("Resource location must not be empty", ele);
      return;
    }

    // Resolve system properties: e.g. "${user.dir}"
    location = getReaderContext().getEnvironment().resolveRequiredPlaceholders(location);

    Set<Resource> actualResources = new LinkedHashSet<>(4);

    // Discover whether the location is an absolute or relative URI
    boolean absoluteLocation = false;
    try {
      absoluteLocation = ResourcePatternUtils.isUrl(location) || ResourceUtils.toURI(location).isAbsolute();
    }
    catch (URISyntaxException ex) {
      // cannot convert to an URI, considering the location relative
      // unless it is the well-known Spring prefix "classpath*:"
    }

    // Absolute or relative?
    if (absoluteLocation) {
      try {
        int importCount = getReaderContext().getReader().loadBeanDefinitions(location, actualResources);
        if (logger.isDebugEnabled()) {
          logger.debug("Imported " + importCount + " bean definitions from URL location [" + location + "]");
        }
      }
      catch (BeanDefinitionStoreException ex) {
        getReaderContext().error(
            "Failed to import bean definitions from URL location [" + location + "]", ele, ex);
      }
    }
    else {
      // No URL -> considering resource location as relative to the current file.
      try {
        int importCount;
        Resource relativeResource = getReaderContext().getResource().createRelative(location);
        if (relativeResource.exists()) {
          importCount = getReaderContext().getReader().loadBeanDefinitions(relativeResource);
          actualResources.add(relativeResource);
        }
        else {
          String baseLocation = getReaderContext().getResource().getURL().toString();
          importCount = getReaderContext().getReader().loadBeanDefinitions(
              StringUtils.applyRelativePath(baseLocation, location), actualResources);
        }
        if (logger.isDebugEnabled()) {
          logger.debug("Imported " + importCount + " bean definitions from relative location [" + location + "]");
        }
      }
      catch (IOException ex) {
        getReaderContext().error("Failed to resolve current resource location", ele, ex);
      }
      catch (BeanDefinitionStoreException ex) {
        getReaderContext().error("Failed to import bean definitions from relative location [" + location + "]",
            ele, ex);
      }
    }
    Resource[] actResArray = actualResources.toArray(new Resource[0]);
    getReaderContext().fireImportProcessed(location, actResArray, extractSource(ele));
  }

从上面的代码可以看到,主要有几下几个步骤:

  • 获取路径。
  • 解析路径中的系统属性,如$(usr.dir}
  • 判断是绝对路径还是相对路径。
  • 如果是绝对路径则递归调用loadBeanDefinitions来进行解析。
  • 如果是相对路径则先计算出绝对路径并解析。