当前位置:首页 » 《资源分享》 » 正文

Dubbo源码解析:基于Extensible XML authoring 扩展Spring XML元素_小识的博客

13 人参与  2021年03月15日 14:56  分类 : 《资源分享》  评论

点击全文阅读


在这里插入图片描述

步骤

  1. 编写XML Schema文件,定义XML结构
  2. 自定义NamespaceHandler实现
  3. 自定义BeanDefinitionParser,将XML解析为BeanDefinition
  4. 注册到Spring容器

扩展Spring XML元素

项目结构如下
在这里插入图片描述

编写XML Schema文件

@Data
@ToString
public class User {

    private Long id;
    private String name;

}

users.xsd

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns="http://www.javashitang.com/schema/users"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.javashitang.com/schema/users">

    <xsd:import namespace="http://www.w3.org/XML/1998/namespace"/>

    <!-- 定义 User 类型(复杂类型) -->
    <xsd:complexType name="User">
        <xsd:attribute name="id" type="xsd:long" use="required"/>
        <xsd:attribute name="name" type="xsd:string" use="required"/>
    </xsd:complexType>


    <!-- 定义 user 元素 -->
    <xsd:element name="user" type="User"/>
</xsd:schema>

自定义NamespaceHandler实现

public class UsersNamespaceHandler extends NamespaceHandlerSupport {

    @Override
    public void init() {
        registerBeanDefinitionParser("user", new UserBeanDefinitionParser());
    }
}

自定义BeanDefinitionParser

public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {

    @Override
    protected Class<?> getBeanClass(Element element) {
        return User.class;
    }

    @Override
    protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
        setPropertyValue("id", element, builder);
        setPropertyValue("name", element, builder);
    }

    private void setPropertyValue(String attributeName, Element element, BeanDefinitionBuilder builder) {
        String attributeValue = element.getAttribute(attributeName);
        if (StringUtils.hasText(attributeValue)) {
            builder.addPropertyValue(attributeName, attributeValue);

        }
    }
}

注册到Spring容器

spring.handlers

## 定义 namespace 与 NamespaceHandler 的映射
http\://www.javashitang.com/schema/users=com.javashitang.UsersNamespaceHandler

spring.schemas

http\://www.javashitang.com/schema/users.xsd=META-INF/users.xsd

测试

users-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:users="http://www.javashitang.com/schema/users"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.javashitang.com/schema/users
        http://www.javashitang.com/schema/users.xsd">

    <users:user id="1" name="小识"/>

</beans>
public class ExtensibleXmlAuthoringDemo {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("META-INF/users-context.xml");
        context.refresh();
        // User(id=1, name=小识)
        System.out.println(context.getBean(User.class));
    }
}

参考博客

[1]https://www.jianshu.com/p/8639e5e9fba6
[2]https://www.cnblogs.com/lifullmoon/p/14449414.html


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/16295.html

自定义  定义  容器  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 完结文毁容的姐姐和瞎眼的我离开后,姜家两兄弟悔哭了+后续列表_完结文毁容的姐姐和瞎眼的我离开后,姜家两兄弟悔哭了+后续(林梦婉)
  • 妻子辱我爸受贿自杀,我掏出一等军功章节选推荐_[陈素云辰朋友]小说精彩章节分享
  • 全书浏览苔藓爬满旧日诺言新上(顾砚廷慕晚夏)_苔藓爬满旧日诺言新上(顾砚廷慕晚夏)全书结局
  • 顾尘傅雅宁(神女老婆,却在背地承欢作乐+后续+结局)结局_(顾尘傅雅宁神女老婆,却在背地承欢作乐+后续+结局全书结局)结局列表_笔趣阁(顾尘傅雅宁)
  • 「老婆怀上助理的孩子后,助理要求我净身出户」章节限时抢先看‌_「黄秋雅秋雅姐刘嘉铭」后续完结版
  • 此去经年人未还,沈青禾霍沉洲_此去经年人未还,沈青禾霍沉洲
  • 我爸娶了九十九个媳妇都死了,这次准备娶我的女同学小说精彩章节免费试读_[小梅娶媳妇孤儿]全文免费在线阅读
  • 此去经年人未还结局+番外文章简述(沈青禾霍沉洲)列表_此去经年人未还结局+番外文章简述
  • 完结文寻你寻不到归期结局+完结列表_完结文寻你寻不到归期结局+完结(姜昭意盛西)
  • 江以蓁的潮起时问归期高分佳作江以蓁秦司礼全书在线
  • 「亲手逼死儿子后,男人悔不当初」后续全文免费阅读_[傅司衍轩轩佳佳]最新章节免费阅读
  • (番外)+(全书)寻你寻不到归期+后续+结局(姜昭意盛西辞)全书在线_寻你寻不到归期+后续+结局免费列表_笔趣阁(姜昭意盛西辞)

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1