1-spring XML and annotation parsing procedures

Source: Internet
Author: User

Spring MVC Portal Dispatcherservlet, the class diagram looks like this

Dispatcherservlet is a servlet, where the servlet's initialization method, Init (), is known through the class diagram, and the init () method in Httpservletbean can be viewed for servlet initialization.

XML parsing and annotation parsing portals

After some XML and spring initialization configurations are loaded, enter the Abstractapplicationcontext#refresh () method

ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
Enter the Abstractrefreshableapplicationcontext#refreshbeanfactory () method to parse the XML and annotations through the Loadbeandefinitions (beanFactory) method
    • Parsing classes FOR XML
      Abstractxmlapplicationcontext
    • Parsing classes for annotations
      Annotationconfigwebapplicationcontext
The flow of XML parsing process classes
    • The XML or properties are loaded into the Resource object through Resourceloader, and the Resource object gets the file stream corresponding to the file, which is used when parsing the XML.
    • Each Resource object has a corresponding reader object, and the reader object encapsulates the configuration into beandefinition
    • Beandefinition is placed in a map or container

    1. In the Classpathxmlapplicationcontext#getconfigresources () method, all XML files are encapsulated as resource objects by means of the getconfigresources () method
    2. Looping resource objects, parsing each XML file
      1. Enter the Loadbeandefinitions method in the Xmlbeandefinitionreader class for XML parsing
        Spring parsing XML using dom4j
      2. In the defaultbeandefinitiondocumentreader#parsebeandefinitions (Element root, Beandefinitionparserdelegate delegate) method , the root root node of the XML is judged by the default label or by a custom label , which is parsed separately.
      3. Gets all child nodes through the XML root node, loops through each child node, and determines whether the child node is the default label or the custom label to parse separately.
      4. The elements of each label are parsed and encapsulated as beandefinition objects. The Beandefinition object is then encapsulated as a Beandefinitionholder object, Beandefinitionholder contains the bean's name, alias, and Bean's Beandefinition object.

Default Tags: import tags, alias tags, bean tags, beans tags
Custom Tags: spring MVC custom labels and their own extended labels, etc.

Beandefinition description

    • We will encapsulate the tag elements in the XML such as: Beans, Componentscan, annotation-config and other tags into beandefinition objects.
    • We will encapsulate annotation such as: @Service, @Controller, @Component, @Resource and other annotations into beandefinition
Custom Label Resolution
    1. Namespace NamespaceURI, which is the beans tag's xmlns, xmlns:context, XMLNS:AOP, and XMLNS:TX URI
    2. When a tag is parsed, the corresponding namespace URI is obtained based on a tag, and the Beandefinitionparserdelegate#parsecustomelement (Element ele) method is viewed
    3. By parsing the namespace URI and instantiating the corresponding namespace-processing class object, the parsing process invokes the Init () method in the namespace-processing class, registering all the parsers about the element in the namespace. See the Defaultnamespacehandlerresolver#resolve (String NamespaceURI) method specifically
    4. The parse method that invokes the processor for a namespace, for example: The Contextnamespacehandler class is a namespace-processing class of Xmlns:context
To view Xmlns:context namespaces
    1. Enter the Spring-context module
    2. Go to the resources/meta-inf/folder below
    3. View the Spring.handlers configuration file to see the URI for each namespace corresponds to a class.
      For example, Xmlns:context corresponds to a URI http://www.springframework.org/schema/context corresponding to the Contextnamespacehandler class each namespace has a corresponding parsing class Namespacehandler, each namespace resolution class has an element parser for the corresponding namespace.

      http\://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler
      Context namespace Description

      For example:
      Context find the URI of the Uri,beans tag that has the content corresponding to it.
      Spring.handlers inside the URI corresponding to the processing class, the implementation of the Namespacehandler interface, the namespace corresponding to the label corresponding processing registered in.

If the tag Component-scan is parsed, then the parsing class is the Componentscanbeandefinitionparser class, and calling the Component-scan tag corresponds to the parse of the parsing class The method is parsed in the Componentscanbeandefinitionparser parsing class to complete the
    1. Scanning of basic Packages
    2. Configuration of type Filters
    3. Compatible with Annotation-config configuration
    4. Note Processor Beanpostprocessor Registration
Analysis Annotationconfigutils.registerannotationconfigprocessors (Readercontext.getregistry (), source);
    1. The Autowiredannotationbeanpostprocessor class is the support for @Autowire annotations
    2. The Requiredannotationbeanpostprocessor class is the support for @Required annotations
    3. The Commonannotationbeanpostprocessor class is the support for jsr250, which is @Resources

All generated Beandifinition objects are registered for cache to Beandefinitionmap key is Beanname,value is beandefinition, then will put Beanname in the list inside, Beandifinitionnames is the list.

1-spring XML and annotation parsing procedures

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.