---
title: "基于xml schema的扩展标签"
author: "dylan0514sina.cn"
date: "2013-07-09T13:34:50Z"
tags: []
categories: ["开源软件"]
slug: "xml-schema"
draft: false
lastmod: "2013-07-09T13:34:50Z"
id: 1901872
url: "https://tmmh.iteye.com/blog/1901872"
---

xml schema是spring 2.0版本之后引入的，在之后的2.5和3.x加入了新的元素。引入的主要动机在于：虽说spring把中一切皆为对象，但在开发人员的角度上讲，我们要在Spring中具体化或抽象化一些东西，比如具体化单值、集合；或特定于具体应用的抽象比如AOP，事务。那不得不在spring中配置一些基础设施bean。或第三方框架支持我们都使用过spring Security框架，说实在的如果不使用security标签，我们必须为每个过滤器有一个定义。所以我们为了方便不得不去自定义标签，xml schema将适用。 引入schema dtd配置 \[code="xml"] \[/code] schema配置 \[code="xml"] \[/code] 以上配置复制粘贴就行，其中schemaLocation不是必须的，但在开发可以通过他在jar中找到spring-beans.xsd。接下来看下spring中提供的自定义标签. \[size=large]util schema \[/size] 主要处理集合，对象或类域。使用之前，配置schema头 \[code="xml"] \[/code] \[size=medium]\[/size] 使用之前 \[code="xml"] \[/code] 比较复杂的构造基本通过FactoryBean实现，FieldRetrievingFactoryBean检索类或对象的字段。 基于schema的配置 \[code="xml"] \[/code] \[size=medium]\[/size] 使用之前 \[code="xml"] \[/code] 使用FactoryBean实现，PropertyPathFactoryBean创建一个ID为“testBean.age”的Bean,整型10。 基于schema配置 \[code="xml"] \[/code] \[size=medium]\[/size] 使用之前 \[code="xml"] \[/code] PropertiesFactoryBean实例化java.util.Properties，载入location指定Resource资源。 基于schema设置 \[code="xml"] \[/code] \[size=medium]\[/size] 使用之前 \[code="xml"] pechorin@hero.org raskolnikov@slums.org stavrogin@gov.org porfiry@gov.org \[/code] ListFactoryBean创建了List集合 基于schema配置 \[code="xml"] pechorin@hero.org raskolnikov@slums.org stavrogin@gov.org porfiry@gov.org \[/code] \[size=medium]\[/size] 使用之前 \[code="xml"] \[/code] MapFactoryBean创建了一个Map 基于schema配置 \[code="xml"] \[/code] \[size=medium]\[/size] 使用之前 \[code="xml"] pechorin@hero.org raskolnikov@slums.org stavrogin@gov.org porfiry@gov.org \[/code] SetFactoryBean创建了 java.util.Set实例 基于schema配置 \[code="xml"] pechorin@hero.org raskolnikov@slums.org stavrogin@gov.org porfiry@gov.org \[/code] \[size=large]jee schema\[/size] jee 标签主要查找JNDI Object比如 数据源，EJB引用。 \[code="xml"] \[/code] \[size=medium] 简单\[/size] 使用之前 \[code="xml"] \[/code] \[size=medium] 单环境设置\[/size] 使用之前 \[code="xml"] bar \[/code] 基于schema配置 \[code="xml"] foo=bar \[/code] \[size=medium] 多环境设置\[/size] 使用之前 \[code="xml"] bar pong \[/code] 基于schema配置 \[code="xml"] foo=bar ping=pong \[/code] \[size=medium] 复杂设置\[/size] 使用之前 \[code="xml"] \[/code] 基于schema配置 \[code="xml"] \[/code] \[size=medium] 简单\[/size] 配置一个本地EJB无状态会话BEAN 使用之前 \[code="xml"] \[/code] 基于schema配置 \[code="xml"] \[/code] \[size=medium]复杂配置\[/size] 使用之前 \[code="xml"] \[/code] 基于schema配置 \[code="xml"] \[/code] \[size=medium]\[/size] 配置一个远程EJB无状态会话BEAN 使用之前 \[code="xml"] \[/code] 基于schema配置 \[code="xml"] \[/code] \[size=large]lang schema\[/size] lang标签用来暴露用脚本语言写的bean,比如Jruby,Groovy,Beanshell \[code="xml"] \[/code] \[size=large]jms schema\[/size] 处理JMS相关的BEAN \[code="xml"] \[/code] \[size=large]tx (transaction) schema\[/size] 事务处理 \[code="xml"] \[/code] \[size=large]aop schema\[/size] \[code="xml"] \[/code] \[size=large]context schema\[/size] spring 2.5之后才有 \[code="xml"] \[/code] \[size=medium] \[/size] 根据指定资源文件(spring resource)激活${},用 PropertyPlaceholderConfigurer实现 \[size=medium]\[/size] 激活侦测配置bean上的注解 @Required ,@Autowired,@PreDestroy , @Resource , @PersistenceContext,@PersistenceUnit \[size=medium]\[/size] 自动侦测类，注解 \[size=medium]\[/size] 使用AspectJ \[size=medium]\[/size] 使用AspectJ用在领域对象 \[size=medium]\[/size]导出Mbean \[size=large]jdbc schema\[/size] 配置内嵌数据库和初始化内嵌数据库 \[code="xml"] \[/code] \[size=medium]内嵌数据库\[/size]支持 HSQL, H2, Derby \[code="xml"] \[/code] \[size=medium]\[/size] 初始化内嵌数据库 \[code="xml"] \[/code] \[size=large]自定义扩展标签\[/size] \[list=1] \[\*]编写schema \[\*]实现解析标签的NamespaceHandler \[\*]实现BeanDefinitionParser \[\*]钩入以上实现配置到Spring中 \[/list] 实现bean定义如下 \[code="xml"] \[/code] \[size=medium]编写schema\[/size] \[code="xml"] \[/code] \[size=medium]2.编写NamespaceHandler\[/size] NamespaceHandler 接口有三个方法 init():第一次被使用时调用，注册BeanDefinitionParser BeanDefinition parse(Element, ParserContext):作为顶级元素时被调用 BeanDefinitionHolder decorate(Node, BeanDefinitionHolder, ParserContext) :作为内嵌bean或不同namespace时被调用 \[code="java"] package org.springframework.samples.xml; import org.springframework.beans.factory.xml.NamespaceHandlerSupport; public class MyNamespaceHandler extends NamespaceHandlerSupport { public void init() { registerBeanDefinitionParser("dateformat", new SimpleDateFormatBeanDefinitionParser()); } } \[/code] \[size=medium]3.编写BeanDefinitionParser\[/size] \[code="java"]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 { 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.addConstructorArg(pattern); // this however is an optional property String lenient = element.getAttribute("lenient"); if (StringUtils.hasText(lenient)) { bean.addPropertyValue("lenient", Boolean.valueOf(lenient)); } } }\[/code] \[size=medium]4.钩入配置\[/size] META-INF/spring.handlers指定命名空间处理器 \[code="xml"]http\\://www.mycompany.com/schema/myns=org.springframework.samples.xml.MyNamespaceHandler\[/code] META-INF/spring.schemas指定schema路径，用于schema验证（xsd放在class路径下） \[code="xml"] http\\://www.mycompany.com/schema/myns/myns.xsd=org/springframework/samples/xml/myns.xsd \[/code] \[size=medium] 5.使用扩展标签\[/size] \[code="xml"] \[/code]