spring自定义标签实现
http://mozhenghua.iteye.com/blog/1830842
spring中编写配置可以用两种方式:
- 普通的通过 <bean id="" class=""><property name="" ref=""></bean> 这种默认标签配置方式
- 自定义Bean 配置方式,例如:
Xml代码
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tsearcher="http://www.taobao.com/terminator/tsearcher"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.taobao.com/terminator/tsearcher http://www.taobao.com/terminator/tsearcher.xsd">
- <tsearcher:parent id="testparent">
- <tsearcher:child />
- </tsearcher:parent>
- </beans>
这种自定义的配置方式,在spring的实现中已经有部门实现,分别在几个命令空间中详细说明请查看(http://static.springsource.org/spring/docs/2.0.x/reference/xsd-config.html)
下面通过一个例子来说明一下,如何实现spring 自定义bean的方式来配置对象:
首先,定义两个properties文件,
- 一个是 spring.handlers 这个文件的作用是将配置文件中的命名空间与处理命名空间的handler(NamespaceHandlerSupport)联系起来,
- 另外一个文件是spring.schemas 文件,这个文件的作用是定义xsd文件的虚拟路径
spring.handlers例子:
- http\://www.taobao.com/terminator/tsearcher=com.taobao.terminator.tag.TermiantorTSearcherNamespaceHandler
spring.shcemas例子:
- http\://www.taobao.com/terminator/tsearcher.xsd=com/taobao/terminator/xsd/tsearcher.xsd
接下来要写一个namespaceHandler 类,用来为这个名称空间下的每个标签定义解析器。
例如,上面提到的TermiantorTSearcherNamespaceHandler:
- import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
- public class TermiantorTSearcherNamespaceHandler extends
- NamespaceHandlerSupport {
- @Override
- public void init() {
- registerBeanDefinitionParser("parent", new ParentBeanParser());
- registerBeanDefinitionParser("child", new ChildParser());
- }
- }
init方法中调用了两次registerBeanDefinitionParser,申明了parent,child 标签的解析器。
parent标签和child标签的关系是父子关系,spring配置文件如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tsearcher="http://www.taobao.com/terminator/tsearcher"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.taobao.com/terminator/tsearcher http://www.taobao.com/terminator/tsearcher.xsd">
- <tsearcher:parent id="testparent">
- <tsearcher:child />
- </tsearcher:parent>
- </beans>
定义tsearcher.xsd的xml元素结构信息:
- <?xml version="1.0" encoding="UTF-8"?>
- <xsd:schema xmlns="http://www.taobao.com/terminator/tsearcher"
- xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans"
- targetNamespace="http://www.taobao.com/terminator/tsearcher"
- elementFormDefault="qualified" attributeFormDefault="unqualified">
- <xsd:import namespace="http://www.springframework.org/schema/beans" />
- <xsd:element name="parent">
- <xsd:complexType>
- <xsd:complexContent>
- <xsd:extension base="beans:identifiedType">
- <xsd:sequence>
- <xsd:element ref="child" />
- </xsd:sequence>
- </xsd:extension>
- </xsd:complexContent>
- </xsd:complexType>
- </xsd:element>
- <xsd:element name="child">
- <xsd:complexType>
- <xsd:complexContent>
- <xsd:extension base="beans:identifiedType">
- </xsd:extension>
- </xsd:complexContent>
- </xsd:complexType>
- </xsd:element>
- </xsd:schema>
这里最重要的是parent标签的解析器ParentBeanParser,在doParse方法中,还需要启动子标签child的解析流程,不然的话子标签child不会被解析:
- import org.springframework.beans.factory.support.BeanDefinitionBuilder;
- import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
- import org.springframework.beans.factory.xml.ParserContext;
- import org.springframework.util.xml.DomUtils;
- import org.w3c.dom.Element;
- /**
- * @author 百岁(baisui@taobao.com)
- * @date 2013-3-15
- */
- public class ParentBeanParser extends AbstractSimpleBeanDefinitionParser {
- @Override
- protected void doParse(Element element, ParserContext parserContext,
- BeanDefinitionBuilder builder) {
- super.doParse(element, parserContext, builder);
- builder.addPropertyValue("child", parserContext.getDelegate()
- .parseCustomElement(
- DomUtils.getChildElementByTagName(element, "child"),
- builder.getRawBeanDefinition()));
- }
- @Override
- protected Class<Parent> getBeanClass(Element element) {
- return Parent.class;
- }
- }
这里特别要说明的是,在调用parserContext.getDelegate()
.parseCustomElement(DomUtils.getChildElementByTagName(element, "child"), builder.getRawBeanDefinition())
方法的时候,方法parseCustomElement的第二个beanDefinition参数是必须的,不然的话框架会认为这个元素是根结点元素,必须要有一个id属性。
接下来又出现一个新的需求,如果parent和child是一对多关系,例如标签格式如下:
- <tsearcher:parent id="testparent">
- <tsearcher:child name="1"/>
- <tsearcher:child name="2"/>
- <tsearcher:child name="3"/>
- </tsearcher:parent>
显然,用上面介绍的ParentBeanParser这个类中的解析标签的方式是不能满足需求的。
如果要知道如何解决一对多的关系请查阅下一篇博客(http://mozhenghua.iteye.com/blog/1914155)
结束
========http://blog.csdn.net/lcllcl987/article/details/4017597
spring自定义标签实现相关推荐
- Spring自定义标签使用及原理
最近大半年一直在看spring的源码,寻思着需要写点什么,也锻炼下自己文档编写的能力.本篇我们讲解spring自定义标签的使用及原理,分为以下小节进行讲解. 自定义标签的用途 自定义标签使用 自定义标 ...
- spring 自定义标签的实现
在我们进行Spring 框架开发中,估计用到最多的就是bean 标签吧,其实在Spring中像<mvc/><context/>这类标签以及在dubbo配置的标签都是属于自定义的 ...
- return error怎么定义_这一次搞懂Spring自定义标签以及注解解析原理
自定义标签解析原理 在上一篇分析默认标签解析时看到过这个类DefaultBeanDefinitionDocumentReader的方法parseBeanDefinitions:拉勾IT课小编为大家分解 ...
- 【Spring学习】Spring自定义标签详细步骤
目录标题 前言 一.自定义标签步骤 1.定义属性POJO 2.定义XSD文件描述组件内容 3.定义标签解析器 4.注册标签解析器 5.定义spring.handlers和spring.schemas文 ...
- dubbo源码学习(二) : spring 自定义标签
做dubbo的配置时很容易发现,dubbo有一套自己的标签,提供给开发者配置,其实每一个标签对应着一个 实体,在容器启动的时候,dubbo会对所有的配置进行解析然后将解析后的内容设置到实体里,最终du ...
- spring 自定义标签 实现步骤 比如<wly:person address=“就是在这里“ age=“18“ name=“赵子龙“ id=“wly“></wly:person>
步骤 1. 加载 spring.handler 配置文件 2. 将配置文件内容加载到 map集合中 3. 根据对应的 key 获取对应的处理器 如何自定义一个标签 1. 创建一个解析器处理类(在ini ...
- spring 自定义标签 学习二
在上篇中写的只支持写属性,不支持标签property的写法,但是如果有时候我们还想做成支持 property的用法,则可以在xsd中增加spring 自带的xsd引用 修改xsd文件如下: <? ...
- spring 自定义标签 学习
自定义配置文件到spring 中,有时候想做一些数据结构的配置化信息,根据业务做一个扩展. 首先: 在项目的META-INF目录下新建两个文件spring.handlers,和spring.shcem ...
- component是什么接口_逐行解读Spring(二)什么,自定义标签没听说过?
一.自定义标签是什么? 上一篇我们讲了默认标签-bean标签的解析,今天我们讲一下自定义标签的解析. 1. 自定义标签的定义 这个问题其实上一篇有讲过,这边再复述一遍,在spring的xml配置文件中 ...
最新文章
- php修改数据库字段内容,php对数据库的增删改查操作
- 深度学习数据集+模型说明
- 【2018年更新】Sublime text 3安装教程(Windows版本)
- 常用公差配合表图_模具设计,常用模具零件选用
- ad 单点登录 java 访问权限_如何配置Portal 基于AD的单点登录配置
- 【SpringBoot】Logback日志框架介绍和SpringBoot整合实战
- 解析函数论 Page 8 $f(x)$在$x_0$处解析的充要条件
- Linux - ATT汇编基础
- 华为资深工程师总结的这本435页的书,居然把网络协议给趣谈了
- 【滤波跟踪】捷联惯导纯惯导解算matlab实现
- RTSP视频流直播实现(海康)
- SQLServer数据库可疑的解决方法
- 锐起无盘找不到服务器,锐起无盘出现重启后连接不到服务器
- ISO_IEC_27003:2017信息安全管理体系中文解读
- 信道估计的理解(转载借鉴)
- 第三周:tesseral 2D软件的使用
- eclispe/myeclipse中输入法的问题
- 鼠标悬浮显示图片和文字
- matplotlib 合并cmap,创建cmap,创建listedcolormap
- C++打印图片的方法
热门文章
- 785. Is Graph Bipartite? 判断二分图
- 2011年第二届蓝桥杯决赛 —— C语言本科 —— 第一题
- 信息学奥赛一本通(C++)在线评测系统——基础(一)C++语言——1102:与指定数字相同的数的个数
- 【Linux】一步一步学Linux——tail命令(42)
- 【内核驱动】 Kconfig简介
- python基础list_Python基础4(list:列表)
- 学习笔记-----关于VS中使用模板类出现无法解析的外部符号问题
- Shell(4)——测试test、[]、逻辑、||文件-f、-d、-x、-eq、-gt、-ge、-lt、-le、-ne
- PHP读写操作Excel
- HDU - 6704 K-th occurrence (后缀数组+主席树)