The introduction
Since the advent of SpringBoot, the removal of Spring’s cumbersome XML configuration has freed up our hands for more focused brick lifting. I remember when I just learned Spring, I was tortured by various XMl configuration files of Spring every day. Every time a new framework was introduced, I was most worried about JAR conflicts, which configuration files were incorrectly configured, and the configuration files did not work. Therefore, every time you build a project, you can record the configuration file with small notes, so that you can copy it directly when you integrate the project next time. Let’s take a look at Spring’s integration with Dubbo
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="demo-provider"/>
<dubbo:registry address=Zookeeper: / / "127.0.0.1:2181"/>
<dubbo:protocol name="dubbo" port="20890"/>
<bean id="demoService" class="org.apache.dubbo.samples.basic.impl.DemoServiceImpl"/>
<dubbo:service interface="org.apache.dubbo.samples.basic.api.DemoService" ref="demoService"/>
</beans>
Copy the code
We have seen in the above code dubbo from defines a set of own label, dubbo: dubbo: application, the registry, dubbo: protocol, dubbo: service is our heart is a little small questions: How are these tags managed by Spring at the start of the Spring project? How is it identified by Spring? Would Spring recognize it if we arbitrarily defined a tag? We went to the Spring website and found that this is actually an extension of XML Schema support provided by Spring. As long as we follow its steps, we can configure any of our custom tags. What is the XML Schema extension mechanism? Here’s one that a lot of people may not have heard:
Spring provides an extension mechanism for XML-built applications to define and configure beans. It allows the consumer to write a custom XML bean parser and integrate the parser itself and the resulting beans into the Spring IOC container.
Spring.io /spring-fram… 10.2. XML Schema Authoring
How to implement a custom XML extension
There are four steps to implementing a custom XML Schema:
- Write an XML Schema file that describes your node elements.
- Write a NamespaceHandler implementation class
- Write one or more implementations of BeanDefinitionParser (a key step).
- Register the above schema and handler.
Since we only need to follow these four steps, let’s implement one ourselves by following this document.
Authoring the Schema
Write a javajr. XSD and put it in the resources/ meta-inf folder of your project (this can be any other way).
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tool="http://www.springframework.org/schema/tool"
xmlns="https://www.javajr.cn/schema/javajr"
targetNamespace="https://www.javajr.cn/schema/javajr">
<xsd:import namespace="http://www.springframework.org/schema/beans"/>
<xsd:element name="application">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="website" type="xsd:string" use="required"/>
<xsd:attribute name="weixin" type="xsd:string" use="required"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Copy the code
targetNamespace="https://www.javajr.cn/schema/javajr"
Here,targetNamespace
After the address useful to.- So what we’ve done here is we’ve defined an element application that has two attributes that are
website
andweixin
.
Write a NamespaceHandler
package org.spring.demo.schema; import org.springframework.beans.factory.xml.NamespaceHandlerSupport; Public class MyNamespaceHandler extends NamespaceHandlerSupport {@override public void init() { The name of the root element of the XSD is written above,<xsd:element name="application">registerBeanDefinitionParser("application", new MyBeanDefinitionParser()); }}Copy the code
This NamespaceHandler simply parses an XML node into an entity class in the IOC container. This means that the objects equivalent to the configuration in XML are managed through the Spring IOC container
Write the implementation class for BeanDefinitionParser
package org.spring.demo.schema; import org.spring.demo.domain.JavajrDomain; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.springframework.util.StringUtils; import org.w3c.dom.Element; public class MyBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { @Override protected Class<? > getBeanClass(Element element) { return JavajrDomain.class; } @Override protected void doParse(Element element, BeanDefinitionBuilder bean) { // this however is an optional property String website = element.getAttribute("website"); if (StringUtils.hasText(website)) { bean.addPropertyValue("website",website); } String weiXin = element.getAttribute("weixin"); if (StringUtils.hasText(weiXin)) { bean.addPropertyValue("weixin",weiXin); }}}Copy the code
The above implementation class simply does an assignment, and you can do it yourself if you need to have your own logical business. There is also a JavajrDomain, which is a simple Javabean that contains two properties, weixin and website.
Registering a Schema component
Finally, add two configuration files (spring.handler and spring.schema) to resources/ meta-INF directory:
resources/META-INF/spring.handlers
https\://www.javajr.cn/schema/javajr=org.spring.demo.schema.MyNamespaceHandler
Copy the code
resources/META-IN/spring.schemas
https\://www.javajr.cn/schema/javajr.xsd=META-INF/javajr.xsd
Copy the code
In this case, we can actually name them after the version number so that we can use multiple versions. This is how Spring-Beans work.
Test the custom schema
- in
resources
Create a new one in the directoryapplicationContext.xml
file
This file is to use our own customschema
The important thing to note in this file is the red lines above, usually if we have introduced a third party framework, such asmq
, ordubbo
And they all have custom things like that.
- Write a startup class
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
JavajrDomain bean = ctx.getBean(JavajrDomain.class);
System.out.println(bean.toString());
}
Copy the code
We can see the console output
JavajrDomain{weixin='javajr8', website='javajr.cn'}
Copy the code
- Here’s one that we implemented ourselves
XML schema
Finished, it is very simple, it is ok to want to light on official document only. There may be several small details that need to be paid attention to during the process of wankingXML schema
You need to pay attention to the space, or some special symbols.
This code has already been submitted in gitee https://gitee.com/javajr/spring-schema-demo interested friends can directly download the run, but it is not recommended to play, so it is better to try myself, after all, has four steps, according to the document.
XML Schema extension in Dubbo
We had an introduction at the beginning of the articledubbo
The custom ofXML schema
Let’s open it togetherdubbo
Source code to see how it is implemented, see the screenshot below, also according to those four steps.
The starter SpringBoot
Now with SpringBoot, most of the previous XML Schema configuration frameworks have a corresponding starter for encapsulation. Starter is much easier to use than XML Schema, out of the box, do not need to write many configuration files. If you’re not familiar with SpringBoot’s starter recommendations, check out these two articles: Can you explain SpringBoot’s automatic assembly principles? Nanny tutorials, hand in hand to teach you to achieve a SpringBoot starter.
conclusion
Although XML schema extension with not much now, but should also have older in the use of the project, if is old project need to introduce a kind of framework, we need at least need to know how to introduce, although there are many articles online can draw lessons from, but we also should know much about it. Instead of just copying the configuration file. We should know why we need to copy this XSD and why we don’t have this XSD.
The end of the
- Due to their lack of knowledge, it is hard to avoid mistakes, if you find the wrong place, also hope to leave a message to me to point out, I will correct it.
- If you think the article is good, your retweet, share, praise, like, comment is the biggest encouragement to me.
- Thank you for reading. I very much welcome and thank you for your attention.
Javajr.cn docs.spring. IO /spring-fram… www.cnkirito.moe/spring-xsd/…