`
yjhexy
  • 浏览: 327263 次
  • 性别: Icon_minigender_1
  • 来自: 火星
社区版块
存档分类
最新评论

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
  • 大小: 17 KB
分享到:
评论
4 楼 jd2bs 2013-06-08  
2楼肯定是people.xsd格式错了 
3 楼 haoxx1110 2012-10-24  
帮忙解决一下吧,谢谢了

2 楼 haoxx1110 2012-10-24  
请问我按照你这个做的出现了这个异常应该怎么解决??
org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'http://www.haoxx.org/schema/people/people.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
1 楼 sunny3super 2012-07-16  
好文章,另外 http://www.3822.net/topicshow/5802 这里也提供了一个类似的例子,并且提供源代码下载。

相关推荐

    扩展Spring schema样例代码 maven

    基于别人的代码:http://www.yihaomen.com/article/java/438.htm ;弄了个maven项目

    Spring自定义配置Schema可扩展(二)

    主要介绍了Spring自定义配置Schema可扩展(二)的相关资料,需要的朋友可以参考下

    Spring自定义配置Schema可扩展(一)

    本教程主要介绍如何扩展Spring的xml配置,让Spring能够识别我们自定义的Schema和Annotation,,需要的朋友可以参考下

    Spring中XML schema扩展机制的深入讲解

    主要给大家介绍了关于Spring中XML schema扩展机制的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    Spring XML 配置扩展机制

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ...

    Spring中自定义Schema如何解析生效详解

    Spring2.5在2.0的基于Schema的Bean配置的基础之上,再增加了扩展XML配置的机制。通过该机制,我们可以编写自己的Schema,并根据自定义的Schema用自定的标签配置Bean,下面这篇文章主要介绍了关于Spring中自定义...

    精通spring--源代码

     对Spring2.5内置的各种XML Schema支持进行了全方位阐述。包括基于XML Schema的权威配置指南  全书理论与实践并重,通过大量的实例帮助读者尽快掌握Spring2,5的各种基本和高级使用技巧,从而提高本书的参考和阅读...

    精通Spring(书签)

     对Spring2.5内置的各种XML Schema支持进行了全方位阐述。包括基于XML Schema的权威配置指南  全书理论与实践并重,通过大量的实例帮助读者尽快掌握Spring2,5的各种基本和高级使用技巧,从而提高本书的参考和阅读...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.3. Schema-based AOP support 6.3.1. 声明一个切面 6.3.2. 声明一个切入点 6.3.3. 声明通知 6.3.3.1. 通知(Advice) 6.3.3.2. 返回后通知(After returning advice) 6.3.3.3. 抛出异常后通知(After throwing ...

    Spring 2.0 开发参考手册

    6.3. Schema-based AOP support 6.3.1. 声明一个切面 6.3.2. 声明一个切入点 6.3.3. 声明通知 6.3.4. 引入 6.3.5. 切面实例化模型 6.3.6. Advisors 6.3.7. 例子 6.4. AOP声明风格的选择 6.4.1. Spring AOP...

    spring.net中文手册在线版

    Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序。它提供了很多方面的功能,比如依赖注入、面向方面编程(AOP)、数据访问抽象及ASP.NET扩展等等。Spring.NET以Java版的Spring框架为...

    Spring中文帮助文档

    6.3. 基于Schema的AOP支持 6.3.1. 声明一个切面 6.3.2. 声明一个切入点 6.3.3. 声明通知 6.3.4. 引入 6.3.5. 切面实例化模型 6.3.6. Advisor 6.3.7. 例子 6.4. AOP声明风格的选择 6.4.1. Spring AOP还是...

    spring chm文档

    6.3. Schema-based AOP support 6.3.1. 声明一个切面 6.3.2. 声明一个切入点 6.3.3. 声明通知 6.3.4. 引入 6.3.5. 切面实例化模型 6.3.6. Advisors 6.3.7. 例子 6.4. AOP声明风格的选择 6.4.1. Spring AOP...

    Spring API

    6.3. 基于Schema的AOP支持 6.3.1. 声明一个切面 6.3.2. 声明一个切入点 6.3.3. 声明通知 6.3.4. 引入 6.3.5. 切面实例化模型 6.3.6. Advisor 6.3.7. 例子 6.4. AOP声明风格的选择 6.4.1. Spring AOP还是...

    精通Spring (书签版)

     对Spring2.5内置的各种XML Schema支持进行了全方位阐述。包括基于XML Schema的权威配置指南  全书理论与实践并重,通过大量的实例帮助读者尽快掌握Spring2,5的各种基本和高级使用技巧,从而提高本书的参考和阅读...

    精通Spring(书签版)

     对Spring2.5内置的各种XML Schema支持进行了全方位阐述。包括基于XML Schema的权威配置指南  全书理论与实践并重,通过大量的实例帮助读者尽快掌握Spring2,5的各种基本和高级使用技巧,从而提高本书的参考和阅读...

    dubbo-master.zip

    dubbo-master.zipDubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。如果不想使用Spring配置,而希望通过API的方式进行...

    Spring.3.x企业应用开发实战(完整版).part2

    Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。  Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架...

    SpringLearn:阅读Spring

    测试Spring中扩展模式来定义Bean 添加依赖: &lt;groupId&gt;top.ningg.learn.spring &lt;artifactId&gt;spring-learn &lt;version&gt;1.0-SNAPSHOT 配置文件中,添加配置: &lt;beans xmlns=...

    dubbo监控中心war包

    dubbo监控中心war包, 可以直接放到tomcat下运行 Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载

Global site tag (gtag.js) - Google Analytics