自定义spring.schemas和spring.handlsers

摘自:https://blog.csdn.net/ll840768874/article/details/77369204

最近看spring源码,看到了Spring提供了可扩展的schema的支持。

1.设计配置属性javabean

2.编写xsd文件

3.编写NamespaceHandlerSupport和BeanDefinitionParser

4.在spring.schemas和spring.handlers中填入关系,使之能关联起来

1.编写JavaBean:


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();
    }

2.编写xsd文件放在META-INF文件下。PS:maven项目如果没有META-INF文件,需要自己在resources文件夹下创建META-INF文件

<?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>

扫描二维码关注公众号,回复: 6216020 查看本文章

3. 编写NamespaceHandlerSupport,写一个类继承NamespaceHandlerSupport
public class UserHandlers extends NamespaceHandlerSupport {
 
    @Override
    public void init() {
        registerBeanDefinitionParser("user", new UserParserDefinitons());
    }
}
然后编写BeanDefinitionParser,写一个解析类,继承AbstractBeanDefinitionParser,重写getBeanClass方法和doParse方法,getBeanClass返回的是之前编写的Javabean
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);
    }
 
}

4.新建spring.schemas,注意此文件放在META-INF文件下,spring加载的时候会读取,里面填入
http\://code.loup.com/schema/loup/loup.xsd=META-INF/loup.xsd
然后新建spring.handlsers,此文件也是放在META-INF文件下,里面填写之前编写的xsd的Namespace的路劲和第三步编写的handlers对应关系

http\://code.loup.com/schema/loup=com.seckill.handlers.UserHandlers

至此简单的自定义扩展已经结束

在spring的文件中引用

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:loup="http://code.loup.com/schema/loup"
       xsi:schemaLocation="http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        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">
 
    <loup:user id="testLoup" name="loup" password="123456" phone="111111"></loup:user>
</beans>


写个测试方法

public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("conf/spring-test.xml");
        Userconfig userconfig = (Userconfig) context.getBean("testLoup");
        System.out.println(userconfig);
    }
输出的内容:

猜你喜欢

转载自blog.csdn.net/dhq_blog/article/details/85777796