本篇內(nèi)容主要講解“dubbo怎么實(shí)現(xiàn)spring自定義標(biāo)簽”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“dubbo怎么實(shí)現(xiàn)spring自定義標(biāo)簽”吧!
創(chuàng)新互聯(lián)是一家專(zhuān)注于成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)與策劃設(shè)計(jì),江達(dá)網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)十年,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:江達(dá)等地區(qū)。江達(dá)做網(wǎng)站價(jià)格咨詢(xún):028-86922220
做dubbo的配置時(shí)很容易發(fā)現(xiàn),dubbo有一套自己的標(biāo)簽,提供給開(kāi)發(fā)者配置,其實(shí)每一個(gè)標(biāo)簽對(duì)應(yīng)著一個(gè) 實(shí)體,在容器啟動(dòng)的時(shí)候,dubbo會(huì)對(duì)所有的配置進(jìn)行解析然后將解析后的內(nèi)容設(shè)置到實(shí)體里,最終dubbo會(huì)根據(jù)實(shí)體中的值生成貫穿全局的統(tǒng)一URL。利用自定義標(biāo)簽使配置簡(jiǎn)單明了化,與spring完美融合。
下面自己寫(xiě)一個(gè)自定義標(biāo)簽,主要需要如下 幾個(gè)步驟:
1、編寫(xiě)實(shí)體類(lèi)
2、編寫(xiě)Parser解析類(lèi)
3、編寫(xiě)NameSpaceHandle類(lèi)
4、配置spring.handlers
5、配置spring.schemas
6、配置customTag .xsd
標(biāo)簽實(shí)體類(lèi)如下:
public class CustomTag {
private String id;
private String name;
private Integer age;
private String profession;
private String address;
private String phone;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String toString(){
StringBuffer sb = new StringBuffer();
sb.append(id + "\n");
sb.append(name + "\n");
sb.append(age + "\n");
sb.append(profession + "\n");
sb.append(address + "\n");
sb.append(phone + "\n");
return sb.toString();
}
}
標(biāo)簽的解析類(lèi)如下:
public class CustomTagBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
private final Class<?> beanClass;
private final boolean required;
public CustomTagBeanDefinitionParser (Class<?> beanClass, boolean required) {
this.beanClass = beanClass;
this.required = required;
}
protected Class getBeanClass(Element element) {
return CustomTag.class;
}
protected void doParse(Element element, BeanDefinitionBuilder builder) {
//通過(guò)配置文件獲取相應(yīng)的值,設(shè)置到bean的屬性中
String id = element.getAttribute("id");
String name = element.getAttribute("name");
String age = element.getAttribute("age");
String profession = element.getAttribute("profession");
String address = element.getAttribute("address");
String phone = element.getAttribute("phone");
if (StringUtils.hasText(id)) {
builder.addPropertyValue("id", id);
}
if (StringUtils.hasText(name)) {
builder.addPropertyValue("name", name);
}
if (StringUtils.hasText(age)) {
builder.addPropertyValue("age", age);
}
if (StringUtils.hasText(profession)) {
builder.addPropertyValue("profession", profession);
}
if (StringUtils.hasText(address)) {
builder.addPropertyValue("address", address);
}
if (StringUtils.hasText(phone)) {
builder.addPropertyValue("phone", phone);
}
}
}
NameSpaceHandle類(lèi)如下:
public class CustomTagNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
//實(shí)現(xiàn)init方法,解析CustomTag標(biāo)簽
registerBeanDefinitionParser("customTag",new CustomTagBeanDefinitionParser(CustomTag.class,true));
}
}
spring.handlers配置,前面那一串其實(shí)可以隨便配置,只要一會(huì)和后面的配置一致即可
http\://www.51gitee.net/schema/customTag=springNameSpace.CustomTagNamespaceHandler
spring.schemas配置
http\://www.51gitee.net/schema/customTag/customTag.xsd=META-INF/customTag.xsd
customTag.xsd的配置
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
xmlns="http://www.51gitee.net/schema/customTag"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://www.51gitee.net/schema/customTag"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans" />
<!-- 定義element名, customTagType對(duì)應(yīng)了bean的屬性 -->
<xsd:element name="customTag" type="customTagType">
<xsd:annotation>
<xsd:documentation><![CDATA[ The customTag config ]]></xsd:documentation>
</xsd:annotation>
</xsd:element>
<!-- 配置各屬性值,有點(diǎn)像Mybatis配置對(duì)應(yīng)的model -->
<xsd:complexType name="customTagType">
<xsd:attribute name="id" type="xsd:ID">
<xsd:annotation>
<xsd:documentation><![CDATA[ The unique identifier for a bean. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="name" type="xsd:string" use="required">
<xsd:annotation>
<xsd:documentation><![CDATA[ The customTag name. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="age" type="xsd:int">
<xsd:annotation>
<xsd:documentation><![CDATA[ The customTag age. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="profession" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[ The customTag profession. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="address" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[ The customTag address. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="phone" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[ The customTag phone. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
</xsd:schema>
最后測(cè)試
在新建一個(gè)spring的配置文件如下
<?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:common="http://www.51gitee.net/schema/customTag"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.oschina.net/schema/customTag
http://www.oschina.net/schema/customTag/customTag.xsd">
<common:customTag id="test" name="chewenliang" address="bei jing" age="12" phone="18618152379" profession="技術(shù)" />
</beans>
在java代碼中測(cè)試
public class TestNameSpace {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-test.xml");
CustomTag customTag= (CustomTag) context.getBean("test");
System.out.println(customTag.toString());
}
}
輸出結(jié)果:
test
chewenliang
12
技術(shù)
bei jing
18618152379
spring的自定義標(biāo)簽自己很容易實(shí)現(xiàn),具體要看在實(shí)際項(xiàng)目中如何正確的實(shí)用它,接下來(lái)會(huì)記錄dubbo是如何解析、暴露服務(wù)。
到此,相信大家對(duì)“dubbo怎么實(shí)現(xiàn)spring自定義標(biāo)簽”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!
分享標(biāo)題:dubbo怎么實(shí)現(xiàn)spring自定義標(biāo)簽
鏈接URL:http://bm7419.com/article46/jdghhg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開(kāi)發(fā)、網(wǎng)站內(nèi)鏈、網(wǎng)站收錄、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)公司、微信公眾號(hào)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)