Custom tag parsing

teasers

Previous review: Custom tag parsing process:

  1. First find the corresponding BeanDefinitionHandler based on the tag’s namespaceURI. Handlers and namespaceURI maps BeanDefinitionHandler to meta-INF/Spring.Handlers. Obtain the corresponding BeanDefinitionHandler according to namespaceURI from the mapping Map.
  2. Then call BeanDefinitionHandler’s init method, which sets up a mapping between tag names and corresponding tag resolution classes, which is also a Map.
  3. Finally, the parse class of the tag is found, and the parse method of the class is called for specific tag parsing logic.
  4. After tag parsing is complete, there is another operation: register PostProcessor. Spring will register some PostProcessor classes after custom tag parsing.

Refresh method main flow

Line 523: Create the bean factory, parse the XML file, and register the BeanDefinition. At this point spring already knows the class information we need it to manage.

Line 526: Set some values for the bean factory.

Line 533: invokeBeanFactoryPostProcessors (the beanFactory)

Call realized BeanDefinitionRegistryPostProcessor interface and spring BeanFactoryPostProcessor interface classes. Click to call this class method:

Line 707: completed by PostProcessorRegistrationDelegate class called logic. The method is long, as shown below:

1. BeanDefinitionRegistryPostProcessor interface

Line 86-87: get all the classes of realized BeanDefinitionRegistryPostProcessor interface of BeanDefinition beanName of objects. Line 88-93: Loop through these BDS, filtering out beandefinitions that have implemented sorted interfaces. Line 94: Sort the filtered BDS that implement the sort interface. Line 96: sorting called after these classes postProcessBeanDefinitionRegistry () method. Lines 99-110: As above, the difference is that beandefinitions that implemented the Ordered interface are filtered. Line 112-118: Process beanDefinitions that do not implement the sort interface last. Line 130-132: Call the postProcessBeanFactory() method of the above BeanDefinition.

2. Spring BeanFactoryPostProcessor interface

Line 142-186: logic is similar to above eanDefinitionRegistryPostProcessor interface.

Completed by PostProcessorRegistrationDelegate class method invocation logic.

The application of 3. BeanDefinitionRegistryPostProcessor interface, spring BeanFactoryPostProcessor interface

, postProcessBeanFactory postProcessBeanDefinitionRegistry () () in the call, the bean has been registered, but has not yet been instantiated, we can custom implementations to operating bean factory and BD object.

Line 536: registerBeanPostProcessors (the beanFactory)

Complete the instantiation of the BeanPostProcessor class and register it with the bean factory.

Methods the annotation: Instantiate and register all BeanPostProcessor beans. If given, the explicit order is followed. It must be called before any instantiation of the application bean.

Line 208, line 229, line 241: Instantiate beans. We’ll talk about that later.

Logic and 533 lines above invokeBeanFactoryPostProcessors (the beanFactory) similar to the above method is invoked, is now a BeanPostProcessor registered to bean plant.

Line 542: initApplicationEventMulticaster ()

Initialize the event manager. Observer model

Each event listener focuses on something different.

Line 766: get ApplicationEventMulticaster instance from the bean plant.

Line 767-768: Set it to the current property,

Line 774-775: no access to the instance, and create a SimpleApplicationEventMulticaster registered to the bean plant.

Line 548: registerListeners ()

Register event listeners.

Line 825-828: First register the specific event listener. Retrieves the specified listener from the context object and completes the registration (method 1).

Line 832: Then register by getting a BD (method two).

Custom event listeners

Create listener:

1. Example of Method 1

Register while the program is running:

2. Example of Mode 2

Register when the program starts:

At this point, the preparation for instantiating the bean is complete.

conclusion

  1. Complete postProcessBeanDefinitionRegistry invokeBeanFactoryPostProcessors (the beanFactory) : (), postProcessBeanFactory () call.
  2. RegisterBeanPostProcessors (the beanFactory) complete BeanPostProcessor instantiation of a class and registered with the bean in the factory.
  3. InitApplicationEventMulticaster () initializes the event manager.
  4. RegisterListeners register event listeners.

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.

Custom tag parsing