日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 自定义xml_6.1 如何在spring中自定义xml标签

發(fā)布時間:2023/12/10 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 自定义xml_6.1 如何在spring中自定义xml标签 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

dubbo自定義了很多xml標(biāo)簽,例如,那么這些自定義標(biāo)簽是怎么與spring結(jié)合起來的呢?我們先看一個簡單的例子。

一 編寫模型類

1 packagecom.hulk.testdubbo.model;2

3 public classHero {4 privateString name;5 private intage;6

7 publicString getName() {8 returnname;9 }10

11 public voidsetName(String name) {12 this.name =name;13 }14

15 public intgetAge() {16 returnage;17 }18

19 public void setAge(intage) {20 this.age =age;21 }22 }

二 定義xsd文件

1

3 xmlns:xsd="http://www.w3.org/2001/XMLSchema"

4 targetNamespace="http://hulk.com/schema">

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

說明:

定義targetNamespace(目標(biāo)命名空間),xmlns的值要與這個相同

xsd:element定義的就是將來會在xml文件中用到的元素,例如中的application

xsd:attribute定義的就是模型類中的屬性,例如中的name,并且可以指定屬性類型,進(jìn)而起到檢測的作用(當(dāng)我們定義的是int,如果在xml中的值是非int型的,直接會報錯)。

三 編寫spring.schemas

作用:該文件用來指定xsd文件的位置。

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

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

四 編寫B(tài)eanDefinition解析器

作用:主要用來解析自定義的xml標(biāo)簽。

1 packagecom.hulk.testdubbo.schema;2

3 importorg.springframework.beans.factory.config.BeanDefinition;4 importorg.springframework.beans.factory.support.BeanDefinitionRegistry;5 importorg.springframework.beans.factory.support.RootBeanDefinition;6 importorg.springframework.beans.factory.xml.BeanDefinitionParser;7 importorg.springframework.beans.factory.xml.ParserContext;8 importorg.w3c.dom.Element;9

10 public class HeroBeanDefinitionParser implementsBeanDefinitionParser {11 private final Class>beanClass;12

13 public HeroBeanDefinitionParser(Class>beanClass) {14 this.beanClass =beanClass;15 }16

17 publicBeanDefinition parse(Element element, ParserContext parserContext) {18 RootBeanDefinition beanDefinition = newRootBeanDefinition();19 beanDefinition.setBeanClass(beanClass);20 beanDefinition.setLazyInit(false);21 beanDefinition.getPropertyValues().add("name", element.getAttribute("name"));22 beanDefinition.getPropertyValues().add("age", element.getAttribute("age"));23 BeanDefinitionRegistry beanDefinitionRegistry =parserContext.getRegistry();24 beanDefinitionRegistry.registerBeanDefinition(beanClass.getName(),beanDefinition);//注冊bean到BeanDefinitionRegistry中

25 returnbeanDefinition;26 }27 }

五 編寫命名空間處理器

作用:主要用來注冊BeanDefinition解析器。

1 packagecom.hulk.testdubbo.schema;2

3 importcom.hulk.testdubbo.model.Hero;4 importorg.springframework.beans.factory.xml.NamespaceHandlerSupport;5

6 public class HeroNamespaceHandler extendsNamespaceHandlerSupport {7 public voidinit() {8 registerBeanDefinitionParser("elementname1", new HeroBeanDefinitionParser(Hero.class));9 }10 }

說明:通常為每一個xsd:element都要注冊一個BeanDefinitionParser。

六 編寫spring.handlers文件

作用:主要用于關(guān)聯(lián)命名空間處理器和xsd中的targetNamespace。

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

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

七 測試 - 編寫hero.xml

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

4 xmlns:hero="http://hulk.com/schema"

5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd6 http://hulk.com/schema http://hulk.com/schema/hero.xsd">

7

8

說明:

xmlns:hero的value是xsd文件中的targetNamespace。

xmlns:hero可以寫成xmlns:xxx,此時就要寫成

八 測試 - 編寫測試主類

1 packagecom.hulk.testdubbo.test;2

3 importcom.hulk.testdubbo.model.Hero;4 importorg.springframework.context.ApplicationContext;5 importorg.springframework.context.support.ClassPathXmlApplicationContext;6

7 public classMain {8 public static voidmain(String[] args) {9 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("hero.xml");10 Hero hero = (Hero) applicationContext.getBean(Hero.class.getName());11 System.out.println("name: " + hero.getName() + " age: " +hero.getAge());12 }13 }

如何在spring中自定義xml標(biāo)簽的方法就結(jié)束了。在實際中,隨著注解和javaconfg的盛行,xml的方式漸漸的會淡出舞臺,但是spring的啟動流程還是會的。來看一下上述代碼涉及到的流程。

使用ResourceLoader將配置文件xml裝載為Resource對象;

使用BeanDefinitionReader解析配置信息:將每一個解析為一個BeanDefinition對象,然后存儲到BeanDefinitionRegistry中

實際上是BeanDefinitionReader調(diào)用BeanDefinitionParser進(jìn)行了解析操作,解析完成后注冊到BeanDefinitionRegistry(代碼看上邊的HeroBeanDefinitionParser)

總結(jié)

以上是生活随笔為你收集整理的java 自定义xml_6.1 如何在spring中自定义xml标签的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。