您的位置:首页 > 编程语言 > Java开发

自定义spirng schema

2015-07-18 13:54 615 查看
spring已经为我们提供了很丰富的xml元素,但是有时候我们需要自己扩展spring schema来定制标签,spring已经为我们提供了扩展的api,使用起来非常方便。本片文章来自于spring官网的教程。

如果要定义自己的schema,通常有如下几步:

1、自定义xml schema,在这里是编写自己的xsd文件,xsd文件可以理解为描述xml文件的元文件,定义了一个xml文件中可以有哪些元素等等一些规则。

2、编写自己的NamespaceHandler。

3、编写自定义BeanDefinationParser类,完成对xml元素的解析工作。

4、注册上面的这些东西,也就是按照spring的规则将上面的功能整合。

这里定义一个SimpleDateFormat类的专用标签,该标签根据xml的配置元素初始化生成一个SimpleDateFormat类。完成后配置大概如下:

<myns:dateformat id="dateFormat"
pattern="yyyy-MM-dd HH:mm"
lenient="true"/>可以像使用spring中其他的bean一样使用dateformat。
xsd文件定义如下:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.mycompany.com/schema/myns"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://www.mycompany.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上面的文件定义了一个xsd文件,由于要使用spring的identifiedType,因此头上import了benas的命令空间。dateformat是一个符合类型,有两个基本的属性lenient和pattern其中的pattern是必选的,相信熟悉soap的同学对上面的文件并不陌生。
package org.springframework.samples.xml;

import org.springframework.beans.factory.xml.NamespaceHandlerSupport;

public class MyNamespaceHandler extends NamespaceHandlerSupport {

public void init() {
registerBeanDefinitionParser("dateformat", new SimpleDateFormatBeanDefinitionParser());
}

}上面的类定义了我们自己的Handler,可见该类非常简单,主要做了一个映射的工作,当遇到dateformat标签的时候,将会使用SimpleDateFormatBeanDefinitionParser类进行解析。
parser类如下:

package org.springframework.samples.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;

import java.text.SimpleDateFormat;

public class SimpleDateFormatBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { 1

protected Class getBeanClass(Element element) {
return SimpleDateFormat.class; 2
}

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.addConstructorArg(pattern);

// this however is an optional property
String lenient = element.getAttribute("lenient");
if (StringUtils.hasText(lenient)) {
bean.addPropertyValue("lenient", Boolean.valueOf(lenient));
}
}

}parser类也很简单,getBeanClass返回SimpleDateFormat.class对象,也就是当遇到dateformat标签的时候spring为我们构造一个SimpleDateFormat类,其中类的具体属性由doParse方法完成,逻辑同样不难理解。
接下来注册handler和schema,在META-INF目录下新建spring.handlers,内容如下:

<pre class="literallayout" style="line-height: 1; background-color: rgb(255, 255, 255);"><pre name="code" class="html">http\://www.mycompany.com/schema/myns=org.springframework.samples.xml.MyNamespaceHandler


需要注意的是冒号前面需要加转义符,接下来定义自己的命名空间,指定该命名空间的Handler类

然后定义spring.schema文件,内容如下:

http\://www.mycompany.com/schema/myns/myns.xsd=org/springframework/samples/xml/myns.xsd该文件指定前面xsd的locat路径,不指定的话spring会联网获取xsd文件。
以上就是自定义schema的步骤,可见还是挺简单的,下面就用一个小例子来应用起来。

<?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.mycompany.com/schema/myns"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.mycompany.com/schema/myns http://www.mycompany.com/schema/myns/myns.xsd">

<!-- as a top-level bean -->
<myns:dateformat id="defaultDateFormat" pattern="yyyy-MM-dd HH:mm" lenient="true"/>

</beans>上面的文件定义了一个id为defaultDateFormat的bean,可以像应用spring自身的bean一样应用该bean。
整个的项目目录如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring xml schema 扩展 xsd