欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring XML Schema擴(kuò)展機(jī)制的使用示例

 更新時間:2021年05月31日 11:33:52   作者:@希望就在前方  
所謂整合,即在Spring的框架下進(jìn)行擴(kuò)展,讓框架能無縫的與Spring工程配合使用。Spring設(shè)計了良好的擴(kuò)展的機(jī)制,本文將對Spring的擴(kuò)展方法及原理進(jìn)行簡單介紹。

前言

在當(dāng)前Java生態(tài),Spring算的上是最核心的框架,所有的開發(fā)組件想要得到大范圍更便捷的使用,都要和Spring進(jìn)行整合,比如我們熟知的Mybatis、Dubbo等,以及內(nèi)部封裝的各類組件包括Redis、MQ、配置中心等。

有了整合這一步,我們只需引入相應(yīng)的jar,比如mybatis-spring,然后進(jìn)行簡單的配置后即可在Spring工程中使用Mybatis的功能,也正是由于這樣的便捷性,導(dǎo)致很多時候我們沒有對其進(jìn)行深究。

XML Schema擴(kuò)展

打開mybatis-spring、dubbo的源碼會發(fā)現(xiàn)在META-INF目錄下有兩個文件(如下圖所示),spring.handlers與spring.schemas,這兩個文件就是XML Schema擴(kuò)展的關(guān)鍵入口點。

XSD

XSD,XML Schema Definition,XML定義。

XML Schema定義XML文檔的結(jié)構(gòu),XML Schema語言也稱為XML定義,即XSD。

簡單的說,XSD用于制定xml文件規(guī)范,包括xml中的元素(簡單元素、復(fù)雜元素)、屬性、以及屬性類型及約束等。

Spring XML Schema擴(kuò)展的第一步就是要定義一個xsd文件,比如spring-beans對應(yīng)xsd文件為http://www.springframework.org/schema/beans/spring-beans.xsd,如下圖:

為了簡單介紹Spring XML Schema擴(kuò)展實現(xiàn),下面將一個簡單例子(模擬一個簡單的分布式id生成器,不會實現(xiàn)具體功能)進(jìn)行說明,xsd定義如下(文件命名為DistributedId.xsd,在META-INF目錄下):

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.hexup.com/schema/distributed-id"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.hexup.com/schema/distributed-id">


    <xsd:element name="distributed-id">
        <xsd:complexType>
            <xsd:attribute name="id" type="xsd:string"></xsd:attribute>
            <xsd:attribute name="bizCode" type="xsd:string"></xsd:attribute>
            <xsd:attribute name="length" type="xsd:int"></xsd:attribute>
        </xsd:complexType>
    </xsd:element>
            
</xsd:schema>

上述xsd文件里定義了一個復(fù)雜元素distributed-id,包含屬性id,bizCode,length,形如:

<distributed-id id="xxx" bizCode="xxx" length="xxx"></distributed-id>

注意:xmlns,即為xml namespace,xml命名空間,后面跟的http鏈接地址可以不存在,因為xsd會放在當(dāng)前工程的META-INF下。

配置spring.handlers和spring.schemas

如下兩張圖所示,spring.schemas文件中用于說明xsd的文件路徑,spring.schemas文件用于說明解析此類xsd定義的標(biāo)簽的處理類,下面會對處理類進(jìn)行詳細(xì)說明。

NameSpaceHandler與BeanDefinitionParser

定義類DistributedIdNamespaceHandler繼承NamespaceHandlerSupport,init方法用于注冊BeanDefinition解析器,也就是解析xml中對應(yīng)標(biāo)簽為Spring Bean。

public class DistributedIdNamespaceHandler extends NamespaceHandlerSupport {

    @Override
    public void init() {
        registerBeanDefinitionParser("distributed-id", new DistributedIdParser());
    }
}

同時要創(chuàng)建BeanDefinitionParser

public class DistributedIdParser implements BeanDefinitionParser {


    @Override
    public BeanDefinition parse(Element element, ParserContext parserContext) {
        // 解析xml內(nèi)的標(biāo)簽
        String bizCode = element.getAttribute("bizCode");
        int length = Integer.valueOf(element.getAttribute("length"));
        String id = element.getAttribute("id");
        
        // 創(chuàng)建DistributedIdFactoryBean bean
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition();
        builder.getRawBeanDefinition().setBeanClass(DistributedIdFactoryBean.class);
        builder.setScope(BeanDefinition.SCOPE_SINGLETON);

        builder.addPropertyValue("bizCode", bizCode);
        builder.addPropertyValue("length", length);

        BeanDefinition beanDefinition = builder.getBeanDefinition();

        parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);

        return beanDefinition;
    }
}

其中DistributedIdFactoryBean實現(xiàn)FactoryBean接口用于創(chuàng)建DistributedIdComponent Bean,如下

public class DistributedIdFactoryBean implements InitializingBean, FactoryBean<DistributedIdComponent> {

    private String bizCode;
    private int length;

    private DistributedIdComponent distributedIdComponent;

    @Override
    public DistributedIdComponent getObject() throws Exception {
        return distributedIdComponent;
    }

    @Override
    public Class<?> getObjectType() {
        return DistributedIdComponent.class;
    }

    @Override
    public boolean isSingleton() {
        return false;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        distributedIdComponent = new DistributedIdComponent(bizCode, length);
    }

    public void setBizCode(String bizCode) {
        this.bizCode = bizCode;
    }

    public void setLength(int length) {
        this.length = length;
    }
}

目標(biāo)Bean DistributedIdComponent如下:

public class DistributedIdComponent {
    private String bizCode;
    private int length;

    public DistributedIdComponent() {

    }

    public DistributedIdComponent(String bizCode, int length) {
        this.bizCode = bizCode;
        this.length = length;
    }

    public String generateId() {
        System.out.println("mock generate id");
        return String.valueOf(System.currentTimeMillis()).substring(0, length);
    }

    public String getBizCode() {
        return bizCode;
    }

    public void setBizCode(String bizCode) {
        this.bizCode = bizCode;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }
}

使用

spring配置文件,spring-service.xml中配置distributed-id標(biāo)簽以及對應(yīng)的屬性值,如下:

<?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:distributed-id="http://www.hexup.com/schema/distributed-id"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd http://www.hexup.com/schema/distributed-id http://www.hexup.com/schema/distributed-id.xsd">


    <distributed-id:distributed-id id="test" bizCode="test" length="8"></distributed-id:distributed-id>
</beans>

運行容器驗證:

public class App {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-service.xml");
        DistributedIdComponent bean = context.getBean(DistributedIdComponent.class);

        String id = bean.generateId();

        System.out.println("id:" + id);
    }
}

總結(jié)

本文主要介紹了Spring XML Schema擴(kuò)展機(jī)制的使用方法,大致步驟為定義XSD文件、配置spring.schemas、編碼實現(xiàn)NameSpaceHanlder和BeanDefinitionParser實現(xiàn)類、配置spring.handlers。但未說明具體的實現(xiàn)原理,后續(xù)會有一篇文章詳細(xì)介紹Spring源碼是怎么實現(xiàn)擴(kuò)展的,以及介紹為什么使用FactoryBean來創(chuàng)建具體的Bean等問題。

以上就是Spring XML Schema擴(kuò)展機(jī)制的使用示例的詳細(xì)內(nèi)容,更多關(guān)于Spring XML Schema擴(kuò)展機(jī)制的使用的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • java多線程中線程封閉詳解

    java多線程中線程封閉詳解

    在本文里我們給大家分享了關(guān)于java多線程中線程封閉的知識點內(nèi)容以及用法,有需要讀者們可以參考下。
    2019-07-07
  • Java常用集合之Set和Map的用法詳解

    Java常用集合之Set和Map的用法詳解

    這篇文章將通過一些示例為大家詳細(xì)介紹一下Java常用集合中Set和Map的用法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-07-07
  • Java 1.8使用數(shù)組實現(xiàn)循環(huán)隊列

    Java 1.8使用數(shù)組實現(xiàn)循環(huán)隊列

    這篇文章主要為大家詳細(xì)介紹了Java 1.8使用數(shù)組實現(xiàn)循環(huán)隊列,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • Spring?Cloud負(fù)載均衡組件Ribbon原理解析

    Spring?Cloud負(fù)載均衡組件Ribbon原理解析

    本文主要講述了微服務(wù)體系下的?Spring?Cloud?Netflix?套件中?Ribbon?的使用,并結(jié)合部分源碼講述了?Ribbon?的底層原理,重點講述了?Ribbon?中是如何獲取服務(wù)以及如何判定一個服務(wù)是否可用,最后也介紹了?Ribbon?中默認(rèn)提供的?7?種負(fù)載均衡策略,感興趣的朋友一起看看吧
    2022-04-04
  • Spring實戰(zhàn)之Bean的作用域singleton和prototype用法分析

    Spring實戰(zhàn)之Bean的作用域singleton和prototype用法分析

    這篇文章主要介紹了Spring實戰(zhàn)之Bean的作用域singleton和prototype用法,結(jié)合實例形式分析了Bean的作用域singleton和prototype相關(guān)使用方法及操作注意事項,需要的朋友可以參考下
    2019-11-11
  • java多線程編程之使用runnable接口創(chuàng)建線程

    java多線程編程之使用runnable接口創(chuàng)建線程

    實現(xiàn)Runnable接口的類必須使用Thread類的實例才能創(chuàng)建線程,通過Runnable接口創(chuàng)建線程分為以下兩步
    2014-01-01
  • Java線程本地變量導(dǎo)致的緩存問題解決方法

    Java線程本地變量導(dǎo)致的緩存問題解決方法

    使用緩存可以緩解大流量壓力,顯著提高程序的性能,我們在使用緩存系統(tǒng)時,尤其是大并發(fā)情況下,經(jīng)常會遇到一些疑難雜癥,這篇文章主要給大家介紹了關(guān)于Java線程本地變量導(dǎo)致的緩存問題的解決方法,需要的朋友可以參考下,
    2024-08-08
  • 一文了解自定義MVC框架實現(xiàn)

    一文了解自定義MVC框架實現(xiàn)

    這篇文章主要為大家詳細(xì)介紹一下MVC框架自定義實現(xiàn)過程,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下
    2022-07-07
  • Java中類的初始化和實例化區(qū)別詳解

    Java中類的初始化和實例化區(qū)別詳解

    這篇文章主要介紹了Java中類的初始化和實例化區(qū)別詳解,類的初始化<BR>是完成程序執(zhí)行前的準(zhǔn)備工作,類的實例化(實例化對象)是指創(chuàng)建一個對象的過程,需要的朋友可以參考下
    2023-08-08
  • Spring Boot修改啟動端口的方法

    Spring Boot修改啟動端口的方法

    下面小編就為大家?guī)硪黄猄pring Boot修改啟動端口的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07

最新評論