Spring-based extended schema configuration

Source: Internet
Author: User
Brief Introduction Why custom schemas are required.

Most of the time we define configurations in the process of using spring, the simplest example of defining < beans, using < AOP or < MVC tags, using Dubbo's small partners may also use a lot of < dubbo:service configurations, Often such files require developers to configure the required content in a specific format, with specific tags, and the file structure has many benefits: it is easier to describe the content of the allowed document to more easily verify the correctness of the data to more easily define data constraints (facets) More easily define a data model (or data format) to more easily convert data between different data types you can use an XML editor to edit the schema file to parse the schema file with an XML parser to make it easier to protect data traffic ... How to customize the schema.

(1) Simple practices can be configured directly on the basis of spring's standard beans, but when the configuration is more complex or requires more rich control, it becomes awkward.

(2) using the original ecological way to parse the well-defined XML file, and then into the configuration object, this way can solve all problems, but it is cumbersome to implement, especially when the configuration is very complex, parsing work is a burden to be considered.

(3) Spring provides support for extensible schemas, which is a good compromise, and completing a custom configuration typically requires the following steps: Designing Configuration Properties and JavaBean writing XSD files Namespacehandler and Beandefinition Parser writing spring.handlers and Spring.schemas all parts in series apply the spring-based schema extension case in the Bean file

(1) write the bean to parse:

public class Providerparameter implements Serializable {
    private String InterfaceName;
    Private String host;
    Private Integer port;
    Public String Getinterfacename () {
        return interfacename;
    }
    public void Setinterfacename (String InterfaceName) {
        this.interfacename = InterfaceName;
    }
    Public String GetHost () {
        return host;
    }
    public void Sethost (String host) {
        this.host = host;
    }
    Public Integer Getport () {
        return port;
    }
    public void Setport (Integer port) {
        this.port = port;
    }
}

(2) write the XSD file provider.xsd and place the file under Meta-inf

<?xml version= "1.0" encoding= "UTF-8"?> <xsd:schema xmlns= "http://syn.test.com/ Schema/provider "xmlns:xsd=" Http://www.w3.org/2001/XMLSchema "xmlns:beans=" Http://www.springframework.org/schema /beans "targetnamespace=" Http://syn.test.com/schema/provider "> <xsd:import namespace=" Http://www.springfram
            Ework.org/schema/beans "/> <xsd:element name=" Providerparameter "> <xsd:complexType> <xsd:complexContent> <xsd:extension base= "Beans:identifiedtype" > <xsd:  Attribute name= "InterfaceName" type= "xsd:string"/> <xsd:attribute name= "host" type= "Xsd:string"
            /> <xsd:attribute name= "port" type= "Xsd:int"/> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:element> </xsd:schema> 

(3) Writing Namespacehandler and Beandefinitionparser
Namespacehandler:

public class Providernamespacehandler extends Namespacehandlersupport {

    @Override public
    void init () {
        // TODO auto-generated Method Stub
         registerbeandefinitionparser ("Providerparameter", new Providerbeandefinitionparser ());  
    }

}

Beandefinitionparser:

public class Providerbeandefinitionparser extends Abstractsinglebeandefinitionparser {

    protected class Getbeanclass (element Element) {  
        return providerparameter.class;  
    }  
    protected void Doparse (element element, Beandefinitionbuilder Bean) {  
        String InterfaceName = Element.getattribute (" InterfaceName ");  
        String host = Element.getattribute ("host");  
        String port = element.getattribute ("port");  
        if (Stringutils.hastext (port)) {  
            Bean.addpropertyvalue ("Port", Integer.parseint (port));  
        }  
        if (Stringutils.hastext (InterfaceName)) {  
            bean.addpropertyvalue ("InterfaceName", InterfaceName);  
        }  
        if (Stringutils.hastext (host)) {  
            Bean.addpropertyvalue ("host", Host);}}} 

(4) Write spring.handlers and Spring.schemas all parts in series, put these two files under Meta-inf

Spring.handlers, used to parse the XSD file:

Http://syn.test.com/schema/provider=basic.schema.ProviderNameSpaceHandler

Spring.schemas:

Http://syn.test.com/schema/provider/provider.xsd=META-INF/provider.xsd

(5) Writing spring-based test cases
Spring Configuration:

It should be noted that Xml:provider is used to specify a custom schema, which defines the tags that can be used to start with < provider in this context.
Use Case Authoring:

Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import org.springframework.test.context.ContextConfiguration;
Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith (Springjunit4classrunner.class)
@ContextConfiguration (locations = {
        "classpath:config/*.xml")
})
public class TestCase {

    @Test public
    void Test () {
        ApplicationContext ctx = new Classpathxmlapplicationcontext ("Config/spring-test.xml");  
        Providerparameter p = (providerparameter) Ctx.getbean ("provider");  
        System.out.println ("=====" + P);
    }
}

The above is a simple spring-based schema extension. The overall process can run through, but because there is no specific schema to learn about the schema of some of the configuration of the writing still have a lot of questions, first left two main records down:

(1) The difference between xmlns and targetnamespace
(2) What kind of rules does spring follow to find the schema, which is now the Xmlns,targetnamespace, As well as the spring configuration file using the same link can be accessed successfully, if later need to change what should be done.

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.