• 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

     


  • 相关阅读:
    bugfree3.0.1-修改“优先级”为中文引起的PHP Error
    【vue】vue单元测试:karma+mocha
    【node】node的核心模块---http模块,http的服务器和客户端
    【node】fs模块,文件和目录的操作
    【webpack】从零开始学webpack
    【vue】webpack插件svg-sprite-loader---实现自己的icon组件
    【vue】vue的路由权限管理
    【node】用koa搭建一个增删改服务(一)
    【小程序】小程序之发送模板消息
    【小程序】返回顶部wx.pageScrollTo和scroll-view的对比
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10033636.html
Copyright © 2020-2023  润新知