一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 基于Spring開發(fā)之自定義標(biāo)簽及其解析

基于Spring開發(fā)之自定義標(biāo)簽及其解析

2020-09-17 15:43lizo Java教程

Spring框架是現(xiàn)在Java最流行的開源框架之一,需要實(shí)現(xiàn)一些自定義的標(biāo)簽,主要是方便使用我們框架的人能夠快速、簡單進(jìn)行配置,有興趣的可以了解一下。

Spring框架是現(xiàn)在Java最流行的開源框架之一,并且Spring下的各種子項(xiàng)目對某些特定問題的解決有很好的支持。因此,如果能在Spring 基礎(chǔ)上實(shí)現(xiàn)搭建自己的一套框架(基于XML配置)。就必然需要實(shí)現(xiàn)一些自定義的標(biāo)簽,主要是方便使用我們框架的人能夠快速、簡單進(jìn)行配置。

1. XML Schema

要想自定義標(biāo)簽,首先第一步需要寫自己的XML Schema。XML Schema的個人感覺比較復(fù)雜,網(wǎng)上的教程比較簡單,因此可以參照spring-beans.xsd依葫蘆畫瓢。這里就按照我自己的理解進(jìn)行簡單介紹一下吧。

1.1 最簡單的標(biāo)簽

一個最簡單的標(biāo)簽,形式如:

?
1
<bf:head-routing key="1" value="1" to="test2"/>

該標(biāo)簽只包含了若干屬性,我們就在xsd文件中這么定義

?
1
2
3
4
5
6
7
8
9
10
<!-- 聲明一個標(biāo)簽,名字為head-routing,他的類型為headRouting-->
<xsd:element name="head-routing" type="headRouting"></xsd:element>
 
  <!-- 定義head-routing的類型,這里定義它有key,value,to,patten四個屬性 -->
  <xsd:complexType name="headRouting">
    <xsd:attribute name="key" type="xsd:string" use="required"></xsd:attribute>
    <xsd:attribute name="value" type="xsd:string" use="required"></xsd:attribute>
    <xsd:attribute name="to" type="xsd:IDREF" use="required"></xsd:attribute>
    <xsd:attribute name="patten" type="xsd:string" default="string"></xsd:attribute>
  </xsd:complexType>

在<xsd:attribute>標(biāo)簽中的type是用來定義該屬性的格式,例如

  1. xsd:string 表示是一個字符串,對格式?jīng)]什么要求

  2. xsd:id 表示該屬性的值是一個id,有格式要求(例如不能以數(shù)字開頭)。

  3. xsd:IDREF 表示該屬性的值與某xsd:id屬性的值對應(yīng)

  4. 其他還有很多,例如number,double,datetime等等。

1.2 復(fù)雜點(diǎn)的標(biāo)簽

所謂復(fù)雜,其實(shí)就是嵌套的標(biāo)簽,形式如:

?
1
2
3
<bf:stop id="test1" ref="testNode">
  <bf:head-routing key="1" value="1" to="test2"/>
</bf:stop>

其實(shí)只要參照Spring 中<bean>標(biāo)簽的xsd依葫蘆畫瓢,首先是定義stop標(biāo)簽

?
1
2
3
4
5
6
7
8
9
10
<xsd:element name="stop">
  <xsd:complexType>
    <xsd:complexContent>
      <xsd:extension base="beans:identifiedType">
        <xsd:group ref="stopElements"/>
        <xsd:attributeGroup ref="stopAttributes"/>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>
</xsd:element>

其中,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<xsd:extension base="beans:identifiedType"> 定義了該標(biāo)簽的id屬性,注意這里引用的是spring-beans中的type,
<xsd:group ref="stopElements"/>中定義了<bf:stop>標(biāo)簽允許的子標(biāo)簽
<xsd:attributeGroup ref="stopAttributes"/> 定義了<bf:stop>標(biāo)簽允許的屬性
  <xsd:group name="stopElements">
    <xsd:sequence>
      <xsd:element ref="description" minOccurs="0"/>
      <xsd:choice minOccurs="0" maxOccurs="unbounded">
        <xsd:element ref="head-routing"/>
         <!-- 有更多的子標(biāo)簽繼續(xù)在這里添加,例如<xsd:element ref="properties"/> -->
      </xsd:choice>
    </xsd:sequence>
  </xsd:group>
 
 
  <xsd:attributeGroup name="stopAttributes">
    <xsd:attribute name="ref" type="xsd:IDREF" use="required">
      <xsd:annotation>        
        <xsd:appinfo>
           <!-- 這里是使用了Spring tool xsd中的標(biāo)簽,格式校驗(yàn)-->
          <tool:annotation kind="ref">
            <tool:expected-type type="com.lizo.node.Station"/>
          </tool:annotation>
        </xsd:appinfo>
      </xsd:annotation>
    </xsd:attribute>
    <!-- 有更多的子標(biāo)簽繼續(xù)在這里添加,例如<xsd:attribute name="value" type="xsd:string"/> -->

2. 配置文件

完成了xsd文件編寫后,還需要讓該文件生效,就需要在項(xiàng)目的resource/META-INF包里面配置2個文件spring.handlers和spring.schemas

2.1 spring.schemas

改配置文件主要是用一個url來映射我們第一步配置好的文件,形式如下

http\://www.lizo.com/schema/bf.xsd=META-INF/bf.xsd

這樣,就可以在Spring的xml配置文件中加入spring.schemas的url,省略掉其他的,在<beans>標(biāo)簽中增加如下信息

?
1
2
3
4
5
6
7
8
<beans
    ..
    xmlns:bf="http://www.lizo.com/schema/bf"
    xsi:schemaLocation="
    ...
    http://www.lizo.com/schema/bf
    http://www.lizo.com/schema/bf.xsd
    ">

完成這步以后,就可以在xml中寫自己的標(biāo)簽了,例如自定義標(biāo)簽的namespace為bf,

?
1
2
3
4
<bf:stop id="test123" ref="testNode">
  <bf:head-routing key="1" value="1" to="test1"/>
  <bf:head-routing key="3" value="4" to="test2"/>
</bf:stop>

2.2 spring.handlers

這個配置文件用來配置解析我們bf標(biāo)簽,然后生成一些BeanDefinition進(jìn)行注冊。例如

http\://www.lizo.com/schema/bf=com.lizo.config.BusinessFlowNamespaceHandlerSupport

其中 BusinessFlowNamespaceHandlerSupport就是我們用來解析標(biāo)簽

3. 自定義標(biāo)簽解析

在上一步中,我們配置了com.lizo.config.BusinessFlowNamespaceHandlerSupport類作為解析自定義標(biāo)簽的類,所以namespace為bf的標(biāo)簽,都會用這里注冊的標(biāo)簽解析器來解析

?
1
2
3
4
5
6
public class BusinessFlowNamespaceHandlerSupport extends NamespaceHandlerSupport {
  public void init() {
    //注冊用于解析<bf:stop>的解析器
    registerBeanDefinitionParser("stop", new BusinessFlowBeanDefinitionParser());
  }
}

我們自定義的標(biāo)簽解析器BusinessFlowBeanDefinitionParser是要實(shí)現(xiàn)BeanDefinitionParser 接口的

?
1
2
3
public interface BeanDefinitionParser {
  BeanDefinition parse(Element element, ParserContext parserContext);
}

一般來說,注冊bean的基本流程為:

  1. 解析標(biāo)簽

  2. 根據(jù)解析的值生成BeanDefinition,

  3. 注冊標(biāo)簽

解析標(biāo)簽就不用說,重點(diǎn)說說怎么生成BeanDefinition

3.1 生成BeanDefinition

一個最簡單的BeanDefinition通過設(shè)置Class和屬性的注入就可以完成。如下:

?
1
2
3
4
5
RootBeanDefinition nodeWrapDefinition = new RootBeanDefinition();
//該BeanDefinition對應(yīng)的是什么類
nodeWrapDefinition.setBeanClass(StationRoutingWrap.class);
//name是解析標(biāo)簽后獲得的值
nodeWrapDefinition.getPropertyValues().addPropertyValue("name", name);

RuntimeBeanReference

RuntimeBeanReference 用于在運(yùn)行時去獲取BeanDefinition,因?yàn)樵谖覀儎?chuàng)建這個BeanDefinition的時候我們只知道他的beanName,并不確定是否已經(jīng)注冊了,這個時候就需要用RuntimeBeanReference,例如

?
1
2
RuntimeBeanReference refBean = new RuntimeBeanReference(ref);
    nodeWrapDefinition.getPropertyValues().addPropertyValue("station", refBean);

集合類BeanDefinition

某個BeanDefinition注入的屬性為一個List,這個時候就需要用ManagedList(同理有ManagedMap,ManagedSet),

?
1
2
3
ManagedList routingConditions = new ManagedList();
....
nodeWrapDefinition.getPropertyValues().add("routing", routing);

3.2 注冊bean

注冊BeanDefinitionParser 接口的函數(shù)中有個參數(shù)ParserContext,有個方法為getRegistry(),因此,注冊bean的時候就很簡單了

 

復(fù)制代碼 代碼如下:

parserContext.getRegistry().registerBeanDefinition("beanName",nodeWrapDefinition);

 

 

總結(jié)

通過以上三步,就可以實(shí)現(xiàn)自己定義標(biāo)簽,并且在Spring容器中注入相關(guān)的bean。讓我們的框架使用起來更方便

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://my.oschina.net/u/3039671/blog/875325

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩在线观看免费 | 亚洲精品成人在线 | 草莓视频榴莲视频 | 国内会所按摩推拿国产 | 国产日日操 | 亚洲上最大成网人站4438 | 白丝校花掀起短裙呻吟小说 | 精品国偷自产在线 | 日韩精品一区二区三区中文版 | 日本日日黄 | 海派甜心完整版在线观看 | 亚洲天天做夜夜做天天欢 | 96萝莉| 67194在线免费观看 | 欧美黑大吊 | 亚洲天堂视频在线免费观看 | 亚洲欧美综合区自拍另类 | 动漫美女羞羞 | 欧美日韩视频在线第一区二区三区 | 果冻传媒九一制片厂网站 | 日本肥熟| 欧美理论片手机在线观看片免费 | 午夜影视在线观看 | 亚洲福利精品电影在线观看 | 欧美va在线 | 四虎网址 | 1024人成网色www | 午夜福利体验免费体验区 | 欧美成人三级伦在线观看 | 欧美特黄三级在线观看 | 亚洲视频一区网站 | 欧美日韩一区二区综合 | 爱情岛论坛亚洲一号路线 | 美国玩尿眼道videos | 四虎影免看黄 | 日本久久啪啪婷婷激情五月 | 成人午夜在线视频 | 免费在线观看网址入口 | 成人看的羞羞视频免费观看 | 国产麻豆精品视频 | 国产精品va在线观看手机版 |