In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you about how to customize the expansion mode of Schema in SpringBoot. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
SpringBoot Custom Schema extension
Recently, when writing the RPC framework, I used a custom xsd configuration. We are used to using bean to configure instantiated objects, so we take xsd as a separate approach.
1. Configure the ServiceConfig attribute @ Datapublic class ServiceConfig {/ * * interface * / private String interfaceClass; / * * reference * / private String ref; / * * version * / private String version @ Override public String toString () {return "ServiceConfig {" + "interfaceClass='" + interfaceClass +'\'+ ", ref='" + ref +'\'+ ", version='" + version +'\'+'}';} 2. Write XSD files
The name of the configured element is service, and the configured attribute corresponds to the attributes defined by ServiceConfig.
3. Write NamespaceHandler
The key of the registerBeanDefinitionParser method is the name of the element configured by xsd, indicating that the service element is parsed by the LinkServiceBeanDefinitionParser object
Public class LinkNamespaceHandler extends NamespaceHandlerSupport {@ Override public void init () {this.registerBeanDefinitionParser ("service", new LinkServiceBeanDefinitionParser ());}} 4. Write BeanDefinitionParser
Parse attributes from element and register with BeanDefinitionBuilder
Public class LinkServiceBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {@ Override protected Class getBeanClass (Element element) {return ServiceConfig.class;} @ Override protected void doParse (Element element, BeanDefinitionBuilder bean) {bean.addPropertyValue ("ref", element.getAttribute ("ref")); bean.addPropertyValue ("interfaceClass", element.getAttribute ("interfaceClass")); bean.addPropertyValue ("version", element.getAttribute ("version"));}} 5. Configure spring.handlers and spring.schemashttp\: / / gitee.com/schema/link=com.test.xsd.handler.LinkNamespaceHandlerhttp\: / / gitee.com/schema/link/link.xsd=META-INF/link.xsd6. Configure the load file for spring
Schema of link needs to be introduced in the header of the file.
7. Test public class App {public static void main (String [] args) {ApplicationContext context = new ClassPathXmlApplicationContext ("link-server.xml"); ServiceConfig config = (ServiceConfig) context.getBean ("serviceConfig"); System.out.println (config.toString ());}} Spring Schema extension mechanism 1. Overview
Starting with Spring2.0, Spring provides XML Schema extensible mechanism, and users can customize XML Schema files and customize them.
XML Bean parser, integrated into the Spring IOC container.
two。 Steps
Create a XML Schema file that describes the custom legitimate build module, the xsd file.
Customize the processor class and implement the NamespaceHandler interface.
Customize one or more parsers to implement the BeanDefinitionParser interface (the key part).
Register the above build to the Spring IOC container.
3. Examples are as follows
(1) Custom XML Schema file
Custom targetNamespace is http://www.liuenyuan.com/schema/myns,xmlns and namespaces must be consistent.
(2) Custom NamespaceHandler
There are only three methods for the NamespaceHandler interface
Init (): called before NamespaceHandler is used to complete initialization
Parse (): parse element
Decorate (): called when nesting elements
Spring provides a default implementation class, NamespaceHandlerSupport, which injects the parser for each element.
The concept of agency delegation is used. NamespaceHandlerSupport can register any BeanDefinitionParser and is responsible for all custom element orchestration, and the actual XML parsing is delegated to each BeanDefinitioParser.
(3) Custom BeanDefinitionParser
BeanDefinitionParser will be called if NamespapceHandler encounters that the element type already has a registered parser (for example, the above handler will be called if it encounters dateformat,DateformatDefinitionParser
Parsing the corresponding property settings to Bean) will be called. BeanDefinitionParser is responsible for parsing a top-level element.
Spring provides AbstractSingleBeanDefinitionParser to handle heavy parsing work.
GetBeanClass (): returns the element Class type
DoParse (): add element attributes or construction parameters
(4) Register handler and schema
Configure the implemented NamespaceHandler and xsd files into the specified configuration file. Located in the META-INF directory.
The spring.handlers file contains the xml schema uri and Handler class mappings.
Http\: / / www.liuenyuan.com/schema/myns=\ com.ley.springboot.schema.xml.MynsNamespaceHandler
When the http\: / / www.liuenyuan.com/schema/myns namespace is encountered, it will be handled by MynsNamespaceHandler. The key part must be consistent with the targetNamespace value in the xsd file.
Spring.schemas files contain xml schema xsd file namespaces and file path mapping relationships.
Http\: / / www.liuenyuan.com/schema/myns.xsd=META-INF/myns.xsd
(5) testing
NamespaceHandler implementation class
Public class MynsNamespaceHandler extends NamespaceHandlerSupport {@ Override public void init () {registerBeanDefinitionParser ("dateFormat", new MynsBeanDefinitionParser ());}}
BeaDefinitionParser implementation class
Import org.springframework.beans.factory.support.BeanDefinitionBuilder;import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;import org.w3c.dom.Element;import java.text.SimpleDateFormat;public class MynsBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {@ Override protected Class getBeanClass (Element element) {return SimpleDateFormat.class;} @ Override protected void doParse (Element element, BeanDefinitionBuilder builder) {String pattern = element.getAttribute ("pattern"); builder.addConstructorArgValue (pattern) }} above is how to customize the Schema extension method in SpringBoot shared by Xiaobian. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.