spring自訂標籤

來源:互聯網
上載者:User

spring自訂標籤
Spring自訂標籤的原理

XML通常通過DTD、XSD定義,但DTD的表達能力較弱,XSD定義則能力比較強,能夠定義類型,出現次數等。自訂標籤需要XSD支援,在實現時使用Namespace擴充來支援自訂標籤。

當你在苦逼的寫下面的代碼時:

Xml代碼 XXXX XXXX

是不是會羨慕這樣寫代碼呢?

Xml代碼

Spring通過XML解析程式將其解析為DOM樹,通過NamespaceHandler指定對應的Namespace的BeanDefinitionParser將其轉換成BeanDefinition。再通過Spring自身的功能對BeanDefinition執行個體化對象。

在期間,Spring還會載入兩項資料:

META-INF/spring.handlers
指定NamespaceHandler(實現org.springframework.beans.factory.xml.NamespaceHandler)介面,或使用org.springframework.beans.factory.xml.NamespaceHandlerSupport的子類。 META-INF/spring.schemas
在解析XML檔案時將XSD重新導向到本地檔案,避免在解析XML檔案時需要上網下載XSD檔案。通過現實org.xml.sax.EntityResolver介面來實現該功能。製作自訂的標籤

spring.handlers:

 

Xml代碼 http\://test.hatter.me/schema/test=me.hatter.test.TestNamespaceHandler

spring.schemas:

 

Xml代碼 http\://test.hatter.me/schema/test/test.xsd=META-INF/test.xsd

test.xsd:

 

Xml代碼 targetNamespace="http://test.hatter.me/schema/test">

me.hatter.test.TestNamespaceHandler:

 

Java代碼 packageme.hatter.test; importorg.springframework.beans.factory.xml.NamespaceHandlerSupport; publicclassTestNamespaceHandlerextendsNamespaceHandlerSupport{ publicvoidinit(){ registerBeanDefinitionParser("custom",newTestCustomBeanDefinitionParser()); } }

me.hatter.test.TestCustomBeanDefinitionParser:

 

Java代碼 packageme.hatter.test; importme.hatter.test.bean.TestBean; importorg.springframework.beans.factory.config.BeanDefinition; importorg.springframework.beans.factory.support.RootBeanDefinition; importorg.springframework.beans.factory.xml.BeanDefinitionParser; importorg.springframework.beans.factory.xml.ParserContext; importorg.w3c.dom.Element; publicclassTestCustomBeanDefinitionParserimplementsBeanDefinitionParser{ publicBeanDefinitionparse(Elementelement,ParserContextparserContext){ Stringid=element.getAttribute("id"); Stringname=element.getAttribute("name"); RootBeanDefinitionbeanDefinition=newRootBeanDefinition(); beanDefinition.setBeanClass(TestBean.class); beanDefinition.getPropertyValues().addPropertyValue("name",name); parserContext.getRegistry().registerBeanDefinition(id,beanDefinition); returnbeanDefinition; } }

 

測試代碼

test.xml:

 

Xml代碼 xmlns:test="http://test.hatter.me/schema/test" xsi:schemaLocation=" http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://test.hatter.me/schema/testhttp://test.hatter.me/schema/test/test.xsd">

me.hatter.test.main.Main:

Java代碼 packageme.hatter.test.main; importorg.springframework.context.ApplicationContext; importorg.springframework.context.support.ClassPathXmlApplicationContext; publicclassMain{ publicstaticvoidmain(String[]args){ Stringxml="classpath:me/hatter/test/main/test.xml"; ApplicationContextcontext=newClassPathXmlApplicationContext(newString[]{xml}); System.out.println(context.getBean("testCustom")); } }

上例輸出為:

TestBean[name=thisis a test custom tag]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.