http://mozhenghua.iteye.com/blog/1830842

spring中编写配置可以用两种方式:

  1. 普通的通过 <bean id="" class=""><property name="" ref=""></bean> 这种默认标签配置方式
  2. 自定义Bean 配置方式,例如:
    Xml代码  
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tsearcher="http://www.taobao.com/terminator/tsearcher"
    4. xsi:schemaLocation="
    5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    6. http://www.taobao.com/terminator/tsearcher http://www.taobao.com/terminator/tsearcher.xsd">
    7. <tsearcher:parent id="testparent">
    8. <tsearcher:child />
    9. </tsearcher:parent>
    10. </beans>

    这种自定义的配置方式,在spring的实现中已经有部门实现,分别在几个命令空间中详细说明请查看(http://static.springsource.org/spring/docs/2.0.x/reference/xsd-config.html)

下面通过一个例子来说明一下,如何实现spring 自定义bean的方式来配置对象:

首先,定义两个properties文件,

  1. 一个是 spring.handlers 这个文件的作用是将配置文件中的命名空间与处理命名空间的handler(NamespaceHandlerSupport)联系起来,
  2. 另外一个文件是spring.schemas 文件,这个文件的作用是定义xsd文件的虚拟路径

spring.handlers例子:

Java代码  
  1. http\://www.taobao.com/terminator/tsearcher=com.taobao.terminator.tag.TermiantorTSearcherNamespaceHandler

spring.shcemas例子:

Xml代码  
  1. http\://www.taobao.com/terminator/tsearcher.xsd=com/taobao/terminator/xsd/tsearcher.xsd

接下来要写一个namespaceHandler 类,用来为这个名称空间下的每个标签定义解析器。

例如,上面提到的TermiantorTSearcherNamespaceHandler:

Java代码  
  1. import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
  2. public class TermiantorTSearcherNamespaceHandler extends
  3. NamespaceHandlerSupport {
  4. @Override
  5. public void init() {
  6. registerBeanDefinitionParser("parent", new ParentBeanParser());
  7. registerBeanDefinitionParser("child", new ChildParser());
  8. }
  9. }

init方法中调用了两次registerBeanDefinitionParser,申明了parent,child 标签的解析器。

parent标签和child标签的关系是父子关系,spring配置文件如下:

Xml代码  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tsearcher="http://www.taobao.com/terminator/tsearcher"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  6. http://www.taobao.com/terminator/tsearcher http://www.taobao.com/terminator/tsearcher.xsd">
  7. <tsearcher:parent id="testparent">
  8. <tsearcher:child />
  9. </tsearcher:parent>
  10. </beans>

定义tsearcher.xsd的xml元素结构信息:

Xml代码  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsd:schema xmlns="http://www.taobao.com/terminator/tsearcher"
  3. xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans"
  4. targetNamespace="http://www.taobao.com/terminator/tsearcher"
  5. elementFormDefault="qualified" attributeFormDefault="unqualified">
  6. <xsd:import namespace="http://www.springframework.org/schema/beans" />
  7. <xsd:element name="parent">
  8. <xsd:complexType>
  9. <xsd:complexContent>
  10. <xsd:extension base="beans:identifiedType">
  11. <xsd:sequence>
  12. <xsd:element ref="child" />
  13. </xsd:sequence>
  14. </xsd:extension>
  15. </xsd:complexContent>
  16. </xsd:complexType>
  17. </xsd:element>
  18. <xsd:element  name="child">
  19. <xsd:complexType>
  20. <xsd:complexContent>
  21. <xsd:extension        base="beans:identifiedType">
  22. </xsd:extension>
  23. </xsd:complexContent>
  24. </xsd:complexType>
  25. </xsd:element>
  26. </xsd:schema>

这里最重要的是parent标签的解析器ParentBeanParser,在doParse方法中,还需要启动子标签child的解析流程,不然的话子标签child不会被解析:

Java代码  
  1. import org.springframework.beans.factory.support.BeanDefinitionBuilder;
  2. import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
  3. import org.springframework.beans.factory.xml.ParserContext;
  4. import org.springframework.util.xml.DomUtils;
  5. import org.w3c.dom.Element;
  6. /**
  7. * @author 百岁(baisui@taobao.com)
  8. * @date 2013-3-15
  9. */
  10. public class ParentBeanParser extends AbstractSimpleBeanDefinitionParser {
  11. @Override
  12. protected void doParse(Element element, ParserContext parserContext,
  13. BeanDefinitionBuilder builder) {
  14. super.doParse(element, parserContext, builder);
  15. builder.addPropertyValue("child", parserContext.getDelegate()
  16. .parseCustomElement(
  17. DomUtils.getChildElementByTagName(element, "child"),
  18. builder.getRawBeanDefinition()));
  19. }
  20. @Override
  21. protected Class<Parent> getBeanClass(Element element) {
  22. return Parent.class;
  23. }
  24. }

这里特别要说明的是,在调用parserContext.getDelegate()

.parseCustomElement(DomUtils.getChildElementByTagName(element, "child"), builder.getRawBeanDefinition())

方法的时候,方法parseCustomElement的第二个beanDefinition参数是必须的,不然的话框架会认为这个元素是根结点元素,必须要有一个id属性。

接下来又出现一个新的需求,如果parent和child是一对多关系,例如标签格式如下:

Xml代码  
  1. <tsearcher:parent id="testparent">
  2. <tsearcher:child name="1"/>
  3. <tsearcher:child name="2"/>
  4. <tsearcher:child name="3"/>
  5. </tsearcher:parent>

显然,用上面介绍的ParentBeanParser这个类中的解析标签的方式是不能满足需求的。

如果要知道如何解决一对多的关系请查阅下一篇博客(http://mozhenghua.iteye.com/blog/1914155)

结束

========http://blog.csdn.net/lcllcl987/article/details/4017597

spring自定义标签实现相关推荐

  1. Spring自定义标签使用及原理

    最近大半年一直在看spring的源码,寻思着需要写点什么,也锻炼下自己文档编写的能力.本篇我们讲解spring自定义标签的使用及原理,分为以下小节进行讲解. 自定义标签的用途 自定义标签使用 自定义标 ...

  2. spring 自定义标签的实现

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

  3. return error怎么定义_这一次搞懂Spring自定义标签以及注解解析原理

    自定义标签解析原理 在上一篇分析默认标签解析时看到过这个类DefaultBeanDefinitionDocumentReader的方法parseBeanDefinitions:拉勾IT课小编为大家分解 ...

  4. 【Spring学习】Spring自定义标签详细步骤

    目录标题 前言 一.自定义标签步骤 1.定义属性POJO 2.定义XSD文件描述组件内容 3.定义标签解析器 4.注册标签解析器 5.定义spring.handlers和spring.schemas文 ...

  5. dubbo源码学习(二) : spring 自定义标签

    做dubbo的配置时很容易发现,dubbo有一套自己的标签,提供给开发者配置,其实每一个标签对应着一个 实体,在容器启动的时候,dubbo会对所有的配置进行解析然后将解析后的内容设置到实体里,最终du ...

  6. spring 自定义标签 实现步骤 比如<wly:person address=“就是在这里“ age=“18“ name=“赵子龙“ id=“wly“></wly:person>

    步骤 1. 加载 spring.handler 配置文件 2. 将配置文件内容加载到 map集合中 3. 根据对应的 key 获取对应的处理器 如何自定义一个标签 1. 创建一个解析器处理类(在ini ...

  7. spring 自定义标签 学习二

    在上篇中写的只支持写属性,不支持标签property的写法,但是如果有时候我们还想做成支持 property的用法,则可以在xsd中增加spring 自带的xsd引用 修改xsd文件如下: <? ...

  8. spring 自定义标签 学习

    自定义配置文件到spring 中,有时候想做一些数据结构的配置化信息,根据业务做一个扩展. 首先: 在项目的META-INF目录下新建两个文件spring.handlers,和spring.shcem ...

  9. component是什么接口_逐行解读Spring(二)什么,自定义标签没听说过?

    一.自定义标签是什么? 上一篇我们讲了默认标签-bean标签的解析,今天我们讲一下自定义标签的解析. 1. 自定义标签的定义 这个问题其实上一篇有讲过,这边再复述一遍,在spring的xml配置文件中 ...

最新文章

  1. php修改数据库字段内容,php对数据库的增删改查操作
  2. 深度学习数据集+模型说明
  3. 【2018年更新】Sublime text 3安装教程(Windows版本)
  4. 常用公差配合表图_模具设计,常用模具零件选用
  5. ad 单点登录 java 访问权限_如何配置Portal 基于AD的单点登录配置
  6. 【SpringBoot】Logback日志框架介绍和SpringBoot整合实战
  7. 解析函数论 Page 8 $f(x)$在$x_0$处解析的充要条件
  8. Linux - ATT汇编基础
  9. 华为资深工程师总结的这本435页的书,居然把网络协议给趣谈了
  10. 【滤波跟踪】捷联惯导纯惯导解算matlab实现
  11. RTSP视频流直播实现(海康)
  12. SQLServer数据库可疑的解决方法
  13. 锐起无盘找不到服务器,锐起无盘出现重启后连接不到服务器
  14. ISO_IEC_27003:2017信息安全管理体系中文解读
  15. 信道估计的理解(转载借鉴)
  16. 第三周:tesseral 2D软件的使用
  17. eclispe/myeclipse中输入法的问题
  18. 鼠标悬浮显示图片和文字
  19. matplotlib 合并cmap,创建cmap,创建listedcolormap
  20. C++打印图片的方法

热门文章

  1. 785. Is Graph Bipartite? 判断二分图
  2. 2011年第二届蓝桥杯决赛 —— C语言本科 —— 第一题
  3. 信息学奥赛一本通(C++)在线评测系统——基础(一)C++语言——1102:与指定数字相同的数的个数
  4. 【Linux】一步一步学Linux——tail命令(42)
  5. 【内核驱动】 Kconfig简介
  6. python基础list_Python基础4(list:列表)
  7. 学习笔记-----关于VS中使用模板类出现无法解析的外部符号问题
  8. Shell(4)——测试test、[]、逻辑、||文件-f、-d、-x、-eq、-gt、-ge、-lt、-le、-ne
  9. PHP读写操作Excel
  10. HDU - 6704 K-th occurrence (后缀数组+主席树)