在写程序框架的过程中,经常为了简化程序,可能需要自定义schema, SPRING 对于JAVA开发者来说,是很好的工具,因此基于SPRING schema 的扩展对框架有很大用处。
比如做成自己的schema 之后,就可以在 xml 配置文件中类似如下的定义: <comtest:people id="cutesource" name="一号门" age="1024"/> 这里的comtest 就是自定义的schema.
曾经在优快云 搜到一篇文章,按照他的方式,我确实没做出来,可能比较笨的原因。后来自己参考他的方法,按自己的思路做出来了。
现在贴出来,供大家参考。
建立两个工程
1.springcomponent (用来实现spring schema 的扩展)
2.sprintschema (用来测试spring schema 的扩展,比须将 springcomponent 打成jar包,放到此工程中)
两个工程的基本结构如图,看上去有错,那是因为 spring schema xsd 校验引起的,运行并没有关系。
其关键在于,springcomponent 工程中 schema 的扩展。
package com.springwork.xml; import org.springframework.beans.factory.xml.NamespaceHandlerSupport; public class MyNamespaceHandler extends NamespaceHandlerSupport { public void init() { registerBeanDefinitionParser("people", new PeopleBeanDefinitionParser()); } }
package com.springwork.xml; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.springframework.util.StringUtils; import org.w3c.dom.Element; public class PeopleBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { protected Class getBeanClass(Element element) { return People.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)); } } }
package com.springwork.xml; public class People { private String name; private int age; private String id; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getId() { return id; } public void setId(String id) { this.id = id; } }
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns="http://www.yihaomen.com/schema/people" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" targetNamespace="http://www.yihaomen.com/schema/people" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xsd:import namespace="http://www.springframework.org/schema/beans" /> <xsd:element name="people"> <xsd:complexType> <xsd:complexContent> <xsd:extension base="beans:identifiedType"> <xsd:attribute name="name" type="xsd:string" /> <xsd:attribute name="age" type="xsd:int" /> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:element> </xsd:schema>
测试代码: 你就可以看到结果了。
package com.springwork.xml; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/config/application.xml"); People p = (People)ctx.getBean("cutesource"); System.out.println(p.getId()); System.out.println(p.getName()); System.out.println(p.getAge()); } }
附上原代码,打包,去掉了SPRING JAR 包,大家自己加进去。太大了。测试工程里面,还加入了一个servlet的测试。