spring自定义标签实现

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

  1.  普通的通过 <bean id="" class=""><property name="" ref=""></bean> 这种默认标签配置方式
  2. 自定义Bean 配置方式,例如:
    <?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文件,

  1. 一个是 spring.handlers 这个文件的作用是将配置文件中的命名空间与处理命名空间的handler(NamespaceHandlerSupport)联系起来,
  2. 另外一个文件是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)

 结束

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值