spring自定義標(biāo)簽

第一步:編寫xsd文件

首先要定義xml的約束文件办斑,本文使用xsd約束而不是dtd谨垃,
META-INF
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns="http://code.ruochentech.com/schema/dubbo"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://code.ruochentech.com/schema/dubbo">
    <xsd:element name="registry">
        <xsd:complexType>
            <xsd:attribute name="protocol" type="xsd:string"></xsd:attribute>
            <xsd:attribute name="address" type="xsd:string"></xsd:attribute>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="reference">
        <xsd:complexType>
            <xsd:attribute name="id" type="xsd:string"></xsd:attribute>
            <xsd:attribute name="interface" type="xsd:string"></xsd:attribute>
            <xsd:attribute name="loadbalance" type="xsd:string"></xsd:attribute>
            <xsd:attribute name="protocol" type="xsd:string"></xsd:attribute>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="protocol">
        <xsd:complexType>
            <xsd:attribute name="name" type="xsd:string"></xsd:attribute>
            <xsd:attribute name="port" type="xsd:string"></xsd:attribute>
            <xsd:attribute name="host" type="xsd:string"></xsd:attribute>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="service">
        <xsd:complexType>
            <xsd:attribute name="interface" type="xsd:string"></xsd:attribute>
            <xsd:attribute name="ref" type="xsd:string"></xsd:attribute>
            <xsd:attribute name="protocol" type="xsd:string"></xsd:attribute>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

特別注意:
自定義的xsd文件頭
第二步:編寫spring.schemas文件

紅框中的內(nèi)容是自定義的內(nèi)容距芬。但是這兩個(gè)值是一一對(duì)應(yīng)的解滓,spring讀取xml文件內(nèi)容的時(shí)候會(huì)校驗(yàn)這個(gè)xsd文件踊谋,默認(rèn)去META-INF路徑去找spring.schemas文件,spring.schemas文件中內(nèi)容如下后裸,

http\://code.ruochentech.com/schema/dubbo/soa.xsd=META-INF/soa.xsd
第三步:配置spring.handlers文件
http\://code.ruochentech.com/schema/dubbo=com.yonyou.soa.parser.SOANamespaceHandler
第四步:編寫自定義標(biāo)簽解析類的初始化類
public class SOANamespaceHandler extends NamespaceHandlerSupport {
    public void init() {
        registerBeanDefinitionParser("registry",new RegistryBeanDefinitionParser(Registry.class));
        registerBeanDefinitionParser("reference",new ReferenceBeanDefinitionParser(Reference.class));
        registerBeanDefinitionParser("protocol",new ProtocolBeanDefinitionParser(Protocol.class));
        registerBeanDefinitionParser("service",new ServiceBeanDefinitionParser(Registry.class));
    }
}
第五步:自定義標(biāo)簽解析類(以xsd文件中registry標(biāo)簽解析為例)
public class RegistryBeanDefinitionParser implements BeanDefinitionParser {
    private Class<?> beanClass;
    public RegistryBeanDefinitionParser(Class<?> beanClass){
        this.beanClass=beanClass;
    }
    public BeanDefinition parse(Element element, ParserContext parserContext) {
        RootBeanDefinition rootBeanDefinition=new RootBeanDefinition();
        rootBeanDefinition.setBeanClass(beanClass);
        rootBeanDefinition.setLazyInit(false);
        String protocol = element.getAttribute("protocol");
        String address = element.getAttribute("address");
        rootBeanDefinition.getPropertyValues().addPropertyValue("protocol",protocol);
        rootBeanDefinition.getPropertyValues().addPropertyValue("address",address);
        return rootBeanDefinition;
    }
}
第六步:測(cè)試
6.1配置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:ruochen="http://code.ruochentech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://code.ruochentech.com/schema/dubbo http://code.ruochentech.com/schema/dubbo/soa.xsd"
       default-lazy-init="true">
    <bean id="testServiceImpl" class="com.yonyou.soa.test.TestServiceImpl"></bean>
    <ruochen:registry address="127.0.0.1:6379" protocol="redis"></ruochen:registry>
    <ruochen:reference id="testService" protocol="http" interface="com.yonyou.soa.test.TestService" loadbalance="random"></ruochen:reference>
    <ruochen:protocol name="http" host="127.0.0.1" port="27017"></ruochen:protocol>
</beans>

特別注意:
測(cè)試xml文件注意點(diǎn)

demo地址:https://github.com/ruochen0328/soa.git

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末瑰钮,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子微驶,更是在濱河造成了極大的恐慌飞涂,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,331評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件祈搜,死亡現(xiàn)場(chǎng)離奇詭異较店,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)容燕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,372評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門梁呈,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人蘸秘,你說我怎么就攤上這事官卡。” “怎么了醋虏?”我有些...
    開封第一講書人閱讀 167,755評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵寻咒,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我颈嚼,道長(zhǎng)毛秘,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,528評(píng)論 1 296
  • 正文 為了忘掉前任阻课,我火速辦了婚禮叫挟,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘限煞。我一直安慰自己抹恳,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,526評(píng)論 6 397
  • 文/花漫 我一把揭開白布署驻。 她就那樣靜靜地躺著奋献,像睡著了一般。 火紅的嫁衣襯著肌膚如雪旺上。 梳的紋絲不亂的頭發(fā)上瓶蚂,一...
    開封第一講書人閱讀 52,166評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音抚官,去河邊找鬼扬跋。 笑死,一個(gè)胖子當(dāng)著我的面吹牛凌节,可吹牛的內(nèi)容都是我干的钦听。 我是一名探鬼主播,決...
    沈念sama閱讀 40,768評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼倍奢,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼朴上!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起卒煞,我...
    開封第一講書人閱讀 39,664評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤痪宰,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體衣撬,經(jīng)...
    沈念sama閱讀 46,205評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡乖订,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,290評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了具练。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片乍构。...
    茶點(diǎn)故事閱讀 40,435評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖扛点,靈堂內(nèi)的尸體忽然破棺而出哥遮,到底是詐尸還是另有隱情,我是刑警寧澤陵究,帶...
    沈念sama閱讀 36,126評(píng)論 5 349
  • 正文 年R本政府宣布眠饮,位于F島的核電站,受9級(jí)特大地震影響铜邮,放射性物質(zhì)發(fā)生泄漏仪召。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,804評(píng)論 3 333
  • 文/蒙蒙 一牲距、第九天 我趴在偏房一處隱蔽的房頂上張望返咱。 院中可真熱鬧,春花似錦牍鞠、人聲如沸咖摹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,276評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)萤晴。三九已至,卻和暖如春胁后,著一層夾襖步出監(jiān)牢的瞬間店读,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工攀芯, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留屯断,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,818評(píng)論 3 376
  • 正文 我出身青樓侣诺,卻偏偏與公主長(zhǎng)得像殖演,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子年鸳,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,442評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理趴久,服務(wù)發(fā)現(xiàn),斷路器搔确,智...
    卡卡羅2017閱讀 134,694評(píng)論 18 139
  • 作者: 一字馬胡 轉(zhuǎn)載標(biāo)志 【2017-11-17】 更新日志 導(dǎo)入 Spring框架的一大強(qiáng)大之處就是框架的設(shè)...
    一字馬胡閱讀 3,790評(píng)論 0 20
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,840評(píng)論 6 342
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,266評(píng)論 25 707
  • 說起周星馳彼棍,這個(gè)大家耳熟能詳?shù)拿置鹬遥词故巧頌?0后,沒有情真意切在當(dāng)年的電影院看過他的電影座硕,也為會(huì)為這位喜...
    司鐵閱讀 464評(píng)論 0 4