您的位置:首页 > 编程语言 > Java开发

spring自定义schema学习

2015-11-16 15:26 585 查看
【转载请注明作者和原文链接,欢迎讨论,相互学习。】

一、前言

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例子完成。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: