对spring的xml文件头部分配置的理解
来源:互联网 发布:淘宝网廷长收货 编辑:程序博客网 时间:2024/06/07 03:57
<?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>
这 个文档中,根元素<beans/>就不用说了,接下来是xmlns。那么什么是xmlns呢?xmlns其实是XML Namespace的缩写,可译为“XML命名空间”,但个人觉得,翻译后的名字反而不好理解,所以我们就叫它为XML Namespace吧。
为什么需要xmlns?
考虑这样两个XML文档:表示HTML表格元素的<table/>:<table> <tr> <td>Apples</td> <td>Bananas</td> </tr></table>
和描述一张桌子的<table/>:
<table> <name>African Coffee Table</name> <width>80</width> <length>120</length></table>假如这两个 XML 文档被一起使用,由于两个文档都包含带有不同内容和定义的 <table> 元素,就会发生命名冲突。XML 解析器是无法确定如何处理这类冲突。为了解决上述问题,xmlns就产生了。
如何是用xmlns?
很简单,使用语法: xmlns:namespace-prefix="namespaceURI"。其中namespace-prefix为自定义前缀,只要在这个XML文档中保证前缀不重复即可;namespaceURI是这个前缀对应的XML Namespace的定义。例如,
xmlns:context="http://www.springframework.org/schema/context"
这一句定义了一个http://www.springframwork.org/schema/context的Namespace(这和Java类中的包的声明很相似),并将其和前缀context绑定。所以上面的Spring XML文档中会有这么一句:
<context:component-scan base-package="xxx.xxx.controller" />
这里的<component-scan/>元素就来自别名为context的XML Namespace,也就是在http://www.springframework.org/schema/context中定义的。
我们还可以将前缀定义为abc:
xmlns:abc="namespaceURI"这样再使用这个namespaceURI中的元素时,需要以abc为前缀,例如:<abc:xxx/>。再拿上面的例子解释怎么使用xmlns:
<!-- 这里xmlns:h="url1"表示这个table是用h作为标记,table的写法在url1中定义 --><h:table xmlns:h="url1"> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr></h:table>和:
<!-- 这里xmlns:f="url2"表示这个table是用f作为标记,table的写法在url2中定义 --><f:table xmlns:f="url2"> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length></f:table>
后者与前者仅仅使用不同前缀,我们为 <table> 标签添加了一个 xmlns 属性,这样就为前缀赋予了一个与某个命名空间相关联的限定名称。此时再把它们放在一起,XML解析器就不会报错了。
注意:当xmlns被定义在元素的开始标签中(如这里的<f:table/>)时,所有带有相同前缀的子元素都会与同一个Namespace相关联(即<f:table/>里面的<f:name/>和<f:width/>也会使用url2定义的写法)。
首先二者都是命名空间,是基本文件与辅助文件的区别;
xmlns默认的根节点就是http://www.springframework.org/schema/beans 很明显这是个网址。我们可以直接打开,如下:
xml中所有的基本标签都在这里文件中的beans/中。但是如果这里面的标签不够我们使用就需要添加外来的,这里的xmlns:context就是添加的,我们还可以添加比如xmlns:mvc等。在标签的写法上。在http://www.springframework.org/schema/beans中的标签直接像<bean></bean>这样使用就好了,但是使用起来就需要像:
<context:component-scan base-package="xxx.xxx.controller" />
使用,这样就能和前面声明的xmls:context绑定在一起。
xsi:schemaLocation有何作用?
xsi:schemaLocation属性其实是Namespace为http://www.w3.org/2001/XMLSchema-instance里的schemaLocation属性,正是因为我们一开始声明了
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"这里才写作xsi:schemaLocation(当然一般都使用这个前缀)。它定义了XML Namespace和对应的 XSD(Xml Schema Definition)文档的位置的关系。它的值由一个或多个URI引用对组成,两个URI之间以空白符分隔(空格和换行均可)。第一个URI是定义的 XML Namespace的值,第二个URI给出Schema文档的位置,Schema处理器将从这个位置读取Schema文档,该文档的targetNamespace必须与第一个URI相匹配。例如:
xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
明显是成对存在的,先声明前面声明好的命名空间,然后 空格 然后,指定配置实例 所在的配置文件。
这里表示Namespace为http://www.springframework.org/schema/context的Schema的位置为http://www.springframework.org/schema/context/spring-context.xsd。这里我们可以打开这个Schema的位置,下面是这个文档的开始部分
<xsd:schema xmlns="http://www.springframework.org/schema/context" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:tool="http://www.springframework.org/schema/tool" <!-- 这里的targetNamespace和上方xsi:schemaLocation中的第一个URI匹配 --> targetNamespace="http://www.springframework.org/schema/context" elementFormDefault="qualified" attributeFormDefault="unqualified">
文章转自:https://www.cnblogs.com/zhao1949/p/5652167.html
- 对spring的xml文件头部分配置的理解
- Spring的XML配置文件的头部文件
- 我对Spring的理解------分三部分去理解
- Spring 配置XML文件头部文件格式
- 对web.xml文件、spring配置文件中的classpath的理解
- Spring配置文件的applicationContext.xml文件头部出现红叉
- 配置Spring的XML文件
- 典型的spring xml配置文件头部
- 对spring配置中bean的理解
- 详解 xml 文件头部的 xmlns:xsi
- web.xml文件的schema头部声明
- 对XML的理解
- 对SPRING的理解
- 对Spring 的理解
- 对Spring的理解
- 对Spring的理解
- 对spring的理解
- 对Spring的理解
- 软件工程(C编码实践篇)学习总结
- SSL2846 2017年11月9日提高组T1 质数(线性筛)
- 道德的大棒·《乖摸摸头》·四
- JZOJsenior2256.【ZJOI2008】树的统计
- java删除文件夹及其文件的方法
- 对spring的xml文件头部分配置的理解
- centos7.2 基于zookeeper集群搭建activeMQ的集群
- 第三十天 数据库(下)连接数据库
- 习题6.6
- Linux学习之————————gcc 编译器以及shell
- mysql
- 单片机学习札记2---调试数码管电路
- PAT甲级 1042. Shuffling Machine (20)
- 移位运算