XML parsing (top)

teasers

Review of the previous article:

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

When this line of code executes, spring internally streams the configuration file “spring.xml”, retrieves a collection of tags, and parses both tags. This article describes the process of parsing default tags.

Default label main flow parsing

Note: Take < bean > tag parsing as an example:

DefaultBeanDefinitionDocumentReader class

Lines 163-165: Method comments: Parse the document for root level elements: “import”, “alias”, “bean”…


Line 169: Checks whether the namespace of the current label belongs to the default namespace.

Judgment logic: BeanDefinitionParserDelegate class: Line 169 ends.


Line 170: Gets the collection of children of root. The diagram below:

Line 170 ends.


Lines 171 -174: Iterate through the collection of child nodes and convert root to an Element object. Line 175: See line 169 logic.


Line 176: Default label entry. Click to enter this class method

Class tag name constant: Line 191: Parse the < import > tag

Line 194: Parse the < alias > tag

Line 197: Parse the < bean > tag, click to enter this class method

Method summary: Convert the label object into a BeanDefinitionHolder object, and start registering the BD to the cache.

Lines 302-303: Method comments that process the given bean element, parse the bean definition and register it in the registry.


Line 306: Element will be packaged into BeanDefinitionHolder object click enter BeanDefinitionParserDelegate class

BeanDefinitionParserDelegate class

Line 515: Create a BD (GenericBeanDefinition) object

Line 517: Adds the bean label’s attribute value to the BD object’s attribute.

Line 520: Parse the < meta > tag in the bean tag.

Line 521: Parse the < look-method > tag in the bean tag.

Line 522: Resolve the < appet-method > tag in the bean tag.

Line 524: Parse the < constructor-arg > tag in the bean tag.

Line 525: Parse the < property > tag in the bean tag.


Line 308: Decorator mode Line 311: Register the final decorator instance.

BeanDefinitionReaderUtils class

Method comments: Register the given bean definition with the given bean factory. DefaultListableBeanFactory into for BDH, previously created.

Line 163: Gets the beanName.


Line 164: use DefaultListableBeanFactory current BD factory registration. Registration logic:

DefaultListableBeanFactory class

Class notes: Spring ConfigurableListableBeanFactory and BeanDefinitionRegistry to the default implementation of the interface: a mature bean bean definition metadata based factories, through after processor extensions.

Class attributes are defined as follows:

Line 140: Mapping from the serialized ID to the factory instance.

Line 144: The optional ID of this factory for serialization purposes.

Line 148: Whether different definitions with the same name are allowed to be re-registered.

Line 151: Whether an eager class is allowed to be loaded even for lazy-init beans.

Line 154: Optional OrderComparator that depends on lists and arrays.

Line 158: The parser used to check whether the bean definition is a candidate for auto-assembly.

Line 161: Map from the dependency type to the corresponding autowiring value.

Line 164: The bean defines the mapping of the object, with the bean name as the key.

Line 167: Mapping from the bean name to the merged BeanDefinitionHolder.

Line 170: Mapping of singleton and non-singleton bean names with dependent types as keys.

Line 173: Singleton bean name mapping with dependent type as key.

Line 176: List of bean definition names, in the order they were registered.

Line 179: List of manually registered singleton names, in the order they were registered.

Line 182: Array of bean definition names cached with frozen configuration.

Line 186: Whether metadata can be defined for caching beans for all beans.

Line 942: Get the current BD from beanDefinitionMap and determine if it has been registered.

Line 945: Throws an exception if registered.


Line 972: Checks if a bean has already been instantiated.

AbstractBeanFactory class

Line 173: Record the name of the bean that has been created at least once.

Line 1776: Checks if the bean is currently created. The first time the program runs here, it doesn’t show up, returns false.


Lines 975 -979: The program has been running for a while, and there are currently instantiated beans. Line 985: No bean has been created yet, so the program will go here and save the current BD to beanDefinitionMap. Line 986: Record the name of the current bean separately to the beanDefinitionNames collection as well.


Back to BeanDefinitionReaderUtils class registered BD method. At this point we have registered BD in the Bean Factory. For easy viewing, here is another registration method:

Line 164: BD registration has been completed.

Line 166: Establish a mapping between aliases and ids.

Line 167: Gets the current BD alias collection.


Line 168-172: Loop alias and register.

SimpleAliasRegistry class

Lines 168-172 end.


At this point, BD has completed the registration process. And then back to DefaultBeanDefinitionDocumentReader class.

For easy viewing, here is a circular label to start registering BD method:

End of line 176: Registration of BD for the current loop has been completed. Continue the cycle of registration…

conclusion

< bean > tag parsing is to convert one < bean > tag to a BD (BeanDefinition) object. During the transformation, the configuration attributes and other elements in the tag are set to the BD object. And then deposited the BD in BD factory (DefaultListableBeanFactory) cache Map (beanDefinitionMap).

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 (next) custom tag parsing