Spring配置文件xml頭信息解析一


我們在使用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:.....與雙引號”之間要有一個空格,否則會找不到地址報錯。

    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM