dubbo自定义了很多xml标签,例如<dubbo:application>,那么这些自定义标签是怎么与spring结合起来的呢?我们先看一个简单的例子。

一 编写模型类

 package com.hulk.testdubbo.model;

 public class Hero {
private String name;
private int age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
}

二 定义xsd文件

 <xsd:schema
xmlns="http://hulk.com/schema"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://hulk.com/schema">
<xsd:complexType name="elementname1complexType">
<xsd:attribute name="name" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[ The elementname1 name. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="age" type="xsd:int">
<xsd:annotation>
<xsd:documentation><![CDATA[ The elementname1 age. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType> <xsd:element name="elementname1" type="elementname1complexType">
<xsd:annotation>
<xsd:documentation><![CDATA[ elementname1的文档 ]]></xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:schema>

说明:

  • 定义targetNamespace(目标命名空间),xmlns的值要与这个相同
  • xsd:element定义的就是将来会在xml文件中用到的元素,例如<dubbo:application>中的application
  • xsd:attribute定义的就是模型类中的属性,例如<dubbo:application name="xxx">中的name,并且可以指定属性类型,进而起到检测的作用(当我们定义的是int,如果在xml中的值是非int型的,直接会报错)。

三 编写spring.schemas

作用:该文件用来指定xsd文件的位置。

http\://hulk.com/schema/hero.xsd=META-INF/hero.xsd

注意:红色部分要与xsd文件中的targetNamespace相同。

四 编写BeanDefinition解析器

作用:主要用来解析自定义的xml标签。

 package com.hulk.testdubbo.schema;

 import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element; public class HeroBeanDefinitionParser implements BeanDefinitionParser {
private final Class<?> beanClass; public HeroBeanDefinitionParser(Class<?> beanClass) {
this.beanClass = beanClass;
} public BeanDefinition parse(Element element, ParserContext parserContext) {
RootBeanDefinition beanDefinition = new RootBeanDefinition();
beanDefinition.setBeanClass(beanClass);
beanDefinition.setLazyInit(false);
beanDefinition.getPropertyValues().add("name", element.getAttribute("name"));
beanDefinition.getPropertyValues().add("age", element.getAttribute("age"));
BeanDefinitionRegistry beanDefinitionRegistry = parserContext.getRegistry();
beanDefinitionRegistry.registerBeanDefinition(beanClass.getName(),beanDefinition);//注册bean到BeanDefinitionRegistry中
return beanDefinition;
}
}

五 编写命名空间处理器

作用:主要用来注册BeanDefinition解析器。

 package com.hulk.testdubbo.schema;

 import com.hulk.testdubbo.model.Hero;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport; public class HeroNamespaceHandler extends NamespaceHandlerSupport {
public void init() {
registerBeanDefinitionParser("elementname1", new HeroBeanDefinitionParser(Hero.class));
}
}

说明:通常为每一个xsd:element都要注册一个BeanDefinitionParser。

六 编写spring.handlers文件

作用:主要用于关联命名空间处理器和xsd中的targetNamespace。

http\://hulk.com/schema=com.hulk.testdubbo.schema.HeroNamespaceHandler

说明:key是xsd文件中的targetNamespace。

七 测试 - 编写hero.xml

 <?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:hero="http://hulk.com/schema"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://hulk.com/schema http://hulk.com/schema/hero.xsd">
<hero:elementname1 name="xiaona" age="18"/>
</beans>

说明:

  • xmlns:hero的value是xsd文件中的targetNamespace。
  • xmlns:hero可以写成xmlns:xxx,此时<hero:elementname1/>就要写成<xxx:elementname1/>

八 测试 - 编写测试主类

 package com.hulk.testdubbo.test;

 import com.hulk.testdubbo.model.Hero;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("hero.xml");
Hero hero = (Hero) applicationContext.getBean(Hero.class.getName());
System.out.println("name: " + hero.getName() + " age: " + hero.getAge());
}
}

如何在spring中自定义xml标签的方法就结束了。在实际中,随着注解和javaconfg的盛行,xml的方式渐渐的会淡出舞台,但是spring的启动流程还是会的。来看一下上述代码涉及到的流程。

  • 使用ResourceLoader将配置文件xml装载为Resource对象;
  • 使用BeanDefinitionReader解析配置信息:将每一个<bean>解析为一个BeanDefinition对象,然后存储到BeanDefinitionRegistry中
    • 实际上是BeanDefinitionReader调用BeanDefinitionParser进行了解析操作,解析完成后注册到BeanDefinitionRegistry(代码看上边的HeroBeanDefinitionParser)

6.1 如何在spring中自定义xml标签的更多相关文章

  1. 6.2 dubbo在spring中自定义xml标签源码解析

    在6.1 如何在spring中自定义xml标签中我们看到了在spring中自定义xml标签的方式.dubbo也是这样来实现的. 一 META_INF/dubbo.xsd 比较长,只列出<dubb ...

  2. Spring源码阅读笔记05:自定义xml标签解析

    在上篇文章中,提到了在Spring中存在默认标签与自定义标签两种,并且详细分析了默认标签的解析,本文就来分析自定义标签的解析,像Spring中的AOP就是通过自定义标签来进行配置的,这里也是为后面学习 ...

  3. web.xml中配置Spring中applicationContext.xml的方式

    2011-11-08 16:29 web.xml中配置Spring中applicationContext.xml的方式 使用web.xml方式加载Spring时,获取Spring applicatio ...

  4. ssh整合思想初步 struts2与Spring的整合 struts2-spring-plugin-2.3.4.1.jar下载地址 自动加载Spring中的XML配置文件 Struts2下载地址

    首先需要JAR包 Spring整合Structs2的JAR包 struts2-spring-plugin-2.3.4.1.jar 下载地址 链接: https://pan.baidu.com/s/1o ...

  5. Winform中自定义xml配置文件后对节点进行读取与写入

    场景 Winform中自定义xml配置文件,并配置获取文件路径: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100522648 ...

  6. Spring 框架的概述以及Spring中基于XML的IOC配置

    Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器. ...

  7. Spring中基于xml的AOP

    1.Aop 全程是Aspect Oriented Programming 即面向切面编程,通过预编译方式和运行期动态代理实现程序功能的同一维护的一种技术.Aop是oop的延续,是软件开发中的 一个热点 ...

  8. Dubbo源码-Dubbo是如何随心所欲自定义XML标签的

    叨叨 今天考虑了很久要不要写这篇文章. 距离<Dubbo源码>系列的开篇到现在已经快两个月时间了.当时是想着工作上的RPC框架使用存在一些让人头疼的问题,就来看看Dubbo给出了一套什么样 ...

  9. dubbo-config-spring自定义xml标签扩展

    要实现自定义自定义标签扩展,需要有如下步骤(在spring中定义了两个接口NamespaceHandler.BeanDefinitionParser,用来实现扩展) 1.设计配置属性和JavaBean ...

随机推荐

  1. Spring-Session实现Session共享入门教程

    任何一种技术的出现,都是来解决特定的问题的! 本篇开始学习Spring-Session相关的一些知识学习整理,让我们开始吧! Spring-Session介绍 Spring-Session使用的场景? ...

  2. 使用Synchronized块同步变量

    我们可以通过synchronized块来同步特定的静态或非静态方法.要想实现这种需求必须为这些特定的方法定义一个类变量,然后将这些方法的代码用synchronized块括起来,并将这个类变量作为参数传 ...

  3. [BZOJ3585]mex(莫队+分块)

    显然可以离线主席树,这里用莫队+分块做.分块的一个重要思想是实现修改与查询时间复杂度的均衡,这里莫队和分块互相弥补. 考虑暴力的分块做法,首先显然大于n的数直接忽略,于是将值域分成sqrt(n)份,每 ...

  4. 8.5 正睿暑期集训营 Day2

    目录 2018.8.5 正睿暑期集训营 Day2 总结 A.占领地区(前缀和) B.配对(组合) C 导数卷积(NTT) 考试代码 T1 T2 T3 2018.8.5 正睿暑期集训营 Day2 时间: ...

  5. KVM源代码解读:linux-3.17.4\include\linux\kvm_host.h

    #ifndef __KVM_HOST_H #define __KVM_HOST_H /* * This work is licensed under the terms of the GNU GPL, ...

  6. BZOJ2911 : [Poi1997]The Number of Symmetrical Choices

    新建源汇S,T,根据题意可以建出一个DAG 设f[x][y]为从x走到y的回文路径的方案数,则 边界条件: f[x][x]=1 对于一条边x->y,若a[x]==a[y],则f[x][y]=1 ...

  7. Redis如何处理客户端连接

    本文主要介绍了 Redis 处理客户端连接的一些内部实现机制,包括连接处理.超时.缓冲区等一系列内容. 注:本文所述内容基于 Redis2.6 及以上版本. 连接的建立 Redis 通过监听一个 TC ...

  8. FCKeditor如何升级CKEditor及使用方法

    之前编辑器用的是FCKeditor,因为项目原因需要升级为最新版本4.2.2,发现是已经更名为CKEditor. 百度了一下,据官方的解释,CK是对FCK的代码的完全重写. 项目环境是asp.net的 ...

  9. WAP 2.0开发XHTML MP语法及常用功能

    XHTML Mobile Profile 的基本结构 <?xml version="1.0" encoding="utf-8"?> <!DOC ...

  10. Unity3D对安卓盒子的支持

    一般的安卓盒子主要按键包含 1.方向键:上下左右 2.确认 3.返回 4.音量(Unity无法获取),须要在安卓层将事件发上来,KeyCode = 24,25 基本的函数是 if (Input.Get ...