spring的配置文件解析(转)

http://www.cnblogs.com/as-dreamer/p/6523215.html

我们在使用Spring框架的时候首先要配置其xml文件,大量的头信息到底代表了什么呢,在这里总结下自己的理解。。。

这里是创建web工程时自带的xml文件头内容:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
        http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

这是我们配置的Spring头信息内容:

复制代码
<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
复制代码

对比之下可以发现存在共性的是他们都有:声明为xml文件,版本为1.0,编码为utf-8,这些大家都容易理解。

可是xmlns:xsi.......;  xmlns......; xsi:schemaLocation......;这些配置代表了什么东东呢???

经过总结网上前辈的经验,在这里说一下自己的理解:

首先xml是一种严格的标记语言(相对于html来说),例如必须有结束标签等,另外大家知道,因为其严格的特点,并且可以根据个人的需要增加新的标签内容,常被用来用作项目的配置文件使用。

那么需要成为严格的xml标签语言就需要有其规则进行约束,我们新建的标准的xml文件包含了xmlns:xsi.......;  xmlns......; xsi:schemaLocation.....这些内容如上所示,xmlns=命名空间,xsi=xml-schema-instance(xml模板实例,标准的是这样来命名的,好官方),知道这些我们就知道标准的web.xml文件内容这些是什么了.

web.xml的头信息:

1.xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance声明该文件的内容可以使用xsi的标签库,

2.xmlns="http://xmlns.jcp.org/xml/ns/javaee"声明标签的使用范围是被javaee的开发使用的

3.xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee声明可以使用的标签库和对应的具体的标签库版本地址。

那么Spring的头信息也就是:

1.xmlns=http://www.springframework.org/schema/beans表示是spring beans的xml配置文件

2.xmlns:xsi;xmlns:context;xmlns:aop;xmlns:tx;声明可以使用标准的xml标签(xsi);也可以使用context;aop;tx等声明后的标签库的标签,即声明的xmlns:后的内容可以作为标签规则出现在xml文件中,没有包含在内的使用时会报错(作为一种严格的标记语言的特性)。

3.xsi-schemaLocation这是语法规则契约(xmlns也是;而xmlns:xsi只是声明标签的规则),=等号后面的内容就是引入的具体的标签库和对应的要在文本中使用的标签库具体版本规则的地址。

4.由于在xsi-schemaLocation中定义的是标签的属性等相关信息,xmlns:p中由于p命名空间的引入是为了简化属性property的书写格式,而p的属性是可变的,所以没有在xsi-schemaLocation中定义。

注意:在xsi-schemaLocation中最后一个http:.....与双引号”之间要有一个空格,否则会找不到地址报错。

xsi全名:xml schema instance

web-app是web.xml的根节点标签名称
version是版本的意思
xmlns是web.xml文件用到的命名空间
xmlns:xsi是指web.xml遵守xml规范
xsi:schemaLocation是指具体用到的schema资源

你不要看相关中文资料,看了就不明白了,schema就是schema~你把他翻译成对文档的限制就行了。你可能会说,dtd才是,实际上xsd和dtd是一样的~~

加入命名空间xmlns是为了避免命名冲突。beans.xml 那里没有加前缀表示该文件默认使用的是beans.xml的语法规则。其他的命名空间如context使用该内元素就得加上它的专属前缀context。



shemaLocation的位置在jar包的MATE-INF下面的.schemas文件中找到
原文地址:https://www.cnblogs.com/aigeileshei/p/8064605.html