spring 自定义标签实现传递list属性

本例的全部源代码:https://git.oschina.net/mn_1127/spring-ext.git

 

1. 定义XSD文件:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.37link.com/schema/scf"
	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.37link.com/schema/scf">
	<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
	<xsd:import namespace="http://www.springframework.org/schema/beans" />
	<xsd:import namespace="http://www.springframework.org/schema/tool" />

	<xsd:element name="student" type="consumerType">
		<xsd:annotation>
			<xsd:documentation><![CDATA[ this is desc ]]></xsd:documentation>
		</xsd:annotation>
	</xsd:element>
	<xsd:element name="own" type="ownType">
		<xsd:annotation>
			<xsd:documentation><![CDATA[ this is desc ]]></xsd:documentation>
		</xsd:annotation>
	</xsd:element>
	<xsd:complexType name="ownType">
		<xsd:attribute name="value" type="xsd:string" use="required">
			<xsd:annotation>
				<xsd:documentation><![CDATA[ this is desc ]]></xsd:documentation>
			</xsd:annotation>
		</xsd:attribute>
	</xsd:complexType>

	<xsd:complexType name="consumerType">
		<xsd:complexContent>
			<xsd:extension base="abstractServiceType">
				<xsd:sequence minOccurs="0" maxOccurs="unbounded">
					<xsd:element ref="own" minOccurs="0" maxOccurs="unbounded" />
				</xsd:sequence>
			</xsd:extension>
		</xsd:complexContent>
	</xsd:complexType>
	<xsd:complexType name="abstractServiceType">

		<xsd:attribute name="id" type="xsd:ID" use="required">
			<xsd:annotation>
				<xsd:documentation><![CDATA[ this is desc ]]></xsd:documentation>
			</xsd:annotation>
		</xsd:attribute>
		<xsd:attribute name="name" type="xsd:string" use="required">
			<xsd:annotation>
				<xsd:documentation><![CDATA[ this is desc ]]></xsd:documentation>
			</xsd:annotation>
		</xsd:attribute>
		<xsd:attribute name="age" type="xsd:int" use="required">
			<xsd:annotation>
				<xsd:documentation><![CDATA[ this is desc ]]></xsd:documentation>
			</xsd:annotation>
		</xsd:attribute>
	</xsd:complexType>
</xsd:schema>

2. 设置:spring.handlers

http\://www.37link.com/schema/scf=com.mm.spring_ext.MyNamespaceHandler

3. 设置:spring.schemas

http\://www.37link.com/schema/scf/scf.xsd=com/mm/spring_ext/scf.xsd 

4. 定义spring对自定义的命名空间进行解析的操作

package com.mm.spring_ext;

import java.util.ArrayList;
import java.util.List;

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

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");
		NodeList elementSons = element.getChildNodes();
		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));
		}
		if (elementSons != null && elementSons.getLength() > 0) {
			List<String> list = new ArrayList<String>();
			for (int i = 0; i < elementSons.getLength(); i++) {
				Node node = elementSons.item(i);
				if (node instanceof Element) {
					if ("own".equals(node.getNodeName()) || "own".equals(node.getLocalName())) {
						String value = ((Element) node).getAttribute("value");
						list.add(value);
					}
				}
			}
			bean.addPropertyValue("own", list);
		}

	}
}

5.将解析类注册到命名空间中的实现

package com.mm.spring_ext;

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

public class MyNamespaceHandler extends NamespaceHandlerSupport {
	public void init() {
		registerBeanDefinitionParser("student", new PeopleBeanDefinitionParser());
	}
}

 6.定义需要spring管理bean的实体 People.java

package com.mm.spring_ext;

import java.util.List;

public class People {
	private String id;
	private String name;
	private Integer age;
	private List<String> own;

	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 Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public List<String> getOwn() {
		return own;
	}

	public void setOwn(List<String> own) {
		this.own = own;
	}

}

7. 然后配置spring的xml配置文件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:scf="http://www.37link.com/schema/scf"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
    http://www.37link.com/schema/scf http://www.37link.com/schema/scf/scf.xsd  
    ">
	<scf:student id="scf1" name="liujie" age="23">
		<scf:own value="dddddd" />
		<scf:own value="aaaa" />
	</scf:student>
</beans>  

 

8. 最后写一个测试类,简单起见,直接用main函数的方式

package com.mm.spring_ext_test;

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

import com.mm.spring_ext.People;

public class AppTest {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("test.xml");
		People f = (People) ctx.getBean("scf1");
		if (f != null && f.getOwn() != null && f.getOwn().size() > 0) {
			for (String own : f.getOwn()) {
				System.out.println(own);
			}
		}
	}
}

本例的全部源代码:https://git.oschina.net/mn_1127/spring-ext.git

转载于:https://my.oschina.net/mn1127/blog/678490

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值