码迷,mamicode.com
首页 > 编程语言 > 详细

Spring自定义标签

时间:2016-12-29 16:37:27      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:registry   size   通过   xml解析   blog   指定   efi   meta   tar   

一、原理:

1、Spring通过XML解析程序将其解析为DOM树,

2、通过NamespaceHandler指定对应的Namespace的BeanDefinitionParser将其转换成BeanDefinition。

3、再通过Spring自身的功能对BeanDefinition实例化对象。

二、自定义标签步骤

1、定义三个文件

META-INF/spring.handlers

http\://www.newlandframework.com/dubbo=com.newlandframework.rpc.spring.DubboNamespaceHandler

META-INF/spring.schemas

http\://www.newlandframework.com/dubbo/dubbo.xsd=META-INF/dubbo.xsd

dubbo.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.newlandframework.com/dubbo" 
            xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            xmlns:beans="http://www.springframework.org/schema/beans" 
            targetNamespace="http://www.newlandframework.com/dubbo" 
            elementFormDefault="qualified" 
            attributeFormDefault="unqualified">
    <xsd:import namespace="http://www.springframework.org/schema/beans"/>
    <xsd:element name="service">
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:extension base="beans:identifiedType">
                    <xsd:attribute name="interfaceName" type="xsd:string" use="required"/>
                    <xsd:attribute name="ref" type="xsd:string" use="required"/>
                </xsd:extension>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="registry">
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:extension base="beans:identifiedType">
                    <xsd:attribute name="ipAddr" type="xsd:string" use="required"/>
                    <xsd:attribute name="protocol" type="xsd:string" use="required"/>
                </xsd:extension>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="reference">
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:extension base="beans:identifiedType">
                    <xsd:attribute name="interfaceName" type="xsd:string" use="required"/>
                    <xsd:attribute name="ipAddr" type="xsd:string" use="required"/>
                    <xsd:attribute name="protocol" type="xsd:string" use="required"/>
                </xsd:extension>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

2、实现NameSpace类(继承NamespaceHandlerSupport)

3、配置相应element元素的Parser(实现BeanDefinitionParser接口)和Bean(例如实现InitializingBean, DisposableBean,ApplicationContextAware, ApplicationListener,要看配置在element需要哪些属性)

 

 

Spring自定义标签

标签:registry   size   通过   xml解析   blog   指定   efi   meta   tar   

原文地址:http://www.cnblogs.com/wangwanchao/p/6233097.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!