扩展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. web服务器的卸载

     在卸载这三个应用之前,咱们可以在终端通过运行“dkpg -l”来查看软件状态. 方法一:选择dpkg -P来卸载软件. 因为dpkg --remove只是删除安装的文件,但不删除配置文件.而dpkg ...

  2. LVS原理详解及部署之二:LVS原理详解(3种工作方式8种调度算法)

    一.集群简介 什么是集群 计算机集群简称集群是一种计算机系统,它通过一组松散集成的计算机软件和/或硬件连接起来高度紧密地协作完成计算工作.在某种意义上,他们可以被看作是一 台计算机.集群系统中的单个计 ...

  3. iOS:Swift界面实例1, 简单界面

    Apple推出了基于Objective-C的新语言Swift. 通过实例, 我们可以很好的感受这门新语言 注意事项: 在XCode6_Beta中, 如果有中文, IDE的自动补全功能就会失效, 所以开 ...

  4. codeforces 359 C - Robbers' watch

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u   Description Robb ...

  5. linux下配置NFS服务器

    (声明:本文大部分文字摘自Linux NFS服务器的安装与配置) 一.NFS简介     NFS 是Network File System的缩写,即网络文件系统.一种使用于分散式文件系统的协定,由Su ...

  6. NET Core+Code First+Docker

    NET Core+Code First+Docker背景介绍 本文将会示范如何在Windows系统下基于ASP.NET Core构建跨平台服务,并通过Docker容器运行发布. 首先说一下为什么选择这 ...

  7. iOS中默认样式修改-b

    项目中有大量的UITableView都需要显示sectionHeader.iOS中默认sessionHeader上的textLabel样式跟设计图不符. 按照我们之前的解决方案,是在每个UITable ...

  8. Web 研发模式演变

    前不久徐飞写了一篇很好的文章:Web 应用的组件化开发.本文尝试从历史发展角度,说说各种研发模式的优劣. 一.简单明快的早期时代 可称之为 Web 1.0 时代,非常适合创业型小项目,不分前后端,经常 ...

  9. bzoj 3041: 水叮当的舞步 迭代加深搜索 && NOIP RP++

    3041: 水叮当的舞步 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 72  Solved: 44[Submit][Status] Descript ...

  10. TCP/IP小纪

    链 路 层 主 要 有 三 个 目 的 :( 1 )为 I P 模 块 发 送 和 接收 I P 数 据 报 ; ( 2 )为 A R P 模块发送 A R P 请 求 和 接 收 A R P 应 答 ...