spring自定义标签实现

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.   
    8.     <tsearcher:parent id="testparent">  
    9.         <tsearcher:child />  
    10.     </tsearcher:parent>  
    11.   
    12. </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.   
  3. public class TermiantorTSearcherNamespaceHandler extends  
  4.         NamespaceHandlerSupport {  
  5.     @Override  
  6.     public void init() {  
  7.         registerBeanDefinitionParser("parent"new ParentBeanParser());  
  8.         registerBeanDefinitionParser("child"new ChildParser());  
  9.     }  
  10.   
  11. }  

 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.   
  8.     <tsearcher:parent id="testparent">  
  9.         <tsearcher:child />  
  10.     </tsearcher:parent>  
  11.   
  12. </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.   
  8.     <xsd:element name="parent">   
  9.         <xsd:complexType>   
  10.            <xsd:complexContent>  
  11.             <xsd:extension base="beans:identifiedType">   
  12.                <xsd:sequence>  
  13.                      <xsd:element ref="child" />   
  14.                      </xsd:sequence>   
  15.               </xsd:extension>  
  16.             </xsd:complexContent>   
  17.             </xsd:complexType>   
  18.     </xsd:element>   
  19.       
  20.        <xsd:element  name="child">   
  21.           <xsd:complexType>   
  22.              <xsd:complexContent>     
  23.                <xsd:extension        base="beans:identifiedType">   
  24.                </xsd:extension>  
  25.              </xsd:complexContent>  
  26.             </xsd:complexType>  
  27.      </xsd:element>  
  28.   
  29. </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. /** 
  8.  * @author 百岁(baisui@taobao.com) 
  9.  * @date 2013-3-15 
  10.  */  
  11. public class ParentBeanParser extends AbstractSimpleBeanDefinitionParser {  
  12.   
  13.     @Override  
  14.     protected void doParse(Element element, ParserContext parserContext,  
  15.             BeanDefinitionBuilder builder) {  
  16.         super.doParse(element, parserContext, builder);  
  17. builder.addPropertyValue("child", parserContext.getDelegate()  
  18.                 .parseCustomElement(  
  19.                         DomUtils.getChildElementByTagName(element, "child"),  
  20.                         builder.getRawBeanDefinition()));  
  21.     }  
  22.     @Override  
  23.     protected Class<Parent> getBeanClass(Element element) {  
  24.         return Parent.class;  
  25.     }  
  26. }  

 

这里特别要说明的是,在调用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值