您好,登錄后才能下訂單哦!
本篇內容主要講解“dubbo怎么實現spring自定義標簽”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“dubbo怎么實現spring自定義標簽”吧!
做dubbo的配置時很容易發現,dubbo有一套自己的標簽,提供給開發者配置,其實每一個標簽對應著一個 實體,在容器啟動的時候,dubbo會對所有的配置進行解析然后將解析后的內容設置到實體里,最終dubbo會根據實體中的值生成貫穿全局的統一URL。利用自定義標簽使配置簡單明了化,與spring完美融合。
下面自己寫一個自定義標簽,主要需要如下 幾個步驟:
1、編寫實體類
2、編寫Parser解析類
3、編寫NameSpaceHandle類
4、配置spring.handlers
5、配置spring.schemas
6、配置customTag .xsd
標簽實體類如下:
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();
}
}
標簽的解析類如下:
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) {
//通過配置文件獲取相應的值,設置到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類如下:
public class CustomTagNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
//實現init方法,解析CustomTag標簽
registerBeanDefinitionParser("customTag",new CustomTagBeanDefinitionParser(CustomTag.class,true));
}
}
spring.handlers配置,前面那一串其實可以隨便配置,只要一會和后面的配置一致即可
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對應了bean的屬性 -->
<xsd:element name="customTag" type="customTagType">
<xsd:annotation>
<xsd:documentation><![CDATA[ The customTag config ]]></xsd:documentation>
</xsd:annotation>
</xsd:element>
<!-- 配置各屬性值,有點像Mybatis配置對應的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>
最后測試
在新建一個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="技術" />
</beans>
在java代碼中測試
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());
}
}
輸出結果:
test
chewenliang
12
技術
bei jing
18618152379
spring的自定義標簽自己很容易實現,具體要看在實際項目中如何正確的實用它,接下來會記錄dubbo是如何解析、暴露服務。
到此,相信大家對“dubbo怎么實現spring自定義標簽”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。