spring自定义schema学习
【转载请注明作者和原文链接,欢迎讨论,相互学习。】
一、前言
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 package com.cnblogs.ericlin.spring; 2 3 public class User { 4 private String id; 5 private String name; 6 private String sex; 7 private int age; 8 9 public String getId() { 10 return id; 11 } 12 13 public void setId(String id) { 14 this.id = id; 15 } 16 17 public String getName() { 18 return name; 19 } 20 21 public void setName(String name) { 22 this.name = name; 23 } 24 25 public String getSex() { 26 return sex; 27 } 28 29 public void setSex(String sex) { 30 this.sex = sex; 31 } 32 33 public int getAge() { 34 return age; 35 } 36 37 public void setAge(int age) { 38 this.age = age; 39 } 40 }
3.xsd文件
1 <?xml version="1.0" encoding="UTF-8" standalone="no" ?> 2 <xsd:schema xmlns="http://www.cnblogs.com/eric-lin/schema/user" 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 <xsd:import namespace="http://www.springframework.org/schema/beans" /> 10 <xsd:import namespace="http://www.springframework.org/schema/tool" /> 11 12 <xsd:element name="user"> 13 <xsd:complexType> 14 <xsd:complexContent> 15 <xsd:extension base="beans:identifiedType"> 16 <xsd:attribute name="name" type="xsd:string"> 17 <xsd:annotation> 18 <xsd:documentation>姓名</xsd:documentation> 19 </xsd:annotation> 20 </xsd:attribute> 21 22 <xsd:attribute name="sex" type="xsd:string"> 23 <xsd:annotation> 24 <xsd:documentation>性别</xsd:documentation> 25 </xsd:annotation> 26 </xsd:attribute> 27 28 <xsd:attribute name="age" type="xsd:int"> 29 <xsd:annotation> 30 <xsd:documentation>年龄</xsd:documentation> 31 </xsd:annotation> 32 </xsd:attribute> 33 </xsd:extension> 34 </xsd:complexContent> 35 </xsd:complexType> 36 </xsd:element> 37 </xsd:schema>
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
<?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:test="http://www.cnblogs.com/eric-lin/schema/user" xsi:schemaLocation="http://www.springframework.org/schema/beans 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"> <test:user id="eric" name="123" sex="male" age="28" /> </beans>
7.NamespaceHandler
package com.cnblogs.ericlin.spring.schema; import org.springframework.beans.factory.xml.NamespaceHandlerSupport; public class UserNamespaceHandler extends NamespaceHandlerSupport { public void init() { registerBeanDefinitionParser("user", new UserBeanDefinitionParser()); } }
8.BeanDefinitionParser
package com.cnblogs.ericlin.spring.schema; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.w3c.dom.Element; import com.cnblogs.ericlin.spring.User; public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { @Override protected Class<?> getBeanClass(Element element) { return User.class; } @Override protected void doParse(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测试类
package com.cnglogs.ericlin.spring; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.cnblogs.ericlin.spring.User; public class TestSpringSchema { @SuppressWarnings("resource") @Test public void test() { 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例子完成。