扩展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. AppDomain与进程、线程、Assembly之间关系

    AppDomain是CLR的运行单元,它可以加载Assembly.创建对象以及执行程序 AppDomain是CLR实现代码隔离的基本机制.   每一个AppDomain可以单独运行.停止:每个AppD ...

  2. Day16 DOM &jQuery

    一.本节主要内容 JavaScript 正则表达式 字符串操作的三个方法 DOM(知道就行,一般使用jQuery) 查找: 直接查找: document.getElementById 根据ID获取一个 ...

  3. highcharts-Highmaps 动态传入城市名称

    做前端按地区(地图)分布监控数据展示用了 HIGHMAPS JAVASCRIPT MAPS 控件,很好很强大. 基础实现是这样的:调用插件动态传入需要展示的数据(data),插件会在地图数据(mapd ...

  4. python 过滤html方法

    from HTMLParser import HTMLParser class MLStripper(HTMLParser): """ 过滤html方法 "&q ...

  5. Quartz1.8.5例子(二)

    /* * Copyright 2005 - 2009 Terracotta, Inc. * * Licensed under the Apache License, Version 2.0 (the ...

  6. imacros实现Excel数据自动录入到网页中

    一.工具选择 最近接到一个项目,需要将excel数据逐条录入.保存到网页中.经过搜集资料,能实现功能的大概有以下几种方式,按键精灵.autoit.imacros.python+selenium. 按键 ...

  7. jquery插件,美化select标签

    最近经常与select打交道,因为ie下的select实在太丑了,css怎么搞都搞不好看,因为程序已经写得差不多了,要再去模拟select改动太大,就想着能否不改动select,同时美化它.借鉴一下这 ...

  8. 最常用的动态sql语句梳理——分享给使用Mybatis的小伙伴们!

    公司项目中一直使用Mybatis作为持久层框架,自然,动态sql写得也比较多了,最常见的莫过于在查询语句中使用if标签来动态地改变过滤条件了.Mybatis的强大特性之一便是它的动态sql,免除了拼接 ...

  9. 与QString("我是中文")完全一样,你必须告诉tr这个窄字符串是何种编码?你不告诉它,它就用latin1。于是所谓的乱码问题就出来了。

    在论坛中漂,经常遇到有人遇到tr相关的问题.用tr的有两类人: (1)因为发现中文老出问题,然后搜索,发现很多人用tr,于是他也开始用tr (2)另一类人,确实是出于国际化的需要,将需要在界面上显示的 ...

  10. truncate 空间不释放问题

    SQL> set linesize 200 SQL> select segment_name, sum(bytes / 1024 / 1024/1024) from dba_segment ...