spring---------配置文件的命名空間

兩種格式的配置文件:html

DTD和Schema區別:java

 Schema是對XML文檔結構的定義和描述,其主要的做用是用來約束XML文件,並驗證XML文件有效性。DTD的做用是定義XML的合法構建模塊,它使用一系列的合法元素來定義文檔結構。它們之間的區別有下面幾點:spring

       一、Schema自己也是XML文檔,DTD定義跟XML沒有什麼關係,Schema在理解和實際應用有不少的好處。swift

       二、DTD文檔的結構是「平鋪型」的,若是定義複雜的XML文檔,很難把握各元素之間的嵌套關係;Schema文檔結構性強,各元素之間的嵌套關係很是直觀。spring-mvc

      三、DTD只能指定元素含有文本,不能定義元素文本的具體類型,如字符型、整型、日期型、自定義類型等。Schema在這方面比DTD強大。ruby

      四、Schema支持元素節點順序的描述,DTD沒有提供無序狀況的描述,要定義無序必需窮舉排列的全部狀況。Schema能夠利用xs:all來表示無序的狀況。mvc

     五、對命名空間的支持。DTD沒法利用XML的命名空間,Schema很好知足命名空間。而且,Schema還提供了include和import兩種引用命名空間的方法。app

 

這裏主要講解spring的配置文件的命名空間spa

 

複製代碼
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"  <!--默認命名空間:表示未使用其餘命名空間的全部標籤的默認命名空間-->
3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   <!--xsi標準命名空間,用於指定義自定義命名空間的schema文件,聲明後就可使用 schemaLocation 屬性了-->
4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">  <!--此處能夠不配置,能夠用來引用schema在本地的副本-->
5    
6     
7 
8 </beans>
複製代碼

爲每一個命名空間指定了對應的Schema文檔的時候,定義的語法爲:code

  「全稱命名空間1  空格  全稱命名空間1對應的Schema文件空格」

其餘命名空間與

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

同一級別下配置便可

  • util

util標籤用來配置集合、常量等的

xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation中須要定義
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd

  使用:分別使用<util:list>、<util:map>、<util:set>、<util:properties>等標籤,用來 

     取代ListFactoryBean、MapFactoryBean、SetFactoryBean、PropertiesFactoryBean。
  用途一:直接使用<util:properties id="appProps" location="classpath:META-INF/app.properties" />指定了配置文件
    之前須要使用
      <!-- creates a java.util.Properties instance with values loaded from the supplied location -->
        <bean id="jdbcConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
          <property name="location" value="classpath:com/foo/jdbc-production.properties"/>
        </bean>
    spring中id就是這個類的一個別名
    如今只需使用
      <!-- creates a java.util.Properties instance with values loaded from the supplied location -->
        <util:properties id="jdbcConfiguration" location="classpath:com/foo/jdbc-production.properties"/>

    

  • jee

jee標籤用來處理javaee標準相關的問題,例如查詢一個jndi對象以及定義一個ejb的引用等

xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation中須要定義
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
  • lang 

 

lang用來將那些已經被定義在一些動態語言(例如Jruby和Groovy)中的對象做爲beans中的對象存放到spring容器中

xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation中須要定義
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
  • jms 

 

xmlns:jms="http://www.springframework.org/schema/jms"

 

http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd
  • tx (transaction) 

使用時建議配合aop

xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"

 

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

 

  • aop 

 

xmlns:aop="http://www.springframework.org/schema/aop"

 

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
  • context 

 

xmlns:context="http://www.springframework.org/schema/context"

 

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  • jdbc 

 

xmlns:jdbc="http://www.springframework.org/schema/jdbc"

 

http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
  • cache 

 

xmlns:jdbc="http://www.springframework.org/schema/cache"

 

http://www.springframework.org/schema/cache http://www.springframework.org/schema/jdbc/spring-cache.xsd

 

  • 完整的一個配置文件頭以下

複製代碼
<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd    
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd    
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd    
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd    
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd    
        http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.1.xsd    
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.1.xsd    
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd    
        http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-4.1.xsd    
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd    
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd    
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">



</beans>      
複製代碼

 

reference:

[1] http://iswift.iteye.com/blog/1657537

[2] http://www.cnblogs.com/brolanda/p/4167553.html

相關文章
相關標籤/搜索