引言:
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
這里就簡單就應用中會經常碰到的一些定義進行說明一下。
應用場景:
對數據庫連接進行進一步的封裝,使配置簡單化。
由原來的配置方式:
- <bean id="dataSource"
- class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
- <property name="url"
- value="jdbc:mysql://localhsost/freebug?useUnicode=true&characterEncoding=utf8&autoReconnect=true"></property>
- <property name="username">
- <value>root</value>
- </property>
- <property name="password">
- <value>root</value>
- </property>
- </bean>
- <!-- 會話 -->
- <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
- <property name="configLocation" value="classpath:SqlMapCommonConfig.xml" />
- <property name="dataSource" ref="dataSource" />
- </bean>
改為:
- <mysql:client id="sqlMapClient" datasouceip="localhsost" characterEncoding="utf8"
- dbname="freebug" username="root" password="root"
- configLocation="classpath:SqlMapCommonConfig.xml" />
從這里面,對配置進行了一次簡化的處理。其他很多的信息屬性還可以進行默認和正則式的限制。
如何做到使上面的配置可以成立,這就要求着,對這個標簽進行xsd規范的定義。
標簽定義
定義如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <xsd:schema xmlns="http://sammor.javaeye.com/schema/tags"
- xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans"
- targetNamespace="http://sammor.javaeye.com/schema/tags"
- elementFormDefault="qualified" attributeFormDefault="unqualified">
- <xsd:import namespace="http://www.springframework.org/schema/beans" />
- <xsd:element name="client">
- <xsd:annotation>
- <xsd:documentation>connect to mysql</xsd:documentation>
- </xsd:annotation>
- <xsd:complexType>
- <xsd:complexContent>
- <!-- 繼承定義 從namespace="http://www.springframework.org/schema/beans" -->
- <xsd:extension base="beans:identifiedType">
- <xsd:attribute name="dbname" type="xsd:string" use="required" />
- <xsd:attribute name="datasouceip" type="xsd:string"
- use="optional" default="127.0.0.1" />
- <xsd:attribute name="username" type="xsd:string" use="required" />
- <xsd:attribute name="password" type="xsd:string" use="required" />
- <xsd:attribute name="characterEncoding" type="xsd:string"
- default="utf8">
- <xsd:simpleType>
- <xsd:restriction base="xsd:string">
- <xsd:enumeration value="utf8" />
- <xsd:enumeration value="gbk" />
- </xsd:restriction>
- </xsd:simpleType>
- </xsd:attribute>
- <xsd:attribute name="configLocation" type="xsd:string"
- default="classpath:SqlMapCommonConfig.xml" />
- </xsd:extension>
- </xsd:complexContent>
- </xsd:complexType>
- </xsd:element>
- </xsd:schema>
說明:
- xsd:element 表示定義標簽
- xsd:extension 如java中的繼承,把現有的定義繼承進來
- xsd:attribute 標簽帶有的屬性
- xsd:restriction 對標簽改屬性進一步的限制,進行一些值的約束
p.s:有時,可能會在
- <xsd:extension base="beans:identifiedType">
處報出錯誤
- src-resolve: Cannot resolve the name 'beans:identifiedType' to a(n) 'type definition' component.
請跳過,這個不影響使用,只是ide的檢查有問題。
聲明標簽
對於寫好的標簽定義需要把他公布到應用中,使其他spring的xml中,可用,需要幾步:
1、在classpath資源路徑下加入文件:
2、spring.schemas該文件中填寫:
- http\://sammor.javaeye.com/schema/tags/springtags.xsd=conf/springtag.xsd
其中http后的“\”為轉義的意思
3、在spring的配置文件xml頭部加入
在配置文件中聲明引用xsd定義
當然,到這一步的時候,還僅是對定義一種自定義標簽的寫法,接下來,如何去做到標簽的實現處理,還要做進一步的處理。spring自定義標簽之三 —— 自我實現
p.s 關於xsd的規范學習,還有很多很多,它是一個強大的規范,可以為我們做很多的基礎信息和約束,為使用者提供更安全方便的做法。