生活随笔
收集整理的這篇文章主要介紹了
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例子:
Java代碼??
http\:??
? ?spring.shcemas例子:
Xml代碼??
http\://www.taobao.com/terminator/tsearcher.xsd=com/taobao/terminator/xsd/tsearcher.xsd??
?
? ? ?接下來要寫一個namespaceHandler 類,用來為這個名稱空間下的每個標簽定義解析器。
? ? ?例如,上面提到的TermiantorTSearcherNamespaceHandler:
Java代碼??
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代碼??
<?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代碼??
<?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不會被解析:
Java代碼??
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;?? ?? ? ? ? ?? 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是一對多關系,例如標簽格式如下:
Xml代碼??
<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自定义标签实现的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。