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="1" /> <xsd:enumeration value="2" /> <xsd:enumeration value="3" /> </xsd:restriction> </xsd:simpleType> </xsd:attribute> <xsd:attribute name="dbTypeName"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="mysql" /> <xsd:enumeration value="oracle" /> <xsd:enumeration value="sqlserver" /> </xsd:restriction> </xsd:simpleType> </xsd:attribute> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:element> </xsd:schema>
改動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 = node.getNodeValue(); if ("id".equals(key)) { continue; } if ("entity".equals(key)){ if(parserContext.getRegistry().containsBeanDefinition(value)) { builder.addPropertyValue(key,parserContext.getRegistry().getBeanDefinition(value)); }else{ builder.addPropertyReference(key,value); } }else{ builder.addPropertyValue(key,value); } } for (Element element2 :els) { String name = element2.getAttribute("name"); String value = element2.getAttribute("value"); String ref = element2.getAttribute("ref"); if (!StringUtils.hasText(ref)){ builder.addPropertyValue(name,value); }else{ if(parserContext.getRegistry().containsBeanDefinition(ref)) { builder.addPropertyValue(name,parserContext.getRegistry().getBeanDefinition(ref)); }else{ builder.addPropertyReference(name,ref); } } } } }
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="5000"/> <property name="delay" value="2000"/> <property name="entity" ref="mye"/> </mi:executor> <mi:executor address="192.168.201.23" name="myexecutor2" id="myexecutor2" entity="mye2"> <property name="interval" value="5000"/> <property name="delay" value="2000"/> </mi:executor> <mi:entity name="mye" id="mye"> <property name="dbTypeID" value="1"/> <property name="dbTypeName" value="mysql" /> <property name="splitBy" value="id"/> <property name="sql" value="1"/> </mi:entity> <mi:entity name="mye2" id="mye2"> <property name="dbTypeID" value="2"/> <property name="dbTypeName" value="oracle" /> <property name="splitBy" value="orderId"/> <property name="sql" value="select * fromtablename" /> </mi:entity> </beans>
測試類:
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
spring 自己定義標簽 學習二