spring自定义标签之规范定义XSD

本文介绍如何通过XML Schema Definition (XSD) 在Spring框架中创建自定义标签来简化数据库连接配置,并提供了具体示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引言:

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. <beanid="dataSource"
  2. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  3. <propertyname="driverClassName"value="com.mysql.jdbc.Driver"></property>
  4. <propertyname="url"
  5. value="jdbc:mysql://localhsost/freebug?useUnicode=true&amp;characterEncoding=utf8&amp;autoReconnect=true"></property>
  6. <propertyname="username">
  7. <value>root</value>
  8. </property>
  9. <propertyname="password">
  10. <value>root</value>
  11. </property>
  12. </bean>
  13. <!--会话-->
  14. <beanid="sqlMapClient"class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  15. <propertyname="configLocation"value="classpath:SqlMapCommonConfig.xml"/>
  16. <propertyname="dataSource"ref="dataSource"/>
  17. </bean>

改为:

Xml代码
  1. <mysql:clientid="sqlMapClient"datasouceip="localhsost"characterEncoding="utf8"
  2. dbname="freebug"username="root"password="root"
  3. configLocation="classpath:SqlMapCommonConfig.xml"/>

从这里面,对配置进行了一次简化的处理。其他很多的信息属性还可以进行默认和正则式的限制。

如何做到使上面的配置可以成立,这就要求着,对这个标签进行xsd规范的定义。

标签定义

定义如下:

Xml代码 收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <xsd:schemaxmlns="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:importnamespace="http://www.springframework.org/schema/beans"/>
  7. <xsd:elementname="client">
  8. <xsd:annotation>
  9. <xsd:documentation>connecttomysql</xsd:documentation>
  10. </xsd:annotation>
  11. <xsd:complexType>
  12. <xsd:complexContent>
  13. <!--继承定义从namespace="http://www.springframework.org/schema/beans"-->
  14. <xsd:extensionbase="beans:identifiedType">
  15. <xsd:attributename="dbname"type="xsd:string"use="required"/>
  16. <xsd:attributename="datasouceip"type="xsd:string"
  17. use="optional"default="127.0.0.1"/>
  18. <xsd:attributename="username"type="xsd:string"use="required"/>
  19. <xsd:attributename="password"type="xsd:string"use="required"/>
  20. <xsd:attributename="characterEncoding"type="xsd:string"
  21. default="utf8">
  22. <xsd:simpleType>
  23. <xsd:restrictionbase="xsd:string">
  24. <xsd:enumerationvalue="utf8"/>
  25. <xsd:enumerationvalue="gbk"/>
  26. </xsd:restriction>
  27. </xsd:simpleType>
  28. </xsd:attribute>
  29. <xsd:attributename="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:extensionbase="beans:identifiedType">

处报出错误

Xml代码 收藏代码
  1. src-resolve:Cannotresolvethename'beans:identifiedType'toa(n)'typedefinition'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的规范学习,还有很多很多,它是一个强大的规范,可以为我们做很多的基础信息和约束,为使用者提供更安全方便的做法。

其他相关文章:

spring自定义标签之一 —— 意义思考

spring自定义标签之三 —— 自我实现

http://sammor.iteye.com/blog/1102401

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值