Spring自定义标签
Posted 不痴不立
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring自定义标签相关的知识,希望对你有一定的参考价值。
一、原理:
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自定义标签的主要内容,如果未能解决你的问题,请参考以下文章