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


兩種格式的配置文件:

DTD和Schema區別:

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

       1、Schema本身也是XML文檔,DTD定義跟XML沒有什么關系,Schema在理解和實際應用有很多的好處。

       2、DTD文檔的結構是“平鋪型”的,如果定義復雜的XML文檔,很難把握各元素之間的嵌套關系;Schema文檔結構性強,各元素之間的嵌套關系非常直觀。

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

      4、Schema支持元素節點順序的描述,DTD沒有提供無序情況的描述,要定義無序必需窮舉排列的所有情況。Schema可以利用xs:all來表示無序的情況。

     5、對命名空間的支持。DTD無法利用XML的命名空間,Schema很好滿足命名空間。並且,Schema還提供了include和import兩種引用命名空間的方法。

 

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

 

復制代碼
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文檔的時候,定義的語法為:

  “全稱命名空間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


免責聲明!

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



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