大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本篇内容主要讲解“dubbo怎么实现spring自定义标签”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“dubbo怎么实现spring自定义标签”吧!
创新互联是一家专注于成都网站建设、网站设计与策划设计,江达网站建设哪家好?创新互联做网站,专注于网站建设十年,网设计领域的专业建站公司;建站业务涵盖:江达等地区。江达做网站价格咨询:028-86922220
做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的配置
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">
最后测试
在新建一个spring的配置文件如下
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">
在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自定义标签”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!