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> ... ...
随机推荐
- transiton,transform,animation,border-image
animation,transition,transform三者联系与区别: https://www.jianshu.com/p/0e0e1903b80d transform: 使用小技巧: tran ...
- μC/OS-II在Microblaze上的移植与使用专题--“安富利杯”赛灵思FPGA设计技巧与应用创新博文大赛参赛作品
reference:http://xilinx.eetrend.com/d6-xilinx/blog/2010-05/682.html 随着集成电路设计与制造技术的发展,FPGA芯片的容量越来越大 ...
- ssh免密登陆
1:建立新用户hadoop 2:进入/home/hadoop/.ssh/目录 3:所有要免密连接的终端运行: ssh-keygen -t rsa 三次回车后会产生:id_rsa,id_rsa.pub两 ...
- spring4全注解web项目demo
记得没接触框架的时候,写demo测试时真的很爽,新建web项目,然后随便写写servlet随便调试 框架越来越多,配置记不得了,整合容易出问题,集成新东西越来越少了,不敢动了. 这是个spring4的 ...
- jmeter中测试接口
本文主要介绍在jmeter中测试接口:主要从以下几个方面进行说明: 1.jmeter简介 2.jmeter怎么做接口测试 3.jmeter进行参数化的几种形式 4.jmeter中处理乱码方法 5.jm ...
- VS2017企业版的密钥
Visual Studio 2017(VS2017) 企业版 Enterprise 注册码:NJVYC-BMHX2-G77MM-4XJMR-6Q8QFVisual Studio 2017(VS2017 ...
- 1)jquery validate 远程验证remote,自定义验证 , 手机号验证 2)bootstrap validate 远程remote验证的方法.
1)jquery validate 远程验证remote,自定义验证 1-1: js <script src="YYFramework/Public/js/jquery-3.1.1. ...
- vue项目功能
vue-router { path: '/', name: 'home', // 路由的重定向 ...
- http协议tcp协议ip协议三次握手四次挥手,为什么三次握手,为什么四次挥手,sockete套接字理解
1.1 TCP是什么? TCP是Tranfer Control Protocol的简称,TCP协议是一种面向连接的.可靠的.基于字节流的运输层通信协议.通过TCP协议传输,得到的是一个顺序的无差错的数 ...
- python生产者消费者模型优点
生产者消费者模型:解耦,通过队列降低耦合,支持并发,生产者和消费者是两个独立的并发体,他们之间使用缓存区作为桥梁连接,生产者指望里丢数据,就可以生产下一个数据了,消费者从中拿数据,这样就不会阻塞,影响 ...