首页 > 代码库 > spring 自定义标签 学习二
spring 自定义标签 学习二
在上篇中写的只支持写属性,不支持标签property的写法,但是如果有时候我们还想做成支持 property的用法,则可以在xsd中增加spring 自带的xsd引用
修改xsd文件如下:
<?xml version="1.0"encoding="UTF-8"?> <xsd:schema xmlns="http://www.ruishenh.com/custom/myTest" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" targetNamespace="http://www.ruishenh.com/custom/mytest" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xsd:import namespace="http://www.springframework.org/schema/beans"/> <xsd:element name="executor"> <xsd:complexType> <xsd:complexContent> <xsd:extension base="beans:identifiedType"> <xsd:group ref="beans:beanElements" /> <xsd:attribute name="name" type="xsd:string"/> <xsd:attribute name="delay" type="xsd:int"/> <xsd:attribute name="interval" type="xsd:int"/> <xsd:attribute name="address" type="xsd:string"/> <xsd:attribute name="entity" type="xsd:string"/> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:element> <xsd:element name="entity"> <xsd:complexType> <xsd:complexContent> <xsd:extension base="beans:identifiedType"> <xsd:group ref="beans:beanElements" /> <xsd:attribute name="name" type="xsd:string"/> <xsd:attribute name="splitBy" type="xsd:string"/> <xsd:attribute name="sql" type="xsd:string"/> <xsd:attribute name="dbTypeID"> <xsd:simpleType> <xsd:restriction base="xsd:int"> <xsd:enumeration value=http://www.mamicode.com/"1" />>
修改BeanDefinitionPaser如下
package com.ruishenh.spring.config; import java.util.List; public class MyBeanDefinitionParser2 extends AbstractSingleBeanDefinitionParser { private Class<?> clssze; public MyBeanDefinitionParser2(Class<?> cls) { this.clssze = cls; } @Override protected String getBeanClassName(Element element) { return clssze.getName(); } @Override protected void doParse(Elementelement, ParserContext parserContext, BeanDefinitionBuilder builder) { List<Element> els = DomUtils.getChildElements(element); NamedNodeMap nnm = element.getAttributes(); for (int i = 0; i <nnm.getLength(); i++) { Node node = nnm.item(i); String key = node.getLocalName(); String value = http://www.mamicode.com/node.getNodeValue();>SpringBean的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:mi="http://www.ruishenh.com/custom/mytest" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.ruishenh.com/custom/mytesthttp://www.ruishenh.com/custom/mytest/myTest2.xsd"> <mi:executor address="127.0.0.1" name="myexecutor"id="myexecutor"> <property name="interval" value=http://www.mamicode.com/"5000"/>>
测试类:
packagecom.ruishenh.spring.test; importorg.springframework.context.support.FileSystemXmlApplicationContext; importcom.ruishenh.spring.config.MyEntity; importcom.ruishenh.spring.config.MyExecutor; publicclass Test { public static void main(String[] args) { String conf ="classpath:spring/myTest2.xml"; FileSystemXmlApplicationContext ac = newFileSystemXmlApplicationContext(conf); MyExecutor me = (MyExecutor)ac.getBean("myexecutor2"); MyExecutor me2 = (MyExecutor)ac.getBean("myexecutor"); System.out.println(me.toString()); System.out.println(me2.toString()); MyEntity mye = (MyEntity) ac.getBean("mye"); MyEntity mye2 = (MyEntity)ac.getBean("mye2"); System.out.println(mye.toString()); System.out.println(mye2.toString()); } }
运行结果:
MyExecutor[name=myexecutor2,delay=2000,interval=5000,address=192.168.201.23,entity=MyEntity[dbTypeID=2,dbTypeName=oracle,splitBy=orderId,sql=select* from tablename,name=mye2]]
MyExecutor[name=myexecutor,delay=2000,interval=5000,address=127.0.0.1,entity=MyEntity[dbTypeID=1,dbTypeName=mysql,splitBy=id,sql=1,name=mye]]
MyEntity[dbTypeID=1,dbTypeName=mysql,splitBy=id,sql=1,name=mye]
MyEntity[dbTypeID=2,dbTypeName=oracle,splitBy=orderId,sql=select* from tablename,name=mye2]
实体类和基本信息在:http://blog.csdn.net/ruishenh/article/details/33741501
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。