spring自定义schema学习
【转载请注明作者和原文链接,欢迎讨论,相互学习。】
一、前言
1. 最近在学习dubbo,里边很多如provider、consumer、registry的配置都是通过spring自定义Schema来实现的,为此,我也学习下如何自定义Schema。

2.学习目标
完成自定义一个shema名称为test,节点名为user的例子。
二、准备内容
1.编写java bean
2.编写xsd配置文件
3.编写spring.handlers和spring.schmas
4.编写applicationContext.xml
5.编写NamespaceHandler和BeanDefinitionParser
三、晒代码
1.工程目录结构

2.java bean
package com.cnblogs.ericlin.spring;
public class User {
private String id;
private String name;
private String sex;
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3.xsd文件
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsd:schema xmlns="http://www.cnblogs.com/eric-lin/schema/user"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tool="http://www.springframework.org/schema/tool"
targetNamespace="http://www.cnblogs.com/eric-lin/schema/user"
elementFormDefault="qualified" attributeFormDefault="unqualified"> <xsd:import namespace="http://www.springframework.org/schema/beans" />
<xsd:import namespace="http://www.springframework.org/schema/tool" /> <xsd:element name="user">
<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="sex" type="xsd:string">
<xsd:annotation>
<xsd:documentation>性别</xsd:documentation>
</xsd:annotation>
</xsd:attribute> <xsd:attribute name="age" type="xsd:int">
<xsd:annotation>
<xsd:documentation>年龄</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
4.spring.handlers文件内容
http\://www.cnblogs.com/eric-lin/schema/user=com.cnblogs.ericlin.spring.schema.UserNamespaceHandler
5.spring.schemas文件内容
http\://www.cnblogs.com/eric-lin/schema/user/user.xsd=META-INF/user.xsd
6.applicationContext.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:test="http://www.cnblogs.com/eric-lin/schema/user"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.cnblogs.com/eric-lin/schema/user
http://www.cnblogs.com/eric-lin/schema/user/user.xsd"> <test:user id="eric" name="123" sex="male" age="28" /> </beans>
7.NamespaceHandler
package com.cnblogs.ericlin.spring.schema;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
public class UserNamespaceHandler extends NamespaceHandlerSupport {
public void init() {
registerBeanDefinitionParser("user", new UserBeanDefinitionParser());
}
}
8.BeanDefinitionParser
package com.cnblogs.ericlin.spring.schema; import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.w3c.dom.Element; import com.cnblogs.ericlin.spring.User; public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { @Override
protected Class<?> getBeanClass(Element element) {
return User.class;
} @Override
protected void doParse(Element element, BeanDefinitionBuilder bean) {
String id = element.getAttribute("id");
String name = element.getAttribute("name");
String sex = element.getAttribute("sex");
int age = Integer.parseInt(element.getAttribute("age")); bean.addPropertyValue("id", id);
bean.addPropertyValue("name", name);
bean.addPropertyValue("sex", sex);
bean.addPropertyValue("age", age);
}
}
9测试类
package com.cnglogs.ericlin.spring; import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.cnblogs.ericlin.spring.User; public class TestSpringSchema { @SuppressWarnings("resource")
@Test
public void test() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); User user = (User) context.getBean("eric");
System.out.println(user.getId());
System.out.println(user.getName());
System.out.println(user.getSex());
System.out.println(user.getAge());
}
}
自此,自定义spring shema例子完成。
spring自定义schema学习的更多相关文章
- spring自定义标签学习
看到几篇很全的自定义标签,从定义到使用,写的很好. 这里我也是在那里学习的,对学习spring源码也很有帮助. 贴出来与大家共享. http://sammor.iteye.com/blog/11009 ...
- spring 自定义schema
扩展schema,定义自己的bean属性..不错! 主要: 1,定义META-INF下.xsd文件,这里是people.xsd;定义spring.handlers;定义spring.schemas 2 ...
- spring 自定义schema 加载异常 White spaces are required between publicId and systemId.
spring 项目启动报错 报错日志如下: Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreExcepti ...
- 实战spring自定义属性(schema)
关于spring自定义属性(schema) 在开发Dubbo应用的时候,我们会在xml中做以下类似的配置: <dubbo:application name="dubbo_service ...
- Spring源码学习-容器BeanFactory(四) BeanDefinition的创建-自定义标签的解析.md
写在前面 上文Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签对Spring默认标签的解析做了详解,在xml元素的解析中,Spri ...
- [spring源码学习]三、IOC源码——自定义配置文件读取
一.环境准备 在文件读取的时候,第9步我们发现spring会根据标签的namespace来选择读取方式,联想spring里提供的各种标签,比如<aop:xxx>等应该会有不同的读取和解析方 ...
- Spring 源码学习(1) —— 自定义标签
Spring 工作流程是先加载解析xml配置文件:配置文件中存在默认的标签,也可以自定义标签.解析默认标签调用: private void parseDefaultElement(Element el ...
- Spring中自定义Schema扩展机制
一.前言 Spring 为基于 XML 构建的应用提供了一种扩展机制,用于定义和配置 Bean. 它允许使用者编写自定义的 XML bean 解析器,并将解析器本身以及最终定义的 Bean 集成到 S ...
- Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签
写在前面 上文Spring源码学习-容器BeanFactory(二) BeanDefinition的创建-解析前BeanDefinition的前置操作中Spring对XML解析后创建了对应的Docum ...
随机推荐
- CSS3实现加载中效果
代码: <!doctype html> <html> <head> <meta charset="utf-8" /> <tit ...
- MySql 里的IFNULL、NULLIF和ISNULL用法区别
mysql中isnull,ifnull,nullif的用法如下: isnull(expr) 的用法:如expr 为null,那么isnull() 的返回值为 1,否则返回值为 0. mysql> ...
- 最大公共字串LCS问题(阿里巴巴)
给定两个串,均由最小字母组成.求这两个串的最大公共字串LCS(Longest Common Substring). 使用动态规划解决. #include <iostream> #inclu ...
- 代码质量管理工具 sonar 配置
代码检查工具有很多findBugs等等 sonar配置: 1.下载sonar 5.5, 解压,运行 sonarqube-5.5\bin\windows-x86-64\StartSonar.bat , ...
- maven项目常见问题
问题1:Maven项目,右键-update project后报错如下的解决办法: 1).DescriptionResourcePathLocationType Java compiler level ...
- Linux C popen()函数详解
表头文件 #include<stdio.h> 定义函数 FILE * popen( const char * command,const char * type); 函数说明 popen( ...
- Shell入门教程:流程控制(6)while 循环
while循环的语法: while 条件测试 do 命令区域 done 举例: #!/bin/bash declare -i i=1 declare -i sum=0 while ((i< ...
- Java 串口通信
在Windows系统下,用Java开发串口通信相关的程序时,需要用到几个文件. (1)win32com.dll 要放在jdk\jre\bin目录下. (2)comm.jar 和javax.comm.p ...
- 记录一下git 的常用命令
以后如果要写一个东西,最好先搭建一个本地仓库,用版本控制对其进行操作,可能一开始有一些麻烦,但是很有可能会受益无穷. 说到git,必然会和github联系起来. 不管是在ubuntu里面还是在Wind ...
- Spring预处理
当需要在某些Spring项目一启动,就执行某些操作时,需要实现改接口ApplicationListener,重写onApplicationEvent方法,将需要的预处理操作全部写在该方法中 当初始化完 ...