美文网首页
自定义spring.schemas和spring.handlse

自定义spring.schemas和spring.handlse

作者: 长孙俊明 | 来源:发表于2019-11-02 17:46 被阅读0次

工程结构

image.png

代码

package com.myself.config;

import java.io.Serializable;

public class Userconfig implements Serializable {
    private static final long serialVersionUID = -5680714598279222721L;

    private String id;

    private String name;

    private String password;

    private String phone;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("name=" + name).append(" password=" + password).append(" phone=" + phone);
        return sb.toString();

    }
}

package com.myself.config;

import org.springframework.beans.factory.xml.NamespaceHandlerSupport;

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

package com.myself.config;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.w3c.dom.Element;

public class UserParserDefinitons extends AbstractSingleBeanDefinitionParser {
    @Override
    protected Class<?> getBeanClass(Element element) {
        return Userconfig.class;
    }

    @Override
    protected void doParse(Element element, BeanDefinitionBuilder builder) {
        String name = element.getAttribute("name");

        String password = element.getAttribute("password");

        String phone = element.getAttribute("phone");
        builder.addPropertyValue("name",name);
        builder.addPropertyValue("password",password);
        builder.addPropertyValue("phone",phone);
    }


}

配置

loup.xsd
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns="http://code.loup.com/schema/loup"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://code.loup.com/schema/loup"
            elementFormDefault="qualified">

    <xsd:import namespace="http://www.w3.org/XML/1998/namespace"></xsd:import>

    <xsd:element name="user">
        <xsd:complexType>
            <xsd:attribute name="id" type="xsd:string"></xsd:attribute>
            <xsd:attribute name="name" type="xsd:string"></xsd:attribute>
            <xsd:attribute name="password" type="xsd:string"></xsd:attribute>
            <xsd:attribute name="phone" type="xsd:string"></xsd:attribute>
        </xsd:complexType>
    </xsd:element>


</xsd:schema>
spring.handlers
http\://code.loup.com/schema/loup=com.myself.config.UserHandlers
spring.schemas
http\://code.loup.com/schema/loup/loup.xsd=META-INF/loup.xsd
mytest.xml
<?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:ssd="http://code.loup.com/schema/loup"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.loup.com/schema/loup
       http://code.loup.com/schema/loup/loup.xsd">

    <ssd:user id="testLoup" name="loup" password="123456" phone="111111"></ssd:user>
</beans>

测试

package com.myself.config;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("mytest.xml");
        Userconfig userconfig = (Userconfig) context.getBean("testLoup");
        System.out.println(userconfig);
    }

}

相关文章

网友评论

      本文标题:自定义spring.schemas和spring.handlse

      本文链接:https://www.haomeiwen.com/subject/pdlhbctx.html