【转载请注明作者和原文链接,欢迎讨论,相互学习。】
一、前言
1. 最近在学习dubbo,里边很多如provider、consumer、registry的配置都是通过spring自定义Schema来实现的,为此,我也学习下如何自定义Schema。
2.学习目标
完成自定义一个shema名称为test,节点名为user的例子。
二、准备内容
1.编写java bean
2.编写xsd配置文件
3.编写spring.handlers和spring.schmas
4.编写applicationContext.xml
5.编写NamespaceHandler和BeanDefinitionParser
三、晒代码
1.工程目录结构
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例子完成。