基于Spring開發(fā)之自定義標(biāo)簽及其解析
Spring框架是現(xiàn)在Java最流行的開源框架之一,并且Spring下的各種子項(xiàng)目對某些特定問題的解決有很好的支持。因此,如果能在Spring 基礎(chǔ)上實(shí)現(xiàn)搭建自己的一套框架(基于XML配置)。就必然需要實(shí)現(xiàn)一些自定義的標(biāo)簽,主要是方便使用我們框架的人能夠快速、簡單進(jìn)行配置。
1. XML Schema
要想自定義標(biāo)簽,首先第一步需要寫自己的XML Schema。XML Schema的個(gè)人感覺比較復(fù)雜,網(wǎng)上的教程比較簡單,因此可以參照spring-beans.xsd依葫蘆畫瓢。這里就按照我自己的理解進(jìn)行簡單介紹一下吧。
1.1 最簡單的標(biāo)簽
一個(gè)最簡單的標(biāo)簽,形式如:
<bf:head-routing key="1" value="1" to="test2"/>
該標(biāo)簽只包含了若干屬性,我們就在xsd文件中這么定義
<!-- 聲明一個(gè)標(biāo)簽,名字為head-routing,他的類型為headRouting--> <xsd:element name="head-routing" type="headRouting"></xsd:element> <!-- 定義head-routing的類型,這里定義它有key,value,to,patten四個(gè)屬性 --> <xsd:complexType name="headRouting"> <xsd:attribute name="key" type="xsd:string" use="required"></xsd:attribute> <xsd:attribute name="value" type="xsd:string" use="required"></xsd:attribute> <xsd:attribute name="to" type="xsd:IDREF" use="required"></xsd:attribute> <xsd:attribute name="patten" type="xsd:string" default="string"></xsd:attribute> </xsd:complexType>
在<xsd:attribute>標(biāo)簽中的type是用來定義該屬性的格式,例如
- xsd:string 表示是一個(gè)字符串,對格式?jīng)]什么要求
- xsd:id 表示該屬性的值是一個(gè)id,有格式要求(例如不能以數(shù)字開頭)。
- xsd:IDREF 表示該屬性的值與某xsd:id屬性的值對應(yīng)
- 其他還有很多,例如number,double,datetime等等。
1.2 復(fù)雜點(diǎn)的標(biāo)簽
所謂復(fù)雜,其實(shí)就是嵌套的標(biāo)簽,形式如:
<bf:stop id="test1" ref="testNode"> <bf:head-routing key="1" value="1" to="test2"/> </bf:stop>
其實(shí)只要參照Spring 中<bean>標(biāo)簽的xsd依葫蘆畫瓢,首先是定義stop標(biāo)簽
<xsd:element name="stop"> <xsd:complexType> <xsd:complexContent> <xsd:extension base="beans:identifiedType"> <xsd:group ref="stopElements"/> <xsd:attributeGroup ref="stopAttributes"/> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:element>
其中,
<xsd:extension base="beans:identifiedType"> 定義了該標(biāo)簽的id屬性,注意這里引用的是spring-beans中的type, <xsd:group ref="stopElements"/>中定義了<bf:stop>標(biāo)簽允許的子標(biāo)簽 <xsd:attributeGroup ref="stopAttributes"/> 定義了<bf:stop>標(biāo)簽允許的屬性 <xsd:group name="stopElements"> <xsd:sequence> <xsd:element ref="description" minOccurs="0"/> <xsd:choice minOccurs="0" maxOccurs="unbounded"> <xsd:element ref="head-routing"/> <!-- 有更多的子標(biāo)簽繼續(xù)在這里添加,例如<xsd:element ref="properties"/> --> </xsd:choice> </xsd:sequence> </xsd:group> <xsd:attributeGroup name="stopAttributes"> <xsd:attribute name="ref" type="xsd:IDREF" use="required"> <xsd:annotation> <xsd:appinfo> <!-- 這里是使用了Spring tool xsd中的標(biāo)簽,格式校驗(yàn)--> <tool:annotation kind="ref"> <tool:expected-type type="com.lizo.node.Station"/> </tool:annotation> </xsd:appinfo> </xsd:annotation> </xsd:attribute> <!-- 有更多的子標(biāo)簽繼續(xù)在這里添加,例如<xsd:attribute name="value" type="xsd:string"/> -->
2. 配置文件
完成了xsd文件編寫后,還需要讓該文件生效,就需要在項(xiàng)目的resource/META-INF包里面配置2個(gè)文件spring.handlers和spring.schemas
2.1 spring.schemas
改配置文件主要是用一個(gè)url來映射我們第一步配置好的文件,形式如下
http\://www.lizo.com/schema/bf.xsd=META-INF/bf.xsd
這樣,就可以在Spring的xml配置文件中加入spring.schemas的url,省略掉其他的,在<beans>標(biāo)簽中增加如下信息
<beans .. xmlns:bf="http://www.lizo.com/schema/bf" xsi:schemaLocation=" ... http://www.lizo.com/schema/bf http://www.lizo.com/schema/bf.xsd ">
完成這步以后,就可以在xml中寫自己的標(biāo)簽了,例如自定義標(biāo)簽的namespace為bf,
<bf:stop id="test123" ref="testNode"> <bf:head-routing key="1" value="1" to="test1"/> <bf:head-routing key="3" value="4" to="test2"/> </bf:stop>
2.2 spring.handlers
這個(gè)配置文件用來配置解析我們bf標(biāo)簽,然后生成一些BeanDefinition進(jìn)行注冊。例如
http\://www.lizo.com/schema/bf=com.lizo.config.BusinessFlowNamespaceHandlerSupport
其中 BusinessFlowNamespaceHandlerSupport就是我們用來解析標(biāo)簽
3. 自定義標(biāo)簽解析
在上一步中,我們配置了com.lizo.config.BusinessFlowNamespaceHandlerSupport類作為解析自定義標(biāo)簽的類,所以namespace為bf的標(biāo)簽,都會(huì)用這里注冊的標(biāo)簽解析器來解析
public class BusinessFlowNamespaceHandlerSupport extends NamespaceHandlerSupport { public void init() { //注冊用于解析<bf:stop>的解析器 registerBeanDefinitionParser("stop", new BusinessFlowBeanDefinitionParser()); } }
我們自定義的標(biāo)簽解析器BusinessFlowBeanDefinitionParser是要實(shí)現(xiàn)BeanDefinitionParser 接口的
public interface BeanDefinitionParser { BeanDefinition parse(Element element, ParserContext parserContext); }
一般來說,注冊bean的基本流程為:
- 解析標(biāo)簽
- 根據(jù)解析的值生成BeanDefinition,
- 注冊標(biāo)簽
解析標(biāo)簽就不用說,重點(diǎn)說說怎么生成BeanDefinition
3.1 生成BeanDefinition
一個(gè)最簡單的BeanDefinition通過設(shè)置Class和屬性的注入就可以完成。如下:
RootBeanDefinition nodeWrapDefinition = new RootBeanDefinition(); //該BeanDefinition對應(yīng)的是什么類 nodeWrapDefinition.setBeanClass(StationRoutingWrap.class); //name是解析標(biāo)簽后獲得的值 nodeWrapDefinition.getPropertyValues().addPropertyValue("name", name);
RuntimeBeanReference
RuntimeBeanReference 用于在運(yùn)行時(shí)去獲取BeanDefinition,因?yàn)樵谖覀儎?chuàng)建這個(gè)BeanDefinition的時(shí)候我們只知道他的beanName,并不確定是否已經(jīng)注冊了,這個(gè)時(shí)候就需要用RuntimeBeanReference,例如
RuntimeBeanReference refBean = new RuntimeBeanReference(ref); nodeWrapDefinition.getPropertyValues().addPropertyValue("station", refBean);
集合類BeanDefinition
某個(gè)BeanDefinition注入的屬性為一個(gè)List,這個(gè)時(shí)候就需要用ManagedList(同理有ManagedMap,ManagedSet),
ManagedList routingConditions = new ManagedList(); .... nodeWrapDefinition.getPropertyValues().add("routing", routing);
3.2 注冊bean
注冊BeanDefinitionParser 接口的函數(shù)中有個(gè)參數(shù)ParserContext,有個(gè)方法為getRegistry(),因此,注冊bean的時(shí)候就很簡單了
parserContext.getRegistry().registerBeanDefinition("beanName",nodeWrapDefinition);
總結(jié)
通過以上三步,就可以實(shí)現(xiàn)自己定義標(biāo)簽,并且在Spring容器中注入相關(guān)的bean。讓我們的框架使用起來更方便
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何解決Spring的UnsatisfiedDependencyException異常問題
這篇文章主要介紹了如何解決Spring的UnsatisfiedDependencyException異常問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04Spring boot實(shí)現(xiàn)一個(gè)簡單的ioc(2)
這篇文章主要為大家詳細(xì)介紹了Spring boot實(shí)現(xiàn)一個(gè)簡單ioc的第二篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04springboot簡單實(shí)現(xiàn)單點(diǎn)登錄的示例代碼
本文主要介紹了springboot簡單實(shí)現(xiàn)單點(diǎn)登錄的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Springboot項(xiàng)目如何使用apollo配置中心
這篇文章主要介紹了Springboot項(xiàng)目如何使用apollo配置中心,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11java構(gòu)造函數(shù)示例(構(gòu)造方法)
這篇文章主要介紹了java構(gòu)造函數(shù)示例(構(gòu)造方法),需要的朋友可以參考下2014-03-03Java設(shè)計(jì)模塊系列之書店管理系統(tǒng)單機(jī)版(三)
這篇文章主要為大家詳細(xì)介紹了Java單機(jī)版的書店管理系統(tǒng)設(shè)計(jì)模塊和思想第三章,感興趣的小伙伴們可以參考一下2016-08-08Java實(shí)現(xiàn)用戶短信驗(yàn)證碼登錄功能實(shí)例代碼
現(xiàn)在不管是各類的網(wǎng)站,還是大小社交app,登錄方式是越來越多了,但是大部分還是以短信登錄為主,本文主要介紹了java短信驗(yàn)證碼登錄功能設(shè)計(jì)與實(shí)現(xiàn),感興趣的可以了解一下2021-11-11spring框架下@value注解屬性static無法獲取值問題
這篇文章主要介紹了spring框架下@value注解屬性static無法獲取值問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11