扩展spring schema 的方法, 代码下载

本文介绍如何使用Spring自定义XML Schema来简化配置过程。通过创建特定的命名空间处理器和解析器,可以实现在XML配置文件中使用自定义标签。文章提供了完整的代码示例和步骤说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在写程序框架的过程中,经常为了简化程序,可能需要自定义schema, SPRING 对于JAVA开发者来说,是很好的工具,因此基于SPRING schema 的扩展对框架有很大用处。

比如做成自己的schema 之后,就可以在 xml 配置文件中类似如下的定义: <comtest:people id="cutesource" name="一号门" age="1024"/> 这里的comtest 就是自定义的schema.
曾经在优快云 搜到一篇文章,按照他的方式,我确实没做出来,可能比较笨的原因。后来自己参考他的方法,按自己的思路做出来了。
现在贴出来,供大家参考。
建立两个工程
1.springcomponent  (用来实现spring schema 的扩展)
2.sprintschema (用来测试spring schema  的扩展,比须将 springcomponent  打成jar包,放到此工程中)

两个工程的基本结构如图,看上去有错,那是因为 spring schema xsd  校验引起的,运行并没有关系。

其关键在于,springcomponent 工程中 schema 的扩展。

package com.springwork.xml;

import org.springframework.beans.factory.xml.NamespaceHandlerSupport;  

public class MyNamespaceHandler extends NamespaceHandlerSupport {  
    public void init() {  
        registerBeanDefinitionParser("people", new PeopleBeanDefinitionParser());  
    }  
}  
package com.springwork.xml;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;  
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;  
import org.springframework.util.StringUtils;  
import org.w3c.dom.Element;  
public class PeopleBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
	
    protected Class getBeanClass(Element element) {  
        return People.class;  
    }  
    protected void doParse(Element element, BeanDefinitionBuilder bean) {  
        String name = element.getAttribute("name");  
        String age = element.getAttribute("age");  
        String id = element.getAttribute("id");  
        if (StringUtils.hasText(id)) {  
            bean.addPropertyValue("id", id);  
        }  
        if (StringUtils.hasText(name)) {  
            bean.addPropertyValue("name", name);  
        }  
        if (StringUtils.hasText(age)) {  
            bean.addPropertyValue("age", Integer.valueOf(age));  
        }  
    }  
}  

package com.springwork.xml;

public class People {
	private String name;
	private int age;
	private String id;
	
	
	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;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
		

}
<?xml version="1.0" encoding="UTF-8"?>  
<xsd:schema   
    xmlns="http://www.yihaomen.com/schema/people"  
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"   
    xmlns:beans="http://www.springframework.org/schema/beans"  
    targetNamespace="http://www.yihaomen.com/schema/people"  
    elementFormDefault="qualified"   
    attributeFormDefault="unqualified">  
    
    <xsd:import namespace="http://www.springframework.org/schema/beans" />  
    
    <xsd:element name="people">  
        <xsd:complexType>  
            <xsd:complexContent>  
                <xsd:extension base="beans:identifiedType">  
                    <xsd:attribute name="name" type="xsd:string" />  
                    <xsd:attribute name="age" type="xsd:int" />  
                </xsd:extension>  
            </xsd:complexContent>  
        </xsd:complexType>  
    </xsd:element>  
</xsd:schema>



测试代码: 你就可以看到结果了。

package com.springwork.xml;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/config/application.xml");  
	    People p = (People)ctx.getBean("cutesource");  
	    System.out.println(p.getId());  
	    System.out.println(p.getName());  
	    System.out.println(p.getAge());  
	}

}


附上原代码,打包,去掉了SPRING JAR 包,大家自己加进去。太大了。测试工程里面,还加入了一个servlet的测试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值