Spring Custom Labels

Source: Internet
Author: User

Overall idea:

    • Create a component that needs to be extended
    • Defines an XSD file that describes the contents of a component
    • Create a Java class that implements the Beandefinitionparser interface for parsing definitions and component definitions in XSD files
    • Create a handler class that extends the child namespacehandlersupport to register the component with the container.
    • Write (add) spring.handlers and Spring.schemas files.

Parsing process: Find the corresponding XSD file by Spring.schemas, validate the XML format, find the corresponding Namespacehandler class by Spring.handlers as the class to parse the custom label, beandefinition implement the class through the parameters in the Init method, Generates a beandefinition based on the parsed value,

Directory structure

To create a model class
 Public classApple {PrivateString name; PrivateString AppleColor;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString Getapplecolor () {returnAppleColor; }     Public voidSetapplecolor (String applecolor) { This. AppleColor =AppleColor; }}
Create an XSD file

For the definition of such tags, there is a corresponding XSD definition document in spring
Http://www.springframework.org/schema/beans
For XSD, simply the definition of a label for XML, where there is no explanation for too many XSD, see
Http://www.w3school.com.cn/schema/schema_example.asp

<?XML version= "1.0" encoding= "UTF-8"?><Xsd:schemaxmlns= "Http://www.zyx.com/schema/apple"xmlns:xsd= "Http://www.w3.org/2001/XMLSchema"Xmlns:beans= "Http://www.springframework.org/schema/beans"targetnamespace= "Http://www.zyx.com/schema/apple"elementFormDefault= "qualified"attributeFormDefault= "unqualified">    <Xsd:importnamespace= "Http://www.springframework.org/schema/beans"schemalocation= "Http://www.springframework.org/schema/beans/spring-beans.xsd" />    <xsd:elementname= "Apple">        <Xsd:complextype>            <Xsd:all>                <xsd:elementref= "Apple-color"minOccurs= "1"maxOccurs= "1"/>            </Xsd:all>            <Xsd:attributename= "id"type= "Xsd:string" />            <Xsd:attributename= "Name"type= "Xsd:string" Use= "Required"/>        </Xsd:complextype>    </xsd:element>    <xsd:elementname= "Apple-color">        <Xsd:complextype>            <Xsd:attributename= "Color"type= "Xsd:string" Use= "Required"/>        </Xsd:complextype>    </xsd:element></Xsd:schema>

Type is the format used to define the property, such as
Xsd:string is a string that doesn't require a format.
Xsd:id indicates that the value of this property is an ID and has formatting requirements (for example, you cannot start with a number).
Xsd:idref indicates that the value of this property corresponds to the value of a Xsd:id property
There are many others, such as Number,double,datetime and so on.

Writing Spring.schemas

The configuration file is mainly a URL to map our first step of the configured file, the form is as follows

Http\://www.zyx.com/schema/apple.xsd=META-INF/namespace/apple.xsd
Writing beandefinition, parsing xml
 Public classApplebeandefinitionparserextendsAbstractsinglebeandefinitionparser {protectedClass Getbeanclass (element Element) {returnApple.class; }    protected voidDoparse (element element, Beandefinitionbuilder Bean) {String name= Element.getattribute ("name"); String Color=Parseapplecolor (Element); if(Stringutils.hastext (name)) {Bean.addpropertyvalue ("Name", name); }        if(Stringutils.hastext (color)) {Bean.addpropertyvalue ("AppleColor", color); }    }    PrivateString Parseapplecolor (element Element) {element Colorelement= Domutils.getchildelementbytagname (element, "Apple-color"); returnColorelement.getattribute ("Color"); }}
Writing Namespacehandlersupport

We configured the Com.zyx.demo.springNamespace.AppleNamespaceHandler class as a class for parsing custom tags, so namespace is Apple's label and will use the tag parser registered here to parse

 Public class extends Namespacehandlersupport {    publicvoid  init () {        Registerbeandefinitionparser ("Apple",new  applebeandefinitionparser ());}    }
Writing Spring.handlers

This configuration file is used to configure the parsing of our Apple tags and then generate some beandefinition to register

Http\://Www.zyx.com/schema/apple=com.zyx.demo.springNamespace.AppleNamespaceHandler

We're done here, and here's the test.

Test: Create Spring.xml
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:myname= "Http://www.zyx.com/schema/apple"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.2.xsd http://www.zyx.com/schema/apple http://www.zyx.com/schema/apple.xsd "Default-autowire= "ByName">    <Myname:appleID= "XXX"name= "Hongfushi">        <Myname:apple-colorColor= "Red"></Myname:apple-color>    </Myname:apple></Beans>
Test code
@Controller @requestmapping ("/test")  Public class testspringnamespace {    @ResponseBody    @RequestMapping ("Spring/namespace")      Public String test () {        = Springutils.getbean (Apple.  Class);        System.out.println (Apple.tostring ());         return apple.tostring ();    }}

Spring Custom Labels

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.