Custom xsd file operation in Spring

1 set the syntax format of html file xsd file

Write xsd file according to the properties of POJO.

for example

<?xml version="1.0" encoding="UTF-8" ?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.zhangyh.com/schema/user"
        elementFormDefault="qualified">

    <element name="user">
        <complexType>
            <attribute name="id" type="string" />
            <attribute name="name" type="string" />
            <attribute name="email" type="string" />
        </complexType>
    </element>
</schema>

The corresponding POJO is

public class User {
    private String name;
    private String email;
		
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}

Property's getter and setter methods are indispensable

The User's properties can be configured in xml, such as

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mytest="http://www.zhangyh.com/schema/user"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            classpath:com.zhangyh.xsd/schema/spring-beans-2.0.xsd
            http://www.zhangyh.com/schema/user
            http://www.zhangyh.com/schema/user.xsd">

    <mytest:user id="testBean"  name="aaa" email="bbb"/>

</beans>

mytest: is a custom namespace name classpath: is the local xsd file loading protocol

2 implement BeanDefinitionParser interface and extension class NamespaceHandlerSupport

Extend NamespaceHandlerSupport class: implement manual loading of data in XML into BeanDefinitionBuilder

public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {

    protected Class getBeanClass(Element element){
        return User.class;
    }
    protected void doParse(Element element, BeanDefinitionBuilder bean) {
        String userName = element.getAttribute("name");
        String email = element.getAttribute("email");

        if(StringUtils.hasText(userName)) {
            bean.addPropertyValue("name", userName);
        }

        if (StringUtils.hasText(email)) {
            bean.addPropertyValue("email", email);
        }
    }
}

Implement BeanDefinitionParser interface: manually register POJO's manual resolution results

public class MyNamespaceHandler extends NamespaceHandlerSupport {
    public void init() {
        registerBeanDefinitionParser("user",new UserBeanDefinitionParser());
    }
}

3 set the Spring.handlers and Spring.schemas files

To configure META-INF/Spring.handlers:

http\://www.zhangyh.com/schema/user=com.zhangyh.xsd.paser.MyNamespaceHandler

When quoted When http://www.zhangyh.com/schema/user uses MyNamespaceHandler to resolve the reference object

Configure META-INF/Spring.schemas

http\://www.zhangyh.com/schema/user.xsd=com.zhangyh.xsd/schema/user.xsd

Load custom xsd file

Access XML file of custom xsd configuration through ClassPathXmlApplicationContext

This article is based on the platform of blog one article multiple sending OpenWrite Release!

Keywords: Programming Spring xml Attribute encoding

Added by pleigh on Wed, 16 Oct 2019 17:33:00 +0300