基於Spring開發——自定義標簽及其解析


 

1. XML Schema

1.1 最簡單的標簽

一個最簡單的標簽,形式如:

<bf:head-routing key="1" value="1" to="test2"/>

該標簽只包含了若干屬性,我們就在xsd文件中這么定義

<!-- 聲明一個標簽,名字為head-routing,他的類型為headRouting-->
<xsd:element name="head-routing" type="headRouting"></xsd:element>

    <!--  定義head-routing的類型,這里定義它有key,value,to,patten四個屬性 -->
    <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>標簽
中的type是用來定義該屬性的格式,例如
  • xsd:string 表示是一個字符串,對格式沒什么要求
  • xsd:id 表示該屬性的值是一個id,有格式要求(例如不能以數字開頭)。
  • xsd:IDREF 表示該屬性的值與某xsd:id屬性的值對應
  • 其他還有很多,例如number,double,datetime等等。

1.2 復雜點的標簽

所謂復雜,其實就是嵌套的標簽,形式如:

    <bf:stop id="test1" ref="testNode">
        <bf:head-routing key="1" value="1" to="test2"/>
    </bf:stop>

 

其實只要參照Spring 中<bean>標簽的xsd依葫蘆畫瓢,首先是定義stop標簽

    <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"> 定義了該標簽的id屬性,注意這里引用的是spring-beans中的type,
  • <xsd:group ref="stopElements"/>中定義了<bf:stop>標簽允許的子標簽
  • <xsd:attributeGroup ref="stopAttributes"/> 定義了<bf:stop>標簽允許的屬性
    <xsd:group name="stopElements">
        <xsd:sequence>
            <xsd:element ref="description" minOccurs="0"/>
            <xsd:choice minOccurs="0" maxOccurs="unbounded">
                <xsd:element ref="head-routing"/>
                  <!-- 有更多的子標簽繼續在這里添加,例如<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中的標簽,格式校驗-->
                    <tool:annotation kind="ref">
                        <tool:expected-type type="com.lizo.node.Station"/>
                    </tool:annotation>
                </xsd:appinfo>
            </xsd:annotation>
        </xsd:attribute>
        <!-- 有更多的子標簽繼續在這里添加,例如<xsd:attribute name="value" type="xsd:string"/> -->

 

2. 配置文件

完成了xsd文件編寫后,還需要讓該文件生效,就需要在項目的resource/META-INF包里面配置2個文件spring.handlers和spring.schemas

2.1 spring.schemas

改配置文件主要是用一個url來映射我們第一步配置好的文件,形式如下

http\://www.lizo.com/schema/bf.xsd=META-INF/bf.xsd

這樣,就可以在Spring的xml配置文件中加入spring.schemas的url,省略掉其他的,在<beans>標簽中增加如下信息

    <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中寫自己的標簽了,例如自定義標簽的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

這個配置文件用來配置解析我們bf標簽,然后生成一些BeanDefinition進行注冊。例如

http\://www.lizo.com/schema/bf=com.lizo.config.BusinessFlowNamespaceHandlerSupport

其中 BusinessFlowNamespaceHandlerSupport就是我們用來解析標簽

3. 自定義標簽解析

在上一步中,我們配置了com.lizo.config.BusinessFlowNamespaceHandlerSupport類作為解析自定義標簽的類,所以namespace為bf的標簽,都會用這里注冊的標簽解析器來解析

public class BusinessFlowNamespaceHandlerSupport extends NamespaceHandlerSupport {
    public void init() {
        //注冊用於解析<bf:stop>的解析器
        registerBeanDefinitionParser("stop", new BusinessFlowBeanDefinitionParser());
    }
}

我們自定義的標簽解析器BusinessFlowBeanDefinitionParser是要實現BeanDefinitionParser 接口的

public interface BeanDefinitionParser {
    BeanDefinition parse(Element element, ParserContext parserContext);
}

一般來說,注冊bean的基本流程為:

  1. 解析標簽
  2. 根據解析的值生成BeanDefinition,
  3. 注冊標簽

解析標簽就不用說,重點說說怎么生成BeanDefinition

3.1 生成BeanDefinition

一個最簡單的BeanDefinition通過設置Class和屬性的注入就可以完成。如下:

RootBeanDefinition nodeWrapDefinition = new RootBeanDefinition();
//該BeanDefinition對應的是什么類
nodeWrapDefinition.setBeanClass(StationRoutingWrap.class);
//name是解析標簽后獲得的值
nodeWrapDefinition.getPropertyValues().addPropertyValue("name", name);

RuntimeBeanReference

RuntimeBeanReference 用於在運行時去獲取BeanDefinition,因為在我們創建這個BeanDefinition的時候我們只知道他的beanName,並不確定是否已經注冊了,這個時候就需要用RuntimeBeanReference,例如

RuntimeBeanReference refBean = new RuntimeBeanReference(ref);
        nodeWrapDefinition.getPropertyValues().addPropertyValue("station", refBean);

集合類BeanDefinition

某個BeanDefinition注入的屬性為一個List,這個時候就需要用ManagedList(同理有ManagedMap,ManagedSet),

ManagedList routingConditions = new ManagedList();
....
nodeWrapDefinition.getPropertyValues().add("routing", routing);

3.2 注冊bean

注冊BeanDefinitionParser 接口的函數中有個參數ParserContext,有個方法為getRegistry(),因此,注冊bean的時候就很簡單了

parserContext.getRegistry().registerBeanDefinition("beanName",nodeWrapDefinition);

總結

通過以上三步,就可以實現自己定義標簽,並且在Spring容器中注入相關的bean。讓我們的框架使用起來更方便(更裝B)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM