Spring 自定义xsd

本文介绍了如何在Spring框架中实现自定义xsd文件,包括项目结构、配置文件如application.xml和people.xsd,以及解析器PeopleBeanDefinitionParser、命名空间处理器StudentNamespaceHandler的实现,最后通过Main.java启动应用进行演示。
摘要由CSDN通过智能技术生成

项目结构图

代码

application.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:people="http://www.laiwenqiang.com/spring/schema/people"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.laiwenqiang.com/spring/schema/people http://www.laiwenqiang.com/spring/schema/people.xsd" >

    <people:student id="student1" name="student1"
                    age="18" />


</beans>

people.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.laiwenqiang.com/spring/schema/people"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:beans="http://www.springframework.org/schema/beans"
            targetNamespace="http://www.laiwenqiang.com/spring/schema/people"
            elementFormDefault="qualified"
            attributeFormDefault="unqualified">
    <xsd:import namespace="http://www.springframework.org/schema/beans" />

    <xsd:element name="student">
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:extension base="beans:identifiedType">

                    <xsd:attribute name="name"
                                   type="xsd:string">
                        <xsd:annotation>
                            <xsd:documentation>
                                姓名
                            </xsd:documentation>
                        </xsd:annotation>
                    </xsd:attribute>

                    <xsd:attribute name="age"
                                   type="xsd:string">
                        <xsd:annotation>
                            <xsd:documentation>
                                年龄
                            </xsd:documentation>
                        </xsd:annotation>
                    </xsd:attribute>

                </xsd:extension>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

spring.handlers

http\://www.laiwenqiang.com/spring/schema/people=com.laiwenqiang.spring.sechema.StudentNamespaceHandler

spring.schemas

http\://www.laiwenqiang.com/spring/schema/people.xsd=META-INF/people.xsd

PeopleBeanDefinitionParser.java

package com.laiwenqiang.spring.sechema;

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


/**
 * Created by laiwenqiang on 2017/1/29.
 */
public class PeopleBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
    protected Class getBeanClass(Element element) {
        return Student.class;
    }
    protected void doParse(Element element, BeanDefinitionBuilder bean) {
        String name = element.getAttribute("name");
        String age = element.getAttribute("age");
        String id = element.getAttribute("id");
        if (StringUtils.hasText(id)) {
            bean.addPropertyValue("id", id);
        }
        if (StringUtils.hasText(name)) {
            bean.addPropertyValue("name", name);
        }
        if (StringUtils.hasText(age)) {
            bean.addPropertyValue("age", Integer.valueOf(age));
        }
    }

}

student.java

package com.laiwenqiang.spring.sechema;

/**
 * Created by laiwenqiang on 2017/1/29.
 */
public class Student {
    private String id;
    private String name;
    private String age;

    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 getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

StudentNamespaceHandler.java

package com.laiwenqiang.spring.sechema;

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

/**
 * Created by laiwenqiang on 2017/1/29.
 */
public class StudentNamespaceHandler extends NamespaceHandlerSupport {
    public void init() {
        registerBeanDefinitionParser("student", new PeopleBeanDefinitionParser());
    }
}

Main.java

package com.laiwenqiang.spring.sechema;

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

/**
 * Created by laiwenqiang on 2017/1/29.
 */
public class Main {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml");
        Student people = (Student) ctx.getBean("student1");
        System.out.println(people.getId());
        System.out.println(people.getName());
        System.out.println(people.getAge());

    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值