最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

前言

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

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

XML Schema擴(kuò)展

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

XSD

XSD,XML Schema Definition,XML定義。

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

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

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

為了簡(jiǎn)單介紹Spring XML Schema擴(kuò)展實(shí)現(xiàn),下面將一個(gè)簡(jiǎn)單例子(模擬一個(gè)簡(jiǎn)單的分布式id生成器,不會(huì)實(shí)現(xiàn)具體功能)進(jìn)行說(shuō)明,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文件里定義了一個(gè)復(fù)雜元素distributed-id,包含屬性id,bizCode,length,形如:

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

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

配置spring.handlers和spring.schemas

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

NameSpaceHandler與BeanDefinitionParser

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

public class DistributedIdNamespaceHandler extends NamespaceHandlerSupport {

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

同時(shí)要?jiǎng)?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實(shí)現(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)簽以及對(duì)應(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>

運(yùn)行容器驗(yàn)證:

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、編碼實(shí)現(xiàn)NameSpaceHanlder和BeanDefinitionParser實(shí)現(xiàn)類、配置spring.handlers。但未說(shuō)明具體的實(shí)現(xiàn)原理,后續(xù)會(huì)有一篇文章詳細(xì)介紹Spring源碼是怎么實(shí)現(xiàn)擴(kuò)展的,以及介紹為什么使用FactoryBean來(lái)創(chuàng)建具體的Bean等問(wèn)題。

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

相關(guān)文章

  • Spring Security實(shí)現(xiàn)基于角色的訪問(wèn)控制框架

    Spring Security實(shí)現(xiàn)基于角色的訪問(wèn)控制框架

    Spring Security是一個(gè)功能強(qiáng)大的安全框架,提供了基于角色的訪問(wèn)控制、身份驗(yàn)證、授權(quán)等安全功能,可輕松保護(hù)Web應(yīng)用程序的安全,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧
    2023-04-04
  • Feign自定義重試策略及超時(shí)時(shí)間詳解

    Feign自定義重試策略及超時(shí)時(shí)間詳解

    這篇文章主要為大家介紹了Feign自定義重試策略及超時(shí)時(shí)間詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • Java String字符串補(bǔ)0或空格的實(shí)現(xiàn)代碼

    Java String字符串補(bǔ)0或空格的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Java String字符串補(bǔ)0或空格的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧
    2016-09-09
  • 最新評(píng)論

    三亚市| 修水县| 筠连县| 伊通| 阳朔县| 宁海县| 巴彦淖尔市| 永仁县| 河源市| 页游| 萝北县| 涟水县| 曲阜市| 上栗县| 沽源县| 托克托县| 宿迁市| 武义县| 吉安市| 鄂州市| 平顶山市| 酒泉市| 通州市| 淳安县| 阜康市| 河池市| 舒城县| 高邑县| 江津市| 广饶县| 桦川县| 泸定县| 苏州市| 永州市| 卓尼县| 陇南市| 五常市| 临沧市| 扎赉特旗| 凯里市| 洛南县|