spring 自定义标签实现传递list属性
2016-05-20 13:19
447 查看
本例的全部源代码:https://git.oschina.net/mn_1127/spring-ext.git
1. 定义XSD文件:
2. 设置:spring.handlers
3. 设置:spring.schemas
4. 定义spring对自定义的命名空间进行解析的操作
5.将解析类注册到命名空间中的实现
6.定义需要spring管理bean的实体 People.java
7. 然后配置spring的xml配置文件test.xml
8. 最后写一个测试类,简单起见,直接用main函数的方式
本例的全部源代码: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
相关文章推荐
- Spring源码学习——自定义标签
- dubbo自定义标签
- spring自定义标签使用
- Spring在初始化过程中的扩展机会
- 关于使用运算符"||"、"&&"的小例子
- 如何解决eclipse android开发中的R cannot be resolved 的错误
- struts2中的几个技术
- spring boot集成data-jpa
- Java遍历文件夹下所有文件,并且将数据保存在数据库当中
- JavaSE入门学习44:文件传输基础之I/O流(三)
- Spring DATA JPA 中findAll 进行OrderBy
- Spring的父子容器问题
- java中void
- Spring 源码解析之HandlerAdapter源码解析(二)
- springmvc与struts2的区别
- Eclipse 常用快捷键
- struts2的第一次实践
- Spring Bean的范围
- (4.1.27.4)NI/NDK开发指南(一)——JVM查找java native方法的规则
- Java+WebSocket+WebRTC实现视频通话实例