Spring Custom Label Configuration

Source: Internet
Author: User

foreground : When you frequently use spring-dependent components, you find that you can inject the plug-in by customizing the spring tags, such as the configuration of the database source, the configuration of the MyBatis, and so on. So how are these spring tags custom configured? Learn the custom configuration of spring tags to make a technical reserve for implementing the Distributed service Framework later.

Technical Analysis : Spring's label configuration is implemented through XML, using XSD (XML Schema definition) to define elements, attributes, data types, and so on.

Spring Custom Label parsing

1.Spring starts by scanning the spring.handlers and Spring.schemas files under the Meta-inf file under the root directory to find the location of the processing class and the path to schemas.

spring.handlers

Http\://jewel.com/schema/jewel=com.jewel.spring.tag.handler.jewelnamespacehandler

 Spring.schemas

Http\://jewel.com/schema/jewel.xsd=meta-inf/jewel.xsd

2. Implement the namespace processing provider Class (Namespacehandlersupport): This class and the path in the spring.handlers correspond

 Public class Jewelnamespacehandler extends Namespacehandlersupport {    publicvoid  init () {        Registerbeandefinitionparser ("application"new Appbeandefinitionparser (appbeancnf.  Class));     }}

3. Implementing the Parsing Class (Appbeandefinitionparser) is responsible for parsing the label

public class Appbeandefinitionparser implements Beandefinitionparser {private class<?> Clssze;public Appbeandefinitionparser (class<?> cls) {this.clssze = CLS;} Public Beandefinition Parse (element element, ParserContext ParserContext) {//Where Element class is responsible for obtaining tag information
ParserContext is a spring context in which the bean can be registered to spring where it is responsible for injecting work}}

4.XSD file Configuration

jewel.xsd files: changing files and corresponding locations in Spring.schemas

<?xml version="1.0"encoding="UTF-8"?><Xsd:schema xmlns="Http://jewel.com/schema/jewel"xmlns:xsd="Http://www.w3.org/2001/XMLSchema"Xmlns:beans="Http://www.springframework.org/schema/beans"targetnamespace="Http://jewel.com/schema/jewel"elementFormDefault="qualified"attributeFormDefault="Unqualified"> <xsd:importnamespace="Http://www.springframework.org/schema/beans"/> <xsd:element name="Application"> <xsd:complexType> <xsd:complexContent> <xsd:extensionBase="Beans:identifiedtype"> <xsd:attribute name="name"Type="xsd:string"/> </xsd:extension> </xsd:complexContent> </xsd:complexType> < /xsd:element> </xsd:schema>

5. In the label configuration (beans.xml)

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" XM Lns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "Http://www.springframework.org/schema/aop" xmlns: p= "http://www.springframework.org/schema/p" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:context= " Http://www.springframework.org/schema/context "xmlns:jpa=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA "xmlns: Tool= "Http://www.springframework.org/schema/tool" xmlns:jdbc= "Http://www.springframework.org/schema/jdbc"xmlns:jewel= "Http://jewel.com/schema/jewel"-------------the designation of the namespacexsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ SPRING-TX.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA /spring-jpa.xsd Http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/ Spring-jdbc.xsdhttp://jewel.com/schema/jewel http://jewel.com/schema/jewel.xsd "----------path designation><!--Annotations Support-<jewel:application name= "Demo-app" ></jewel:application>-------------------label Configuration</beans>

  

Summary: with the above steps we can customize the application tag and can parse it through sping.

         

Spring Custom Label Configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.