Spring's Custom labels

Source: Internet
Author: User

When spring gets an element, the first thing to do is to parse it according to the namespace, and if it is the default namespace, parse the element using the Parsedefaultelement method, or use the Parsecustom element method.

    protected voidparsebeandefinitions (Element root, beandefinitionparserdelegate delegate) {if(delegate.isdefaultnamespace (root)) {NodeList nl=root.getchildnodes ();  for(inti = 0; I < nl.getlength (); i++) {node node=Nl.item (i); if(nodeinstanceofElement) {Element Ele=(Element) node; if(delegate.isdefaultnamespace (ele)) {parsedefaultelement (ele, delegate); }                    Else{delegate.parsecustomelement (ele); }                }            }        }        Else{delegate.parsecustomelement (root); }    }     Publicbeandefinition parsecustomelement (Element ele, beandefinition containingbd) {String NamespaceURI=Getnamespaceuri (ele); Namespacehandler Handler= This. Readercontext.getnamespacehandlerresolver (). Resolve (NamespaceURI); if(Handler = =NULL) {error ("Unable to locate Spring Namespacehandler for XML schema namespace [" + NamespaceURI + "]", ele); return NULL; }        returnHandler.parse (Ele,NewParserContext ( This. Readercontext, This, CONTAININGBD)); }
Use of custom labels

Extending the Spring Custom label configuration takes a few steps (provided the spring core package is added to the project).

    1. Create a component that needs to be extended.
    2. Defines an XSD file that describes the component content.
    3. Create a file that implements the Beandefinitionparser interface for parsing definitions and component definitions in XSD files.
    4. Create a handler file, extended from Namespacehandlersupport, to register the component with the spring container.
    5. Write Spring.handlers and Spring.schemas files.

Now let's follow the steps above to experience the custom label process step-by-step.

The first step:

 Public class Testbean {    private  String name;      Public String GetName () {        return  name;    }      Public void setName (String name) {        this. Name= name;    }    @Override    public  String toString () {        return "testbean{" +                " Name= ' + name + ' \ ' +                '} ';}    }

Step Two:

<?XML version= "1.0" encoding= "UTF-8" standalone= "no"?>  <Xsd:schemaxmlns= "Http://code.alibabatech.com/schema/dubbo"xmlns:xsd= "Http://www.w3.org/2001/XMLSchema"targetnamespace= "Http://code.alibabatech.com/schema/dubbo">            <xsd:elementname= "Custom"type= "Customtype">          </xsd:element>          <Xsd:complextypename= "Customtype">                  <Xsd:attributename= "id"type= "Xsd:id">                  </Xsd:attribute>                  <Xsd:attributename= "Name"type= "Xsd:string">                  </Xsd:attribute>          </Xsd:complextype>    </Xsd:schema> 

Step Three:

Importorg.springframework.beans.factory.config.BeanDefinition; Importorg.springframework.beans.factory.support.RootBeanDefinition; ImportOrg.springframework.beans.factory.xml.BeanDefinitionParser; ImportOrg.springframework.beans.factory.xml.ParserContext; Importorg.w3c.dom.Element;  Public classTestcustombeandefinitionparserImplementsBeandefinitionparser { PublicBeandefinition Parse (element element, ParserContext ParserContext) {String ID= Element.getattribute ("id"); String name= Element.getattribute ("name"); Rootbeandefinition beandefinition=Newrootbeandefinition (); Beandefinition.setbeanclass (Testbean.class); Beandefinition.getpropertyvalues (). Addpropertyvalue ("Name", name);            Parsercontext.getregistry (). Registerbeandefinition (ID, beandefinition); returnbeandefinition; }  }  

Fourth Step:

Import Org.springframework.beans.factory.xml.NamespaceHandlerSupport;    Public class extends Namespacehandlersupport {      publicvoid  init () {          Registerbeandefinitionparser (new  testcustombeandefinitionparser ());      }  }  

Fifth Step:

Spring.handlers:
Http\://code.alibabatech.com/schema/dubbo=com.alibaba.dubbo.config.spring.schema.dubbonamespacehandler
Spring.schemas:
Http\://code.alibabatech.com/schema/dubbo/dubbo.xsd=meta-inf/dubbo.xsd

Sixth step:

<?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:dubbo= "Http://code.alibabatech.com/schema/dubbo"xsi:schemalocation= "http://Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp//Code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd"><dubbo:custom id= "Testcustom" Name= "This is a test custom tag"/> </beans>ImportOrg.springframework.context.ApplicationContext; ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;  Public classMain { Public Static voidMain (string[] args) {String XML= "Classpath:test.xml"; ApplicationContext Context=NewClasspathxmlapplicationcontext (Newstring[] {XML}); System.out.println (Context.getbean ("Testcustom")); }} The above example output is: Testbean {name=this is a test custom tag}

Spring's 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.