首页 > 代码库 > Spring自定义标签
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自定义标签
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。