Spring 啓動記錄(9)

Spring BeanDefinitionParserDelegate 解析node

一、DefaultBeanDefinitionDocumentReader持有BeanDefinitionParserDelegate,spring

調用BeanDefinitionParserDelegate作真正的解析工做spring-mvc

/**
 * Parse the elements at the root level in the document:
 * "import", "alias", "bean".
 * @param root the DOM root element of the document
 */
protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {
   if (delegate.isDefaultNamespace(root)) {
      NodeList nl = root.getChildNodes();
      for (int i = 0; i < nl.getLength(); i++) {
         Node node = nl.item(i);
         if (node instanceof Element) {
            Element ele = (Element) node;
            // 解析 xsd http://www.springframework.org/schema/beans對應的標籤 <beans>
            if (delegate.isDefaultNamespace(ele)) {
               parseDefaultElement(ele, delegate);
            }
            //解析其餘xsd對應的如:
            //http://www.springframework.org/schema/mvc <mvc:annotation-driven />
            else {
               delegate.parseCustomElement(ele);
            }
         }
      }
   }
   else {
      delegate.parseCustomElement(root);
   }
}

 

 

二、解析其餘xsd的handlermvc

如:http://www.springframework.org/schema/mvcapp

public BeanDefinition parseCustomElement(Element ele, BeanDefinition containingBd) {
   //現獲取到uri:http://www.springframework.org/schema/mvc
   String namespaceUri = getNamespaceURI(ele);
   
   NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);
   if (handler == null) {
      error("Unable to locate Spring NamespaceHandler for XML schema namespace [" + namespaceUri + "]", ele);
      return null;
   }
   return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd));
}

這是XMLReaderContext中全部handler的mappingcors

這個例子中的的MvcNamespaceHandleride

public class MvcNamespaceHandler extends NamespaceHandlerSupport {

   @Override
   public void init() {
      registerBeanDefinitionParser("annotation-driven", new AnnotationDrivenBeanDefinitionParser());
      registerBeanDefinitionParser("default-servlet-handler", new DefaultServletHandlerBeanDefinitionParser());
      registerBeanDefinitionParser("interceptors", new InterceptorsBeanDefinitionParser());
      registerBeanDefinitionParser("resources", new ResourcesBeanDefinitionParser());
      registerBeanDefinitionParser("view-controller", new ViewControllerBeanDefinitionParser());
      registerBeanDefinitionParser("redirect-view-controller", new ViewControllerBeanDefinitionParser());
      registerBeanDefinitionParser("status-controller", new ViewControllerBeanDefinitionParser());
      registerBeanDefinitionParser("view-resolvers", new ViewResolversBeanDefinitionParser());
      registerBeanDefinitionParser("tiles-configurer", new TilesConfigurerBeanDefinitionParser());
      registerBeanDefinitionParser("freemarker-configurer", new FreeMarkerConfigurerBeanDefinitionParser());
      registerBeanDefinitionParser("velocity-configurer", new VelocityConfigurerBeanDefinitionParser());
      registerBeanDefinitionParser("groovy-configurer", new GroovyMarkupConfigurerBeanDefinitionParser());
      registerBeanDefinitionParser("script-template-configurer", new ScriptTemplateConfigurerBeanDefinitionParser());
      registerBeanDefinitionParser("cors", new CorsBeanDefinitionParser());
   }

}

這些初始化都持有了內部的key都是對應了spring-mvc-4.0.xsd中聲明的 element(如<xsd:element name="interceptors">)等;this

其實就是經過Dom對象找到對應xsd,而後找到對應的handler,進而經過handler中持有的BeanDefinitionParser的實現類spa

 

上圖就是解析BeanDefinition對象的流程圖對象

相關文章
相關標籤/搜索