Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement spring Custom tags with dubbo

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "dubbo how to achieve spring custom tags", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "dubbo how to achieve spring custom tags" bar!

When configuring dubbo, it is easy to find that dubbo has its own set of tags for developers to configure. In fact, each tag corresponds to an entity. When the container starts, dubbo will parse all configurations and set the parsed content to the entity. Finally, dubbo will generate a unified URL that runs through the whole world according to the values in the entity. The use of custom tags to make the configuration simple and clear, and perfect integration with spring.

Write a custom tag by yourself, which mainly requires the following steps:

1. Write entity classes

2. Write Parser parsing class

3. Write NameSpaceHandle class

4. Configure spring.handlers

5. Configure spring.schemas

6. Configure customTag .xsd

The label entity classes are as follows:

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 ()

}

}

The parsing class of the tag is as follows:

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) {

/ / obtain the corresponding value through the configuration file and set it to the attribute of 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)

}

}

}

The NameSpaceHandle class is as follows:

Public class CustomTagNamespaceHandler extends NamespaceHandlerSupport {

@ Override

Public void init () {

/ / implement the init method to parse the CustomTag tag

RegisterBeanDefinitionParser ("customTag", new CustomTagBeanDefinitionParser (CustomTag.class,true))

}

}

Spring.handlers configuration. The front string can be configured freely, as long as it is consistent with the later configuration for a while.

Http\: / / www.51gitee.net/schema/customTag=springNameSpace.CustomTagNamespaceHandler

Spring.schemas configuration

Http\: / / www.51gitee.net/schema/customTag/customTag.xsd=META-INF/customTag.xsd

Configuration of customTag.xsd

Final test

The configuration file for creating a new spring is as follows

Testing in java code

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 ())

}

}

Output result:

Test

Chewenliang

twelve

Technical

Bei jing

18618152379

Spring's custom tag is easy to implement itself, depending on how to correctly apply it in the actual project, and then document how dubbo parses and exposes the service.

At this point, I believe that everyone on the "dubbo how to achieve spring custom tags" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report