扩展schema,定义自己的bean属性。。不错!

主要:

1,定义META-INF下.xsd文件,这里是people.xsd;定义spring.handlers;定义spring.schemas

2,定义namaspace解析类,这里是StudentNamespaceHandler

3,定义beanDefinition,这里是StudentBeanDefinitionParser

4,当然还有相关的javabean定义,这里是Student.java

详细代码:

people.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.luyee.com/bat/schema/people"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://www.luyee.com/bat/schema/people"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans" /> <xsd:element name="student">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType"> <xsd:attribute name="name"
type="xsd:string">
<xsd:annotation>
<xsd:documentation>
姓名
</xsd:documentation>
</xsd:annotation>
</xsd:attribute> <xsd:attribute name="age"
type="xsd:string">
<xsd:annotation>
<xsd:documentation>
年龄
</xsd:documentation>
</xsd:annotation>
</xsd:attribute> </xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>

spring.handlers;

http\://www.luyee.com/bat/schema/people=com.luyee.bat.spring.StudentNamespaceHandler

spring.schemas

http\://www.luyee.com/bat/schema/people.xsd=META-INF/people.xsd

StudentNamespaceHandler和StudentBeanDefinitionParser

package com.luyee.bat.spring;

import java.text.SimpleDateFormat;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element; public class StudentNamespaceHandler extends NamespaceHandlerSupport { public void init() {
registerBeanDefinitionParser("student", new StudentBeanDefinitionParser());
} class StudentBeanDefinitionParser extends AbstractSingleBeanDefinitionParser{
protected Class getBeanClass(Element element) {
return Student.class;
} protected void doParse(Element element, BeanDefinitionBuilder bean) {
String name = element.getAttribute("name");
bean.addPropertyValue("name", name); String age = element.getAttribute("age");
if (StringUtils.hasText(age)) {
bean.addPropertyValue("age", Integer.valueOf(age));
}
}
}
}

JavaBean:Student

package com.luyee.bat.spring;

public class Student {

	private String name;

	private int age;

	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;
} }

测试:applicationContex.xml(people:student就好比bean)

<?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:people="http://www.luyee.com/bat/schema/people"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.luyee.com/bat/schema/people http://www.luyee.com/bat/schema/people.xsd" >
<people:student id="student1" name="student1"
age="18" /> <people:student id="student2" name="student2"
age="20" /> </beans>

StudentXsdTest.java

package com.luyee.bat.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class StudentXsdTest { public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student1 = (Student) ctx.getBean("student1");
Student student2 = (Student) ctx.getBean("student2");
System.out.println("name: " +student1.getName()+" age :" + student1.getAge());
System.out.println("name: " +student2.getName()+" age :" + student2.getAge());
}
}

输出:

八月 13, 2013 8:50:50 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@46f7ba12: startup date [Tue Aug 13 20:50:50 CST 2013]; root of context hierarchy
八月 13, 2013 8:50:50 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
八月 13, 2013 8:50:52 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@25e9a396: defining beans [student1,student2]; root of factory hierarchy
name: student1 age :18
name: student2 age :20

最后贴个文件结构

spring 自定义schema的更多相关文章

  1. spring自定义schema学习

    [转载请注明作者和原文链接,欢迎讨论,相互学习.] 一.前言 1. 最近在学习dubbo,里边很多如provider.consumer.registry的配置都是通过spring自定义Schema来实 ...

  2. spring 自定义schema 加载异常 White spaces are required between publicId and systemId.

    spring 项目启动报错 报错日志如下: Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreExcepti ...

  3. 实战spring自定义属性(schema)

    关于spring自定义属性(schema) 在开发Dubbo应用的时候,我们会在xml中做以下类似的配置: <dubbo:application name="dubbo_service ...

  4. Spring中自定义Schema扩展机制

    一.前言 Spring 为基于 XML 构建的应用提供了一种扩展机制,用于定义和配置 Bean. 它允许使用者编写自定义的 XML bean 解析器,并将解析器本身以及最终定义的 Bean 集成到 S ...

  5. (spring-第2回【IoC基础篇】)Spring的Schema,基于XML的配置

    要深入了解Spring机制,首先需要知道Spring是怎样在IoC容器中装配Bean的.而了解这一点的前提是,要搞清楚Spring基于Schema的Xml配置方案. 在深入了解之前,必须要先明白几个标 ...

  6. spring 自定义标签的实现

    在我们进行Spring 框架开发中,估计用到最多的就是bean 标签吧,其实在Spring中像<mvc/><context/>这类标签以及在dubbo配置的标签都是属于自定义的 ...

  7. spring基础---->spring自定义标签(一)

    Spring具有一个基于架构的扩展机制,可以使用xml文件定义和配置bean.本博客将介绍如何编写自定义XML bean的解析器,并用实例来加以说明.其实我一直相信 等你出现的时候我就知道是你. Sp ...

  8. spring自定义标签之 自我实现

     引言: 最近心情比较难以平静,周末的两天就跑出去散心了,西湖边上走走,看日落,还是不错的.回来博客上发现,在自定义标签上,最后一步实现忘记加上了.其实,人生的路程中,我们总是实现着自我的价值,让自己 ...

  9. spring自定义标签之 规范定义XSD

    引言: spring的配置文件中,一切的标签都是spring定义好的.<bean/>等等,有了定义的规范,才能让用户填写的正常可用.想写自定义标签,但首先需要了解XML Schema De ...

随机推荐

  1. 网站开发常用jQuery插件总结(九)侧边栏插件pageslide

    一.pageslide插件功能 实现现实隐藏侧边栏的功能.插件可以读取另个一html,也可以是当前页面中的元素. 二.pageslide官方地址 http://srobbin.com/jquery-p ...

  2. [转] js call

    call 方法  转自: http://www.cnblogs.com/sweting/archive/2009/12/21/1629204.html调用一个对象的一个方法,以另一个对象替换当前对象. ...

  3. JavaScript学习总结【1】、初识JS

    1.什么是 JavaScript? JavaScript 是一门跨平台.面向对象的动态的弱类型的轻量级解释型语言,是一种基于对象和事件驱动并具有相对安全性的客户端脚本语言.应用于 HTML 文档能够在 ...

  4. Centos6.5安装

    前奏:CentOS 6.5下载地址http://mirror.centos.org/centos/6.5/isos/x86_64/CentOS-6.5-x86_64-bin-DVD1to2.torre ...

  5. 桂电在线_微信公众平台开发之-运用angularjs显示学校公告新闻列表和详情页面

    折腾angularjs的感悟 几天折腾,总的来说看了很多博客,要么不是最新的技术文档,要么写得不够完整,因为别人是基于他们的理解写的技术博客代码不一定会贴完整,所以一旦你用的是最新的想要看完整的实例就 ...

  6. javascript动态添加效果

    <script type="text/javascript"> window.onload=function(){ $("#ch").click(f ...

  7. ng-form

    form提供的属性都是用来表示表单的验证状态的,包括:$pristine(表单没有填写记录).$dirty(表单有填写记录).$valid(通过验证).$invalid(未通过验证).$error(验 ...

  8. print,print_r,echo,var_dump,var_export比较

    print string 1个参数 返回1 语言结构echo 多个string 无返回 语言结构 print_r array 如果想捕捉 print_r() 的输出,可使用 return 参数.若此参 ...

  9. gcd 控制线程执行顺序(供参考)

    dispatch_group_t group = dispatch_group_create(); dispatch_group_async(group, dispatch_get_global_qu ...

  10. bzoj 1031: [JSOI2007]字符加密Cipher 後綴數組模板題

    1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3157  Solved: 1233[Submit ...