Spring自定义标签

Spring通过XML解析程序将其解析为DOM树,通过NamespaceHandler指定对应的Namespace的BeanDefinitionParser将其转换成BeanDefinition。再通过Spring自身的功能对BeanDefinition实例化对象。

在期间,Spring还会加载两项资料:

  • META-INF/spring.handlers 
    指定NamespaceHandler(实现org.springframework.beans.factory.xml.NamespaceHandler)接口,或使用org.springframework.beans.factory.xml.NamespaceHandlerSupport的子类。
  • META-INF/spring.schemas 
    在解析XML文件时将XSD重定向到本地文件,避免在解析XML文件时需要上网下载XSD文件。通过现实org.xml.sax.EntityResolver接口来实现该功能。

制作自定义的标签

spring.handlers :
http\://www.xxz.com/schema/user=com.xxz.demo.springsupport.MyNamespaceHandler
  spring.schemas :
http\://www.xxz.com/schema/user.xsd=META-INF/xxz.xsd
xxz.xsd :
<?xml version="1.0" encoding="UTF-8"?>
<schema  xmlns="http://www.w3.org/2001/XMLSchema" 
		targetNamespace="http://www.xxz.com/schema/user"  
    	xmlns:tns="http://www.xxz.com/schema/user" 
    	elementFormDefault="qualified"> 
	<element name="user">  
        <complexType>  
            <attribute name="id" type="string" />  
            <attribute name="name" type="string" />  
            <attribute name="address" type="string" />  
        </complexType>  
    </element>  
</schema>    
com.xxz.demo.springsupport.MyNamespaceHandler
package com.xxz.demo.springsupport;

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

public class MyNamespaceHandler extends NamespaceHandlerSupport{

	public void init() {
		registerBeanDefinitionParser("user", new UserBeanDefinitionParser());
	}
}

com.xxz.demo.springsupport.UserBeanDefinitionParser
package com.xxz.demo.springsupport;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

import com.xxz.demo.bean.User;

public class UserBeanDefinitionParser implements BeanDefinitionParser{

	public BeanDefinition parse(Element element, ParserContext parserContext) {
		String id = element.getAttribute("id");
		String name = element.getAttribute("name");
		String address = element.getAttribute("address");
		
		RootBeanDefinition beanDefinition = new RootBeanDefinition();
		beanDefinition.setBeanClass(User.class);
		beanDefinition.getPropertyValues().addPropertyValue("name", name);
		beanDefinition.getPropertyValues().addPropertyValue("address", address);
		parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);
		return beanDefinition;
	}

}

测试代码

test.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:myname="http://www.xxz.com/schema/user"
     xsi:schemaLocation="  
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
     http://www.xxz.com/schema/user http://www.xxz.com/schema/user.xsd">  
   <myname:user id="testBean" name="xiong" address="beijing"/>  
</beans> 
com.xxz.demo.MainBootStrap
package com.xxz.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xxz.demo.bean.User;

public class MainBootStrap {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:test.xml");
		User bean = (User) ctx.getBean("testBean");
		System.out.println(bean.getName()+","+bean.getAddress());
		
	}

}

输出为:

xiong,beijing

源代码地址下载:https://download.csdn.net/download/xiongxianze/10453635






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值