tomcat与springmvc 结合 之---第19篇(下,补充) springmvc 加载.xml文件的bean标签的过程

writedby 张艳涛,上一篇写了springmvc对<mvc:annoXXXX/>标签的解析过程,其实是遗漏重要的细节,因为理解的不深入吧

今天接着解析<bean>标签

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd  ">
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
    <!-- 处理器 -->
    <bean id="NoannaContoller" name="/go2.action" class="com.zyt.NoannaContoller"></bean>
    <!-- 处理器映射器 根据bean的name进行查找Handler 将action的url配置在bean的name中 -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
    <mvc:annotation-driven/>
</beans>

对于这个标签的处理

  • 在新建了wac(webapplicationcontext)就需要解析xml标签,那么步骤
  • 新建beanfactory,类型为 DefaultListableBeanFactory
  • 新建 beanDefinitionReader,类型为XmlBeanDefinitionReader,负责读取成员变量 configLocation内容,本例中是classpath:springmvc.xml
  • 使用了rt.jar的 DocumentBuilder 对xml文件进行了读取,这里比较神奇,根据xsi:schemaLocation=http://www.springframework.org/schema/beans/spring-beans.xsd,如何找到的factory.xml目录下文件还不太清楚,根据
    此文件定义了xml文件bean标签的解析规则,并且给bean对象添加了默认属性,最后生成了doc文件
  • 新建documentReader,类型为 BeanDefinitionDocumentReader,新建XmlReaderContext,并且里面有一个一个默认的namespaceHandlerResolver
  • 之后进入parseBeanDefinitions

tomcat与springmvc 结合 之---第19篇(下,补充) springmvc 加载.xml文件的bean标签的过程

看这个ele对象

tomcat与springmvc 结合 之---第19篇(下,补充) springmvc 加载.xml文件的bean标签的过程

非常重要的就是ele元素,有一个namespaceurl,那么这个bean的namespaceurl是http://www.springframework.org/schema/beans

之前的mvc 的nameurl应该是...mvc

所以在

                    if (delegate.isDefaultNamespace(ele)) {
                        parseDefaultElement(ele, delegate);
                    }
                    else {
                        delegate.parseCustomElement(ele);
                    }

第一个if就是是默认的namespace 吗

tomcat与springmvc 结合 之---第19篇(下,补充) springmvc 加载.xml文件的bean标签的过程

tomcat与springmvc 结合 之---第19篇(下,补充) springmvc 加载.xml文件的bean标签的过程tomcat与springmvc 结合 之---第19篇(下,补充) springmvc 加载.xml文件的bean标签的过程

能看到默认的就是http://www.springframework.org/schema/beans

那么使用的就是默认解析器BeanDefinitionParserDelegate

看如果遇到mvc标签怎么办?

tomcat与springmvc 结合 之---第19篇(下,补充) springmvc 加载.xml文件的bean标签的过程

ele元素

tomcat与springmvc 结合 之---第19篇(下,补充) springmvc 加载.xml文件的bean标签的过程

进入customelement解析

tomcat与springmvc 结合 之---第19篇(下,补充) springmvc 加载.xml文件的bean标签的过程

先使用 namespaceHandlerResolver来解析 handlerMappings 就是读取所有的META-INF目录下的spring.handlers内容,读取保存到namespaceHandlerResolver.handlerMappings,根据key value键值对来查找或新建handler

tomcat与springmvc 结合 之---第19篇(下,补充) springmvc 加载.xml文件的bean标签的过程

可以看到根据namespaceurl来查找handler,再次使用handler来解析mvc标签