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 自己定义标签 学习二的更多相关文章
- freemarker自己定义标签(二)
freemarker自己定义标签 1.自己定义标签 通过自己定义标签,写一个反复指定字符串 2.实现源代码 <html> <head> <meta http-equiv= ...
- dubbo源码学习(二) : spring 自定义标签
做dubbo的配置时很容易发现,dubbo有一套自己的标签,提供给开发者配置,其实每一个标签对应着一个 实体,在容器启动的时候,dubbo会对所有的配置进行解析然后将解析后的内容设置到实体里,最终du ...
- Spring+SpringMVC+MyBatis深入学习及搭建(十二)——SpringMVC入门程序
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6999743.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十一)--S ...
- Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签
写在前面 上文Spring源码学习-容器BeanFactory(二) BeanDefinition的创建-解析前BeanDefinition的前置操作中Spring对XML解析后创建了对应的Docum ...
- Spring+SpringMVC+MyBatis深入学习及搭建(十二)——SpringMVC入门程序(一)
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6999743.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十一)——S ...
- Spring+SpringMVC+MyBatis深入学习及搭建(二)——MyBatis原始Dao开发和mapper代理开发
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6869133.html 前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(一)——My ...
- Spring Boot 项目学习 (二) MySql + MyBatis 注解 + 分页控件 配置
0 引言 本文主要在Spring Boot 基础项目的基础上,添加 Mysql .MyBatis(注解方式)与 分页控件 的配置,用于协助完成数据库操作. 1 创建数据表 这个过程就暂时省略了. 2 ...
- Java开发学习(二十二)----Spring事务属性、事务传播行为
一.事务配置 上面这些属性都可以在@Transactional注解的参数上进行设置. readOnly:true只读事务,false读写事务,增删改要设为false,查询设为true. timeout ...
- HTML学习笔记——标签(二)
标签十二:<ul>标签 语义:是没有前后顺序的信息列表 语法: <ul> <li>信息</li> <li>信息</li> ... ...
随机推荐
- leetcode python 005
## 给定字符串,寻找最长回文子串## 单回文,双回文 def findh(s): ## 单回文 ld,l=[],len(s) if len(s)<3: re ...
- CentOS7安装配置Amanda
参考: https://wenku.baidu.com/view/881e0c998e9951e79a892759.html yum -y install amanda* http://blog ...
- matlab中的reshape快速理解,卷积和乘积之间的转换
reshape: THe convertion between convolution and multiplication:
- 构造方法调用另一个构造方法,用this
using System; class Person { public int age; public string name; public Person(int age, string name) ...
- Microsoft Project 常用快捷键
任务升级 : ALT + SHIFT + 向左键 任务降级: ALT + SHIFT + 向右键 滚动到表头(第一个任务):Ctrl + HOME 滚动到表尾(最后一个任务):Ctrl + E ...
- HDU 2013(递归)
Problem Description 喜欢西游记的同学肯定都知道悟空偷吃蟠桃的故事,你们一定都觉得这猴子太闹腾了,其实你们是有所不知:悟空是在研究一个数学问题!什么问题?他研究的问题是蟠桃一共有多少 ...
- HDU 1205 吃糖果(想想题)
题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=1205 Problem Description HOHO,终于从Speakless手上赢走了所有的糖果, ...
- table中head表头固定,body滚动
<style type="text/css"> .table-head { background-color: #; color: #; } .table-body { ...
- 【转】Skynet之消息队列 - 消息的存储与分发
Skynet之消息队列 - 消息的存储与分发 http://www.outsky.org/code/skynet-message-queue.html Sep 8, 2014 按我的理解,消息队列是S ...
- MySQL中的视图详解
一.什么是视图? 简单来说,视图就是从一张表中导出的虚拟表.视图拥有表的结构,但是在数据库中只有视图的定义,但是没有视图中的数据. 视图是由查询语句从一张表中导出来的数据,不是一张实际的表. 二.视图 ...