spring schema 扩充
spring schema 扩展
spring 可以基于schema 扩展,自定义 schema。参考文档自己搭了个应用试验了一下:
首先看下自己写的 myns.xsd
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns="http://www.yjhexy.com/schema/myns" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" targetNamespace="http://www.yjhexy.com/schema/myns" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xsd:import namespace="http://www.springframework.org/schema/beans" /> <xsd:element name="dateformat"> <xsd:complexType> <xsd:complexContent> <xsd:extension base="beans:identifiedType"> <xsd:attribute name="lenient" type="xsd:boolean" /> <xsd:attribute name="pattern" type="xsd:string" use="required" /> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:element> </xsd:schema>
然后看下我的applicationContxt.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:myns="http://www.yjhexy.com/schema/myns" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.yjhexy.com/schema/myns http://www.yjhexy.com/schema/myns/myns.xsd "> <myns:dateformat id="dateFormat" pattern="yyyy-MM-dd HH:mm" lenient="true" /> </beans>
很明显实现了个自定义的bean ,这个bean有两个属性,一个是时间的格式,另外一个不知道啥东西。
然后在META-INF下面写了两个文件,
spring.handlers:用来描述如何处理自定义的namespace
http\://www.yjhexy.com/schema/myns=com.yajun.balance.spring.MyNamespaceHandler
spring.schemas:描述schema的位置
http\://www.yjhexy.com/schema/myns/myns.xsd=com/yajun/balance/spring/myns.xsd
然后是需要两个类,一个处理namespace,一个处理beanDefinition
package com.yajun.balance.spring; import org.springframework.beans.factory.xml.NamespaceHandlerSupport; public class MyNamespaceHandler extends NamespaceHandlerSupport { public void init() { registerBeanDefinitionParser("dateformat", new SimpleDateFormatBeanDefinitionParser()); } }
package com.yajun.balance.spring; import java.text.SimpleDateFormat; 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 SimpleDateFormatBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { protected Class getBeanClass(Element element) { return SimpleDateFormat.class; } protected void doParse(Element element, BeanDefinitionBuilder bean) { // this will never be null since the schema explicitly requires that a value be supplied String pattern = element.getAttribute("pattern"); bean.addConstructorArgValue(pattern); // this however is an optional property String lenient = element.getAttribute("lenient"); if (StringUtils.hasText(lenient)) { bean.addPropertyValue("lenient", Boolean.valueOf(lenient)); } } }
最后main输出试试:
package com.yajun.balance.spring; import java.text.SimpleDateFormat; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); SimpleDateFormat f = (SimpleDateFormat) ctx.getBean("dateFormat"); System.out.println(f); } }
===========================
xsd 基础 常用的简单 类型
- xs:string
- xs:decimal
- xs:integer
- xs:boolean
- xs:date
- xs:time
- xs:positiveInteger
1 楼
sunny3super
2012-07-16
好文章,另外 http://www.3822.net/topicshow/5802 这里也提供了一个类似的例子,并且提供源代码下载。