java 自定义xsd_spring自定义schema学习

【转载请注明作者和原文链接,欢迎讨论,相互学习。】

一、前言

1. 最近在学习dubbo,里边很多如provider、consumer、registry的配置都是通过spring自定义Schema来实现的,为此,我也学习下如何自定义Schema。

11c29c9555ac0a26cc98f41f1422f127.png

2.学习目标

完成自定义一个shema名称为test,节点名为user的例子。

二、准备内容

1.编写java bean

2.编写xsd配置文件

3.编写spring.handlers和spring.schmas

4.编写applicationContext.xml

5.编写NamespaceHandler和BeanDefinitionParser

三、晒代码

1.工程目录结构

6e312f04a132589069f406fb4323be29.png

2.java bean

1 packagecom.cnblogs.ericlin.spring;2

3 public classUser {4 privateString id;5 privateString name;6 privateString sex;7 private intage;8

9 publicString getId() {10 returnid;11 }12

13 public voidsetId(String id) {14 this.id =id;15 }16

17 publicString getName() {18 returnname;19 }20

21 public voidsetName(String name) {22 this.name =name;23 }24

25 publicString getSex() {26 returnsex;27 }28

29 public voidsetSex(String sex) {30 this.sex =sex;31 }32

33 public intgetAge() {34 returnage;35 }36

37 public void setAge(intage) {38 this.age =age;39 }40 }

3.xsd文件

1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>

2

3 xmlns:xsd="http://www.w3.org/2001/XMLSchema"

4 xmlns:beans="http://www.springframework.org/schema/beans"

5 xmlns:tool="http://www.springframework.org/schema/tool"

6 targetNamespace="http://www.cnblogs.com/eric-lin/schema/user"

7 elementFormDefault="qualified"attributeFormDefault="unqualified">

8

9

10

11

12

13

14

15

16

17

18 姓名

19

20

21

22

23

24 性别

25

26

27

28

29

30 年龄

31

32

33

34

35

36

37

4.spring.handlers文件内容

http\://www.cnblogs.com/eric-lin/schema/user=com.cnblogs.ericlin.spring.schema.UserNamespaceHandler

5.spring.schemas文件内容

http\://www.cnblogs.com/eric-lin/schema/user/user.xsd=META-INF/user.xsd

6.applicationContext.xml

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.cnblogs.com/eric-lin/schema/user

http://www.cnblogs.com/eric-lin/schema/user/user.xsd">

7.NamespaceHandler

packagecom.cnblogs.ericlin.spring.schema;importorg.springframework.beans.factory.xml.NamespaceHandlerSupport;public class UserNamespaceHandler extendsNamespaceHandlerSupport {public voidinit() {

registerBeanDefinitionParser("user", newUserBeanDefinitionParser());

}

}

8.BeanDefinitionParser

packagecom.cnblogs.ericlin.spring.schema;importorg.springframework.beans.factory.support.BeanDefinitionBuilder;importorg.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;importorg.w3c.dom.Element;importcom.cnblogs.ericlin.spring.User;public class UserBeanDefinitionParser extendsAbstractSingleBeanDefinitionParser {

@Overrideprotected Class>getBeanClass(Element element) {return User.class;

}

@Overrideprotected voiddoParse(Element element, BeanDefinitionBuilder bean) {

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

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

String sex= element.getAttribute("sex");int age = Integer.parseInt(element.getAttribute("age"));

bean.addPropertyValue("id", id);

bean.addPropertyValue("name", name);

bean.addPropertyValue("sex", sex);

bean.addPropertyValue("age", age);

}

}

9测试类

packagecom.cnglogs.ericlin.spring;importorg.junit.Test;importorg.springframework.context.support.ClassPathXmlApplicationContext;importcom.cnblogs.ericlin.spring.User;public classTestSpringSchema {

@SuppressWarnings("resource")

@Testpublic voidtest() {

ClassPathXmlApplicationContext context= new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

User user= (User) context.getBean("eric");

System.out.println(user.getId());

System.out.println(user.getName());

System.out.println(user.getSex());

System.out.println(user.getAge());

}

}

自此,自定义spring shema例子完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值