zoukankan      html  css  js  c++  java
  • Spring XML配置文件头部字段的解析

    在对Spring进行配置时,XML文件的头部字段总是有如下形式:

    <?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">
        <context:component-scan base-package="xxx.xxx.controller" />
         
        <context:annotation-config/>
        <mvc:default-servlet-handler/>
        <mvc:annotation-driven/>
         
        <mvc:resources mapping="/images/**" location="/images/" />
         
        <bean id="xxx" class="xxx.xxx.xxx.Xxx">
            <property name="xxx" value="xxxx"/>
        </bean>
    </beans>
    

    之前总是从各大博客上复制粘贴,但是具体是什么意思并未深究。如今我将对该头部字段进行逐步分析:

    1. xlmns

    首先,在beans标签的属性中,我们指定了xmlns="http://www.springframework.org/schema/beans"。这里xmlns是XML NameSpace的缩写,用以区分相同名称的标签,例如table在某个命名空间中可以表示“表”,在另一个命名空间中则可以表示“桌子”。而"http://www.springframework.org/schema/beans"是全局唯一的字符串。当xmlns单独使用时,表示这个xml文档默认使用后面的字符串作为命名空间。若在xmlns后面跟上对应的名称,如xmlns:context="http://www.springframework.org/schema/context",那么意思是使用字符串"http://www.springframework.org/schema/context"来表示context命名空间。也就是说,我们可以使用context标签来定义元素,如<context:component-scan base-package="xxx.xxx.controller" />

    2. xmlns:xsi

    在xmlns所定义的命名空间中,xsi是比较特殊的一个。根据之前的知识我们可以理解, xmlns:xsi 定义了一个命名空间前缀xsi对应的唯一字符串 http://www.w3.org/2001/XMLSchema-instance。这个xmlns:xsi在不同的xml文档中似乎都会出现。 这是因为,xsi已经成为了一个业界默认的用于xsd(XML Schema Definition) 文件的命名空间。而xsd文件(也常常称为Schema文件)是用来定义xml文档结构的。

    XML解析器可以根据一个xsd文件的内容来解析另一个XML文件,判断该文件的结构是否和xsd文件中定义的一致。 xsd文件可以理解为XML文档可以自定义的语法或格式检查器。

    xsi:schemaLocation实际上是一个“K-V”形式的属性,它表示xsi命名空间下的schemaLocation属性为“字符串-xsd文件的URI”的形式。例如:

    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans.xsd"
    

    这就表示命名空间为http://www.springframework.org/schema/beans的属性需要使用URI为http://www.springframework.org/schema/beans/spring-beans.xsd的xsd文件来进行格式的校验。

  • 相关阅读:
    关于回调地狱
    node.js 关于跨域和传递给前台参数
    关于js的当前日期的格式化,和两个日期之间的相减的天数
    CSS 关于让页面的高度达到电脑屏幕的底部
    前端 为什么我选择用框架而不是Jquery
    关于webpack打包图片的路径问题
    关于webpack打包js和css
    微信小程序网络请求的setDate
    WinSocket聊天程序实例(多线程)
    Orcal的JDBC数据连接方式
  • 原文地址:https://www.cnblogs.com/muuu520/p/14846590.html
Copyright © 2011-2022 走看看