引言:

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

这里就简单就应用中会经常碰到的一些定义进行说明一下。

应用场景:

对数据库连接进行进一步的封装,使配置简单化。

由原来的配置方式:

  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. <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  15. <property name="configLocation" value="classpath:SqlMapCommonConfig.xml" />
  16. <property name="dataSource" ref="dataSource" />
  17. </bean>

改为:

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

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

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

标签定义

定义如下:

  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>

说明:

  1. xsd:element     表示定义标签
  2. xsd:extension  如java中的继承,把现有的定义继承进来
  3. xsd:attribute    标签带有的属性
  4. xsd:restriction  对标签改属性进一步的限制,进行一些值的约束

p.s:有时,可能会在

  1. <xsd:extension base="beans:identifiedType">

处报出错误

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

请跳过,这个不影响使用,只是ide的检查有问题。

声明标签

对于写好的标签定义需要把他公布到应用中,使其他spring的xml中,可用,需要几步:

1、在classpath资源路径下加入文件:

2、spring.schemas该文件中填写:

  1. http\://sammor.javaeye.com/schema/tags/springtags.xsd=conf/springtag.xsd

其中http后的“\”为转义的意思

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

在配置文件中声明引用xsd定义

当然,到这一步的时候,还仅是对定义一种自定义标签的写法,接下来,如何去做到标签的实现处理,还要做进一步的处理。spring自定义标签之三 —— 自我实现

p.s 关于xsd的规范学习,还有很多很多,它是一个强大的规范,可以为我们做很多的基础信息和约束,为使用者提供更安全方便的做法。

spring自定义标签之 规范定义XSD的更多相关文章

  1. spring自定义标签之 自我实现

     引言: 最近心情比较难以平静,周末的两天就跑出去散心了,西湖边上走走,看日落,还是不错的.回来博客上发现,在自定义标签上,最后一步实现忘记加上了.其实,人生的路程中,我们总是实现着自我的价值,让自己 ...

  2. spring基础---->spring自定义标签(一)

    Spring具有一个基于架构的扩展机制,可以使用xml文件定义和配置bean.本博客将介绍如何编写自定义XML bean的解析器,并用实例来加以说明.其实我一直相信 等你出现的时候我就知道是你. Sp ...

  3. spring自定义标签学习

    看到几篇很全的自定义标签,从定义到使用,写的很好. 这里我也是在那里学习的,对学习spring源码也很有帮助. 贴出来与大家共享. http://sammor.iteye.com/blog/11009 ...

  4. Spring 自定义标签配置

    前景:经常使用一些依赖于Spring的组件时,发现可以通过自定义配置Spring的标签来实现插件的注入,例如数据库源的配置,Mybatis的配置等.那么这些Spring标签是如何自定义配置的?学习Sp ...

  5. spring 自定义标签的实现

    在我们进行Spring 框架开发中,估计用到最多的就是bean 标签吧,其实在Spring中像<mvc/><context/>这类标签以及在dubbo配置的标签都是属于自定义的 ...

  6. 这一次搞懂Spring自定义标签以及注解解析原理

    前言 在上一篇文章中分析了Spring是如何解析默认标签的,并封装为BeanDefinition注册到缓存中,这一篇就来看看对于像context这种自定义标签是如何解析的.同时我们常用的注解如:@Se ...

  7. Spring自定义标签

    一.原理: 1.Spring通过XML解析程序将其解析为DOM树, 2.通过NamespaceHandler指定对应的Namespace的BeanDefinitionParser将其转换成BeanDe ...

  8. 自己构建一个Spring自定义标签以及原理讲解

    平时不论是在Spring配置文件中引入其他中间件(比如dubbo),还是使用切面时,都会用到自定义标签.那么配置文件中的自定义标签是如何发挥作用的,或者说程序是如何通过你添加的自定义标签实现相应的功能 ...

  9. Spring自定义标签解析与实现

           在Spring Bean注册解析(一)和Spring Bean注册解析(二)中我们讲到,Spring在解析xml文件中的标签的时候会区分当前的标签是四种基本标签(import.alias ...

随机推荐

  1. [LeetCode 题解]: Anagrams

    Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...

  2. 在 Mac OSX 上安装 nginx

    今天在使用 brew 安装 nginx 时,提示错误,安装不上去: brew install nginx, 提示:/usr/local is not writable. 这个是需要修改 /usr/lo ...

  3. Re:从零开始的Spring Security Oauth2(二)

    本文开始从源码的层面,讲解一些Spring Security Oauth2的认证流程.本文较长,适合在空余时间段观看.且涉及了较多的源码,非关键性代码以…代替. 准备工作 首先开启debug信息: l ...

  4. 关于dm-file-uploader(dmUploader)上传时传参

    官网:https://github.com/danielm/uploaderDemo Online https://danielmg.org/demo/java-script/uploader/bas ...

  5. lua luna工具库

    luna工具库 概述 luna库提供了几个lua开发的常见辅助功能: lua/c++绑定 lua序列化与反序列化 变长整数编码,用于lua序列化,当然也可以方便的用于其他场合 这里把代码编译成了动态库 ...

  6. DCL实现多线程安全的高性能懒汉模式

    DCL实现多线程安全的高性能懒汉模式 1.单线程安全的懒汉模式实现 源码: private static LazyLoad instance = null; public static LazyLoa ...

  7. java内存模型(jMM)(一)

    在说java的内存模型之前先简单的了解计算机的主存和缓存的相关概念. 多任务和高并发是衡量一台计算机处理器的重要指标.一般衡量一个服务器性能的高低好坏,使用每秒事务处理数(Transactions P ...

  8. MVC 提交List 集合 注意对应的参数名称

    public void AddMovieInfos(List<MoviesInfo> movies) { foreach (var item in movies) { dal.Add(it ...

  9. docker 容器container运行后,如何进入容器内部?

    docker exec -ti  容器ID  /bin/bash

  10. API Monitor程序分析工具简介

    API Monitor是一个免费软件,可以让你监视和控制应用程序和服务,取得该应用程序的API调用情况. 它是一个强大的工具,看到的应用程序和服务是如何工作的,或跟踪,你在自己的应用程序的问题. AP ...