Spring Source Analysis (iv)--use of custom tags

Source: Internet
Author: User

In the previous code analysis, the parsing of the spring tag was divided into the default label and the custom label, and the previous article analyzed the default label parsing process in spring.

This article will analyze the use of custom labels in spring:

One: Review

 Public classDefaultbeandefinitiondocumentreaderImplementsBeandefinitiondocumentreader {     /*** Parse The elements at the root level in the document: * "import", "Alias", "Bean". * @paramroot the DOM root element of the document*/    protected voidparsebeandefinitions (Element root, beandefinitionparserdelegate delegate) {if(Delegate.isdefaultnamespace (root)) {NodeList nl=root.getchildnodes ();  for(inti = 0; I < nl.getlength (); i++) {node node=Nl.item (i); if(nodeinstanceofElement) {Element Ele=(Element) node; if(Delegate.isdefaultnamespace (ele)) {parsedefaultelement (ele, delegate); }                    Else{delegate.                     Parsecustomelement(ele); }                }            }        }        Else{delegate.         Parsecustomelement(root); }    }}

In the Defaultbeandefinitiondocumentreader class, the default labels and custom labels are resolved differently.

Two: Custom Label use

In many cases, we need to provide configurable support for the system, which can be configured directly on the basis of spring's standard beans, but when the configuration is more complex or requires more rich control, it becomes awkward. The usual approach would be to parse the defined XML file in a primitive way and then convert it into a configuration object. This approach can certainly solve all problems, but it is cumbersome to implement, especially when the configuration is very complex, parsing is a burden to be considered. Spring provides support for extensible schemas, which is a good compromise, and it takes a few steps to extend the Spring Custom label configuration (provided the spring core package is added to the project).

    • Create a component that needs to be extended
    • Defining an XSD file describing component content
    • Create a file that implements the Beandefinitionparser interface for parsing definitions and component definitions in XSD files
    • Create a handler file, extended from Namespacehandlersupport, to register the component with the spring container.
    • Write Spring.handlers and Spring.schemas files.

(1) Create a component that needs to be extended

(2) Define an XSD file to describe the component content

(3) Create a file that implements the Beandefinitionparser interface for parsing definitions and component definitions in XSD files

(4) Create a handler file, extended from Namespacehandlersupport, to register the component with the spring container.

(5) Write spring.handlers and Spring.schemas files, the default location is under the/meta-inf/folder of the project, of course, you can change the path by the way of spring extension or modify the source code.

(6) Create a test configuration file, and after introducing the corresponding namespace and XSD in the configuration file, you can use the custom label directly.

(7) test

Spring Source Analysis (iv)--use of custom tags

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.