/**
   * Load the template BeanDefinition and call {@link #transform(ConfigurableListableBeanFactory,
   * BeanDefinition, Element, ParserContext)} to apply runtime configuration value to it. <code>
   * builder</code> will be configured to instantiate the bean in the Spring context that we are
   * parsing.
   *
   * <p>During parsing, an instance of {@link
   * TemplateBeanDefinitionParser.TemplateComponentDefinition} is pushed onto <code>ParserContext
   * </code> so that nested tags can access the enclosing template configuration with a call to
   * {@link #findEnclosingTemplateFactory(ParserContext)}. Subclasses can override {@link
   * #newComponentDefinition(String, Object, DefaultListableBeanFactory)} to provide a subclass of
   * {@link TemplateBeanDefinitionParser.TemplateComponentDefinition} to the parser context if
   * necessary.
   */
  @Override
  protected final void doParse(
      Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {

    // if we have multiple nested bean definitions, we only parse the template factory
    // once.  this allows configuration changes made by enclosing bean parsers to be inherited
    // by contained beans, which is quite useful.
    DefaultListableBeanFactory templateFactory = findEnclosingTemplateFactory(parserContext);
    TemplateComponentDefinition tcd = null;
    if (templateFactory == null) {

      // no nesting -- load the template XML configuration from the classpath.
      final BeanFactory parentFactory = (BeanFactory) parserContext.getRegistry();
      templateFactory = new DefaultListableBeanFactory(parentFactory);

      // load template bean definitions
      DefaultResourceLoader loader = new DefaultResourceLoader();
      XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(templateFactory);
      reader.setResourceLoader(loader);
      reader.setEntityResolver(new ResourceEntityResolver(loader));
      reader.loadBeanDefinitions(templateResource);

      // propagate factory post-processors from the source factory into the template
      // factory.
      BeanDefinition ppChain = new RootBeanDefinition(ChainingBeanFactoryPostProcessor.class);
      ppChain.getPropertyValues().addPropertyValue("targetFactory", templateFactory);
      parserContext.getReaderContext().registerWithGeneratedName(ppChain);

      // push component definition onto the parser stack for the benefit of
      // nested bean definitions.
      tcd =
          newComponentDefinition(
              element.getNodeName(), parserContext.extractSource(element), templateFactory);
      parserContext.pushContainingComponent(tcd);
    }

    try {
      // allow subclasses to apply overrides to the template bean definition.
      BeanDefinition def = templateFactory.getBeanDefinition(templateId);
      transform(templateFactory, def, element, parserContext);

      // setup our factory bean to instantiate the modified bean definition upon request.
      builder.addPropertyValue("beanFactory", templateFactory);
      builder.addPropertyValue("beanName", templateId);
      builder.getRawBeanDefinition().setAttribute("id", def.getAttribute("id"));

    } finally {
      if (tcd != null) parserContext.popContainingComponent();
    }
  }