引言:
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的规范学习,还有很多很多,它是一个强大的规范,可以为我们做很多的基础信息和约束,为使用者提供更安全方便的做法。
其他相关文章:
spring自定义标签之一 —— 意义思考
spring自定义标签之三 —— 自我实现
- 大小: 15.8 KB
- 大小: 89.3 KB
分享到:
相关推荐
超级简单的spring 自定义xsd。如果需要详细或复杂的请另行查找资料。 本资源只适合初学者。制作过程请看说明
spring-beans-4.3.xsd spring-beans-4.3.xsd spring-beans-4.3.xsd spring-beans-4.3.xsd
spring全套xsd文件,放入xml catalog中后,会自动自动提示spring标签
1. 定义一个Xml文档中都有什么元素 2. 定义一个Xml文档中都会有什么属性 3. 定义某个节点的都有什么样的子节点,可以有多少个子节点,子节点出现的顺序 4. 定义元素或者属性的数据类型 5. 定义元素或者属性的...
spring3.0 xsd文件 spring3.0 xsd文件 spring3.0 xsd文件 spring3.0 xsd文件 spring3.0 xsd文件 spring3.0 xsd文件
生成XML文件的定义规范文件XSD,能够严格定义XML文件的内容样式,对于刚学习XSD的人有一定的帮助。
spring-beans-3.0.xsd
spring-context-3.2.xsd
spring-aop-3.0.xsd 这个下载之后可以直接放在eclipese的xml的catalog中配置,已到达eclipse可以自动提示xml的功能
内含spring-aop-4.0.xsd、spring-beans-4.0.xsd、spring-context-4.0.xsd、spring-mvc-4.0.xsd、spring-tool-4.0.xsd、spring-tx-4.0.xsd、spring-util-4.0.xsd,在xml中引入相应xsd文件,便可以自动提示
spring和spring mvc各个版本的xsd文件还有mybatis的ddt约束文件
包含 spring-aop.xsd spring-beans.xsd spring-cache.xsd spring-jdbc spring-jms spring-lang 等
spring-mvc-3.0.xsd
Spring默认在启动时是要加载XSD文件来验证xml文件的,所以如果有的时候断网了,或者一些开源软件切换域名,那么就很容易碰到应用启动不了。我记得当时Oracle收购Sun公司时,遇到过这个情况。为了防止这种情况,...
spring约束-4.2.xsd
spring-beans-2.0.xsd
里面有两个xsd文件,springmodules-ehcache和springmodules-cache.xsd。需求:因为有时候在你在xml中用某个标签时,提示错误,有时候就是少了xsd的引入
spring-beans-3.2.xsd
Spring-3.0.xsd