spring自定义标签的使用

首先写一个pojo类user来接收配置文件

public class User {
	private String userName;
	private String emal;
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getEmal() {
		return emal;
	}
	public void setEmal(String emal) {
		this.emal = emal;
	}
}

定义一个xsd文件描述组件的内容 user.xsd

targetNamespace="http://www.ry.com/schema/user"意思是命名空间为 http://www.ry.com/schema/user
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.ryazmm.com/schema/user"
xmlns:tns="http://www.ranyang.com/schema/user"
elementFormDefault="qualified"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <xsd:element name="user">
        <xsd:complexType>
		<xsd:attribute name="id" type="string" />
		<xsd:attribute name="userName" type="string" />
		<xsd:attribute name="emal" type="string" />
	</xsd:complexType>
 </xsd:element>
</xsd:schema>

创建一个文件解析xsd文件和组件的定义

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.w3c.dom.Element;
import org.springframework.util.StringUtils;

public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser{
	
	protected Class getBeanClass(Element element)
	{
		return User.class;
	}
	protected void doParse(Element element,BeanDefinitionBuilder bean){
		String userName=element.getAttribute("userName");
		String emal=element.getAttribute("emal");
		if(StringUtils.hasText(userName))
		{
			bean.addPropertyValue("userName",userName);
		}
		if(StringUtils.hasText(emal))
		{
			bean.addPropertyValue("emal",emal);
		}
	}
}

创建一个handler文件为了把组件注册到spring容器

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

public class MyNamespaceHandler extends NamespaceHandlerSupport{

	@Override
	public void init() {
		// TODO Auto-generated method stub
		registerBeanDefinitionParser("user",new UserBeanDefinitionParser());
	}
}

下面重要的一点是编写映射文件

spring.handlers内容为

http\://www.ry.com/schema/user=com.fhit.util.MyNamespaceHandler 意思是当遇到以user为开头的bean时,进入方法MyNamespaceHandler

spring.schemas内容为

http\://www.ry.com/schema/user.xsd=META-INF/user.xsd 意思是引用的www.ry.com/schema/user.xsd指向本地META-INF/user.xsd

创建测试配置文件test.xml

<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:myname="http://www.ranyang.com/schema/user"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
	http://www.ranyang.com/schema/user
	http://www.ranyang.com/schema/user.xsd">
	
	<myname:user id="testbean" userName="aa" emal="bb"/>
</beans>

最终测试

public static void main(string[] args){
                //通过映射加载test.xml 遇到bean<myname:user时依据user.xsd进行解析,通过spring.handlers映射到
                //MyNamespaceHandler再进入UserBeanDefinitionParser()加入到beanFactory中
                ApplicationContext bf=new ClassPathXmlApplicationContext("com/fhit/util/test.xml");

                User user=(User) bf.getBean("testbean");
		system.out.print(user.getUserName()+","+user.getEmal());
}

输出为aa,bb

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值