XML parsing (middle) Default tag parsing

teasers

The Spring framework was originally developed based on configuration files and later introduced annotations. Think back to the way I started using the Spring framework:

  1. Create a New Maven project and introduce spring-related JARS.
  2. Create a configuration file “spring.xml”.
  3. Create the test class.

From this, the Spring framework creates an instance of Man. Spring creates the Man instance according to the < bean > tag in the “spring.xml” configuration file.

Main process analysis

Class inheritance relationships

ClassPathXmlApplicationContext class

Line 142: Record the configuration file name “spring.xml”.

Line 143: Main flow

AbstractApplicationContext class

@Override

// Spring source gate, are you ready?

public void refresh(a) throws BeansException, IllegalStateException {
        synchronized (this.startupShutdownMonitor) {
                // Prepare this context for refreshing.
                prepareRefresh();

                // Tell the subclass to refresh the internal bean factory.
                / / XML parsing
                ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

                // Prepare the bean factory for use in this context.
                prepareBeanFactory(beanFactory);

                try {
                        // Allows post-processing of the bean factory in context subclasses.
                        postProcessBeanFactory(beanFactory);

                        // Invoke factory processors registered as beans in the context.
                        invokeBeanFactoryPostProcessors(beanFactory);

                        // Register bean processors that intercept bean creation.
                        registerBeanPostProcessors(beanFactory);

                        // Initialize message source for this context.
                        initMessageSource();

                        // Initialize event multicaster for this context.
                        initApplicationEventMulticaster();

                        // Initialize other special beans in specific context subclasses.
                        onRefresh();

                        // Check for listener beans and register them.
                        registerListeners();

                        // Instantiate all remaining (non-lazy-init) singletons.
                        finishBeanFactoryInitialization(beanFactory);

                        // Last step: publish corresponding event.
                        finishRefresh();
                }

                catch (BeansException ex) {
                        if (logger.isWarnEnabled()) {
                                logger.warn("Exception encountered during context initialization - " +
                                                "cancelling refresh attempt: " + ex);
                        }

                        // Destroy already created singletons to avoid dangling resources.
                        destroyBeans();

                        // Reset 'active' flag.
                        cancelRefresh(ex);

                        // Propagate exception to caller.
                        throw ex;
                }

                finally {
                        // Reset common introspection caches in Spring's core, since we
                        // might not ever need metadata for singleton beans anymore...resetCommonCaches(); }}Copy the code

Line 523: Main flow

ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); 1. Create BeanFactory object. 2. XML parsing -- Default tag parsing: <bean>... -- Custom tag parsing: <context:component-scan>Copy the code

AbstractRefreshableApplicationContext class

Line 127: create bean factory line 129: whether can set the circular dependencies line 130: main process, parse the XML and load BD line (BeanDefinition) click enter AbstractXmlApplicationContext class 131: Assigned to AbstractRefreshableApplicationContext attributes of a class.

AbstractXmlApplicationContext class

Line 83:Delegate patternCreate an XML parser. XML configuration files are parsed using XmlBeanDefinitionReader.

Line 94: Main flow, load BD (XmlBeanDefinitionReader)

Line 126: Get the configuration file to load, originally set to “spring.xml”

Line 128: Main flow

Line 223: Convert configuration files to Resource objects (as streams)

Line 224: Start calling the load BD method of the XmlBeanDefinitionReader class

Line 188:Template patternCall the method in your subclass, in this case the XmlBeanDefinitionReader class

XmlBeanDefinitionReader class

Line 310: Wrap the Resource object as EncodedResource

Line 333: Gets the stream object from the Resource object

Line 338: Main flow

Line 390: Encapsulates the inputSource as a Document file object, which is the JDK API

Line 391: Main flow. The label obtained from the Document object is encapsulated as BD

Line 509:Delegate patternEntrust BeanDefinitionDocumentReader this class to parse the Document object

Line 511: Main flow

DefaultBeanDefinitionDocumentReader class

Line 121: Entry parameter is root node

Line 129: create a BeanDefinitionParserDelegate object

Line 148: Called before parsing to decorate the root node. The default implementation logic is empty and can be customized

Line 149: Main flow. Parsing the BD

Line 148: Called before parsing to decorate the root node. The default implementation logic is empty and can be customized.

Logical conclusion of this method: traverse the set of child nodes configured in THE XML file obtained by the root node, judge node types (default tags/custom tags) and parse them respectively.

Line 170: Obtain the subtag list of the root tag. Line 175: Determine whether it is a default tag or a custom tag. Line 176: Resolve the default tag

Now that we have the tags from the configuration file, the logic for parsing the two tags is as follows.

conclusion

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Copy the code

When this line of code executes, Spring finds the configuration file based on the input parameters, parses the file through a stream and retrieves the node information in the file, and parses each node based on its type (that is, converting the label to BeanDefinition).

Note: this article through the source code + line description of the way to describe, if not understand can leave a message. This article is only a personal learning record, there are mistakes, please correct.

XML parsing (middle) Default tag parsing