天天看點

spring 自定義标簽實作Spring 自定義标簽實作一、自定義Spring标簽簡介二、自定義标簽實作

Spring 自定義标簽實作

參考資料:

dubbo對spring自定義标簽的擴充的實作:https://my.oschina.net/lenglingx/blog/889662

一、自定義Spring标簽簡介

Spring官方文檔 42.1中,介紹了如何自定義Spring标簽,步驟如下:

1、編寫一個XML Schema描述您的自定義元素

2、編寫自定義命名空間處理程式實作,實作NamespackHandler接口

3、編寫一個或多個自定義的Bean定義解析器,實作BeanDefinitionParser接口

4、注冊到Spring容器

ps:官方文檔描述出處,打開 https://docs.spring.io/spring/docs/4.3.20.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#extensible-xml-introduction ,搜尋42.1即可

二、自定義标簽實作

1、編寫yf.xsd,名稱根據自己的喜好來寫即可

complexContent 該複雜類型隻包含元素或不包含任何元素内容(空)

attribute 該複雜類型包含指定的屬性。

annotation和documentation,結合起來,對定義的内容,做一定的文字說明

<xsd:annotation>
    <xsd:documentation>
        <![CDATA[ Namespace support for the yf services provided by yf framework. ]]></xsd:documentation>
</xsd:annotation>
           

1)定義一個type

<!-- xsd:complexType 定義一個type,及其屬性attribute -->
<xsd:complexType name="applicationType">
    <!-- 定義屬性id, id值唯一 -->
    <xsd:attribute name="id" type="xsd:ID">
        <xsd:annotation>
            <xsd:documentation><![CDATA[ The unique identifier for a bean. ]]></xsd:documentation>
        </xsd:annotation>
    </xsd:attribute>
</xsd:complexType>
           

2)定義标簽,并指定其引用哪個type,截取部分示例如下

<!-- 定義标簽application,并指明其對應的type,示例 <yf:application></yf:application> -->
<xsd:element name="application" type="applicationType">
    <xsd:annotation>
        <xsd:documentation><![CDATA[ The application config ]]></xsd:documentation>
    </xsd:annotation>
</xsd:element>
           

3)編寫标簽對應的實體類。(一個标簽對應一個實體類)

兩個标簽:applaction、registry,實體類對應Application、Registry

2、編寫标簽解析類(因為命名空間中需要用到此類)

ApplicationBeanDefinitionParser.java

RegistryBeanDefinitionParser.java

3、編寫命名空間處理類

public class YfNamespaceHandler extends NamespaceHandlerSupport {
    @Override
    public void init() {
        /**
         * registerBeanDefinitionDecorator(String elementName, BeanDefinitionDecorator dec)
         * 通過指定的BeandefiniParser解析xsd中定義的element元素
         */
        registerBeanDefinitionParser("application", new ApplicationBeanDefinitionParser(Application.class, true));
        registerBeanDefinitionParser("registry", new RegistryBeanDefinitionParser());
    }
}
           

4、編寫spring.handlers和spring.schemas串聯起所有部件,二者都存放在META-INF檔案夾下

spring.handlers指定NamespaceHandler,内容如下:

http\://yf.oschina.net/schema/yf= com.yf.springcustomtag.springsupport.YfNamespaceHandler
           

spring.schemas解析XML檔案時将xsd檔案重定向到本地檔案,避免從網上下載下傳xsd檔案(通過實作org.xml.sax.EntityResolver接口來實作該功能),内容如下:

http\://yf.oschina.net/schema/yf.xsd=META-INF/yf.xsd
           

5、測試

1)編寫spring配置applicationContext.xml,注意Beans中引入自定義标簽的xmlns資訊

xmlns:yf="http://yf.oschina.net/schema/yf"
http://yf.oschina.net/schema/yf http://yf.oschina.net/schema/yf.xsd
           

完整内容如下:

<?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:c="http://www.springframework.org/schema/c"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:yf="http://yf.oschina.net/schema/yf"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
      http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd
      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd

      http://yf.oschina.net/schema/yf http://yf.oschina.net/schema/yf.xsd">
    <!-- 上述Beans中須引用自定義标簽的xsd資訊 -->

    <yf:application id="applicationTest"  name="yfNameTest" version="1.0" />
    <yf:registry id="yfRegistry" address="127.0.0.1" port="8080" />
</beans>
           

2)編寫測試類

public class YfTagTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        Application application1 = (Application) context.getBean("applicationTest");
        System.out.println(application1.toString());

        Registry registry1 =  (Registry) context.getBean("yfRegistry");
        System.out.println(registry1.toString());

    }
}
           

3)運作測試類,結果如下,表明自定義标簽測試成功。

23:34:29.734 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
23:34:29.743 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source
23:34:29.752 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'applicationTest'
Application{id='applicationTest', name='yfNameTest', version='1.0'}
23:34:29.753 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'yfRegistry'
Registry{id='yfRegistry', address='127.0.0.1', port=8080}

Process finished with exit code 0
           

完整源碼位址:https://gitee.com/yyhcsfy/LearningDemo/tree/master/spring-custom-tag