spring自定義標簽之 規范定義XSD


引言:

       spring的配置文件中,一切的標簽都是spring定義好的。<bean/>等等,有了定義的規范,才能讓用戶填寫的正常可用。想寫自定義標簽,但首先需要了解XML Schema Definition(XSD) 的。

 

標簽定義:

       對於該類標簽的定義,spring中有着相應的XSD定義文檔

       http://www.springframework.org/schema/beans

 

       對於XSD,簡單的說是xml的一個標簽的定義,在這里就不對XSD過多的解釋了,祥見

       http://www.w3school.com.cn/schema/schema_example.asp

       這里就簡單就應用中會經常碰到的一些定義進行說明一下。

 

應用場景:

      對數據庫連接進行進一步的封裝,使配置簡單化。

      由原來的配置方式:

Xml代碼   收藏代碼
  1. <bean id="dataSource"  
  2.     class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  3.     <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
  4.     <property name="url"  
  5.         value="jdbc:mysql://localhsost/freebug?useUnicode=true&amp;characterEncoding=utf8&amp;autoReconnect=true"></property>  
  6.     <property name="username">  
  7.         <value>root</value>  
  8.     </property>  
  9.     <property name="password">  
  10.         <value>root</value>  
  11.     </property>  
  12. </bean>  
  13.   
  14. <!-- 會話 -->  
  15. <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">  
  16.     <property name="configLocation" value="classpath:SqlMapCommonConfig.xml" />  
  17.     <property name="dataSource" ref="dataSource" />  
  18. </bean>   

    改為:

Xml代碼   收藏代碼
  1. <mysql:client id="sqlMapClient" datasouceip="localhsost"  characterEncoding="utf8"   
  2.             dbname="freebug"   username="root" password="root"  
  3.             configLocation="classpath:SqlMapCommonConfig.xml" />  

   從這里面,對配置進行了一次簡化的處理。其他很多的信息屬性還可以進行默認和正則式的限制。

 

   如何做到使上面的配置可以成立,這就要求着,對這個標簽進行xsd規范的定義。

 

標簽定義

   定義如下:

Xml代碼   收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <xsd:schema xmlns="http://sammor.javaeye.com/schema/tags"  
  3.     xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans"  
  4.     targetNamespace="http://sammor.javaeye.com/schema/tags"  
  5.     elementFormDefault="qualified" attributeFormDefault="unqualified">  
  6.     <xsd:import namespace="http://www.springframework.org/schema/beans" />  
  7.     <xsd:element name="client">  
  8.         <xsd:annotation>  
  9.             <xsd:documentation>connect to mysql</xsd:documentation>  
  10.         </xsd:annotation>  
  11.         <xsd:complexType>  
  12.             <xsd:complexContent>  
  13.                 <!-- 繼承定義 從namespace="http://www.springframework.org/schema/beans" -->  
  14.                 <xsd:extension base="beans:identifiedType">  
  15.                     <xsd:attribute name="dbname" type="xsd:string" use="required" />  
  16.                     <xsd:attribute name="datasouceip" type="xsd:string"  
  17.                         use="optional" default="127.0.0.1" />  
  18.                     <xsd:attribute name="username" type="xsd:string" use="required" />  
  19.                     <xsd:attribute name="password" type="xsd:string" use="required" />  
  20.                     <xsd:attribute name="characterEncoding" type="xsd:string"  
  21.                         default="utf8">  
  22.                         <xsd:simpleType>  
  23.                             <xsd:restriction base="xsd:string">  
  24.                                 <xsd:enumeration value="utf8" />  
  25.                                 <xsd:enumeration value="gbk" />  
  26.                             </xsd:restriction>  
  27.                         </xsd:simpleType>  
  28.                     </xsd:attribute>  
  29.                     <xsd:attribute name="configLocation" type="xsd:string"  
  30.                         default="classpath:SqlMapCommonConfig.xml" />  
  31.                 </xsd:extension>  
  32.             </xsd:complexContent>  
  33.         </xsd:complexType>  
  34.     </xsd:element>  
  35. </xsd:schema>  

 

說明:

Xml代碼   收藏代碼
  1. xsd:element     表示定義標簽  
  2. xsd:extension  如java中的繼承,把現有的定義繼承進來  
  3. xsd:attribute    標簽帶有的屬性  
  4. xsd:restriction  對標簽改屬性進一步的限制,進行一些值的約束  

p.s:有時,可能會在

Xml代碼   收藏代碼
  1. <xsd:extension base="beans:identifiedType">  

    處報出錯誤

Xml代碼   收藏代碼
  1. src-resolve: Cannot resolve the name 'beans:identifiedType' to a(n) 'type definition' component.  

    請跳過,這個不影響使用,只是ide的檢查有問題。

 

聲明標簽

     對於寫好的標簽定義需要把他公布到應用中,使其他spring的xml中,可用,需要幾步:

1、在classpath資源路徑下加入文件:

      

 2、spring.schemas該文件中填寫:

Xml代碼   收藏代碼
  1. http\://sammor.javaeye.com/schema/tags/springtags.xsd=conf/springtag.xsd  

     其中http后的“\”為轉義的意思

 

 3、在spring的配置文件xml頭部加入

 在配置文件中聲明引用xsd定義

 

當然,到這一步的時候,還僅是對定義一種自定義標簽的寫法,接下來,如何去做到標簽的實現處理,還要做進一步的處理。spring自定義標簽之三 —— 自我實現

 

p.s 關於xsd的規范學習,還有很多很多,它是一個強大的規范,可以為我們做很多的基礎信息和約束,為使用者提供更安全方便的做法。


免責聲明!

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



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