Development Based on Spring -- custom tag and Its parsing, spring custom tag

Source: Internet
Author: User

Development Based on Spring -- custom tag and Its parsing, spring custom tag
1. Simple labels of XML Schema1.1

The simplest tag format is as follows:

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

This tag only contains several attributes, which are defined in the xsd file.

<! -- Declare a label named head-routing and its type is headRouting --> <xsd: element name = "head-routing" type = "headRouting"> </xsd: element> <! -- Define the head-routing type. Here, four attributes are defined: key, value, to, and 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> in <xsd: attribute> label
The type in is used to define the format of this attribute, for example
  • Xsd: string indicates a string, which has no requirements on the format.
  • Xsd: id indicates that the value of this attribute is an id with a format requirement (for example, it cannot start with a number ).
  • Xsd: IDREF indicates that the value of this attribute corresponds to the value of an xsd: id attribute.
  • There are many others, such as number, double, datetime, and so on.
1.2 tags of complex points

The so-called complexity is actually nested labels, in the form:

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

 

In fact, as long as you refer to the xsd of the <bean> label in Spring, the stop label is defined first.

    <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>

Where,

  • <Xsd: extension base = "beans: identifiedType"> defines the id attribute of the label. Note that the type in spring-beans is referenced here,
  • <Xsd: group ref = "stopElements"/> defines the child tags allowed by the <bf: stop> label.
  • <Xsd: attributeGroup ref = "stopAttributes"/> defines the attributes allowed by the <bf: stop> label.
<Xsd: group name = "stopElements"> <xsd: sequence> <xsd: element ref = "description" minOccurs = "0"/> <xsd: choice minOccurs = "0" maxOccurs = "unbounded"> <xsd: element ref = "head-routing"/> <! -- More sub-tags are added here, for example, <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> <! -- The label in Spring tool xsd is used for format validation. --> <tool: annotation kind = "ref"> <tool: expected-type = "com. lizo. node. station "/> </tool: annotation> </xsd: appinfo> </xsd: annotation> </xsd: attribute> <! -- More sub-tags are added here, for example, <xsd: attribute name = "value" type = "xsd: string"/> -->

 

2. Configuration File

After writing the xsd file, you also need to make the file take effect, you need to configure two files in the project's resource/META-INF package spring. handlers and spring. schemas

2.1 spring. schemas

To change the configuration file, we use a url to map the file we configured in the first step. The format is as follows:

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

In this way, you can add the Spring. schemas url to the xml configuration file of spring, omit other URLs, and add the following information to the <beans> label.

    <beans        ..       xmlns:bf="http://www.lizo.com/schema/bf"       xsi:schemaLocation="        ...        http://www.lizo.com/schema/bf        http://www.lizo.com/schema/bf.xsd        ">

After completing this step, you can write your own labels in xml. For example, the namespace of a custom tag is 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

This configuration file is used to configure and parse our bf tag, and then generate some BeanDefinition for registration. For example

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

BusinessFlowNamespaceHandlerSupport is used to parse tags.

3. Custom Tag Parsing

In the previous step, we configured the com. lizo. config. BusinessFlowNamespaceHandlerSupport class as the class for parsing custom labels. Therefore, tags whose namespace is bf will be parsed using the label parser registered here.

Public class BusinessFlowNamespaceHandlerSupport extends NamespaceHandlerSupport {public void init () {// register the registerBeanDefinitionParser ("stop", new partition () Parser for parsing <bf: stop> ());}}

The custom label parser BusinessFlowBeanDefinitionParser is used to implement the BeanDefinitionParser interface.

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

Generally, the basic process for registering a bean is as follows:

Not to mention parsing tags. Let's focus on how to generate BeanDefinition.

3.1 generate BeanDefinition

A simplest BeanDefinition can be completed by setting Class and attribute injection. As follows:

RootBeanDefinition nodeWrapDefinition = new RootBeanDefinition (); // The class nodeWrapDefinition corresponding to this BeanDefinition. setBeanClass (StationRoutingWrap. class); // name is the value nodeWrapDefinition obtained after the tag is parsed. getPropertyValues (). addPropertyValue ("name", name );
RuntimeBeanReference

RuntimeBeanReference is used to get BeanDefinition at runtime, because we only know its beanName when we create this BeanDefinition and are not sure whether it has been registered. RuntimeBeanReference is required at this time, for example

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

A BeanDefinition injection attribute is a List, which requires ManagedList (similarly, ManagedMap and ManagedSet ),

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

The function that registers the BeanDefinitionParser interface has a ParserContext parameter and a method is getRegistry (). Therefore, it is easy to note the bean.

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

With the preceding three steps, you can define your own labels and inject related beans into the Spring container. Make our framework more convenient to use (more compatible with B)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.