Springboot boot Process (3)-refresh method

Source: Internet
Author: User

1 springboot at boot time, the Run method is called to create the environment settings spring container, which contains the Refresh method, complete the configuration class resolution, various beanfactorypostprocess and beanpostprocessor registrations, Web built-in container construction, internationalization configuration initialization, and so on, refresh calls the parent class Abstractapplicationcontext Refresh method as follows.

public void Refresh () throws Beansexception, IllegalStateException {
Object var1 = This.startupshutdownmonitor;
Synchronized (this.startupshutdownmonitor) {
This.preparerefresh ();
Configurablelistablebeanfactory beanfactory = This.obtainfreshbeanfactory ();
This.preparebeanfactory (beanfactory);

try {

This.postprocessbeanfactory (beanfactory);
This.invokebeanfactorypostprocessors (beanfactory);
This.registerbeanpostprocessors (beanfactory);
This.initmessagesource ();
This.initapplicationeventmulticaster ();
This.onrefresh ();
This.registerlisteners ();
This.finishbeanfactoryinitialization (beanfactory);
This.finishrefresh ();
} catch (Beansexception var9) {
if (this.logger.isWarnEnabled ()) {
This.logger.warn ("Exception encountered during context initialization-cancelling refresh attempt:" + var9);
}

This.destroybeans ();
This.cancelrefresh (VAR9);
Throw VAR9;
} finally {
This.resetcommoncaches ();
}

}
}

(1) Preparerefresh before Rehresh, one is to set the spring start event, activate the active state, the second is to initialize the property source information, and the third is to verify the necessary properties.

(2) Preparebeanfactory gets beanfactory from the spring container and makes the relevant settings for subsequent use.

* Set the ClassLoader to load the bean, set the expression parser that resolves the bean expression, and add the property registrar resourceeditorregistrar.

* Add Applicationcontextawarepocessor this beanpostprocessor, inject resourceloaderaware, Applicationeventpublisheraware, Messagesourceaware, Applicationcontextaware, environmentaware several interfaces.

* Set various bean,beanfactory,resourceloader,applicatioineventpublisher,applicationcontext.

* Configure default System properties and so on.

(3) Postprocessbeanfactory after the previous step beanfactory set up after the next operation, different spring containers for different operations. For example, Annotationconfigembeddedwebapplicationcontext will inject the bean, check the Basepackages property, If set, the bean under Basepackage will be scanned and registered using Classpathbeandefinitionscanner, if the Annotatedclasses property is set, These annotated beans are registered with Annotatedbeandefinitionreader.

(4) Invokebeanfactorypostprocessors

* Beanfactorypostprocessor its implementation class can be executed before the bean instantiation, after the spring container has loaded the bean's definition file. The entry parameter of the interface method is Configurrablelistablebeanfactory, which can get the definition information of the related bean, modify the metadata of various configurations, and can configure multiple processor. Control the order of execution for each implementation class by setting the Order property.

* Beanpostprocessor Beanpostprocessor is executed after the spring container has loaded the bean's definition file and instantiated the bean. The order in which the beanpostprocessor is executed is after beanfactorypostprocessor, the scope of the Beanpostprocessor is container-level, which is only relevant to the container in which it resides. If you define beanpostprocessor in a container, it simply resets the beans in this container. It does not do any processing on beans that are defined in another container.

* Beandefinitionregistrypostprocessor: Inherit beanfactorypostprocessor, function like Beanfactorypostprocessor, It's just using beandefinitionregistry to process beans.

When a web-based spring container annotationconfigembeddedwebapplicationcontext is constructed, the internal properties are initialized Annotatedbeandefinitionreader reader , this reader constructs some post processor in Beanfactory, including Beanpostprocessor and Beanfactorypostprocessor ( such as Configurationclasspostprocessor, autowiredannotationbeanpostprocessor); The logic for processing beanfactorypostprocessor by the Invokebeanfactorypostprocessors method is:

Find the Beandefinitionregistrypostprocessor type bean from the Spring container ( These processor are registered in the container when the container was first created by constructing the Annotatedbeandefinitionreader, and then executed according to priority, with the following logic:

1) Implement priorityordered interface Beandefinitionregistrypostprocessor all first, then sort and then execute

2) Implement ordered interface Beandefinitionregistrypostprocessor find out, then sort and then execute

3) priorityordered and ordered interfaces are not implemented Beandefinitionregistrypostprocessor find to execute and execute sequentially

  Configurationclasspostprocessor This processor is the highest-priority executed processor (implements the Priorityordered interface). The configurationclasspostprocessor will go to Beanfactory to find all the beans with @configuration annotations, and then use Configurationclassparser to parse the class. Configurationclassparser has a map-type configurationclasses attribute to hold the parsed class, and ConfigurationClass is an encapsulation of the configuration class to be parsed. The annotated information of the configuration class is stored internally, the method that is modified by the @bean annotation, the information that @ImportResource the annotation adornment, the importbeandefinitionregistrar and so on are stored in this encapsulation class. Here Configurationclasspostprocessor is the first to be processed another reason is that if there is a custom beanfactorypostprocessor in the program, Then the postprocessor first has to be parsed through configurationclasspostprocessor, before it can be found and executed by the spring container. (Configurationclasspostprocessor is not executed first, this processor will not be parsed, will not be parsed and will not be executed).

The Invokebeanfactorypostprocessors method concludes by finding the implementation class for the Beandefinitionregistrypostprocessor and Beanfactorypostprocessor interfaces from the spring container and following a Order of the rules to be executed. Where configurationclasspostprocessor this beandefinitionregistrypostprocessor has the highest priority, it will modify the class of @configuration annotations in the project (@ Component, @ComponentScan, @Import, @ImportResource-modified classes are also processed), and the beans are registered to Beanfactory after parsing is complete. It is important to note that the bean registered at this time has not yet been instantiated.

(5) Registerbeanpostprocessors

Executed using the Registerbeanpostprocessors method of the Postprocessorregistrationdelegate class. The process here is similar to invokebeanfactorypostprocessors:
1) First find out the implementation of the Priorityordered interface Beanpostprocessor and sorted and added to the Beanpostprocessor collection of Beanfactory
2) Find the beanpostprocessor that implements the ordered interface and sort it into the Beanpostprocessor collection of Beanfactory
3) Beanpostprocessor added to Beanfactory beanpostprocessor collection without implementing the priorityordered and ordered interfaces
These already existing beanpostprocessor have been explained in the Postprocessbeanfactory method, are registered by Annotationconfigutils's Registerannotationconfigprocessors method. These beanpostprocessor include autowiredannotationbeanpostprocessor (processing beans that are @autowired annotated and injected), Requiredannotationbeanpostprocessor (the method that handles the @required annotation adornments), commonannotationbeanpostprocessor (processing @predestroy, @ Postconstruct, @Resource, and more). If it is a custom beanpostprocessor, it has been registered in the container by Configurationclasspostprocessor.
These beanpostprocessor will be instantiated within this method (by invoking the Getbean method of Beanfactory, if no instantiated class is found, the instantiation is instantiated).

(6) Initmessagesource initializes the internationalization attribute.

(7) Initapplicationeventmulticaster initializes the event broadcaster for publishing events. The Eventpublishingrunlistener listens for events and is injected contextprepared before the run function. This time does not need to register, as long as the beanfactory to get the broadcaster directly set to the spring container, if not to initialize themselves.

(8) Onrefresh different containers are implemented, such as Configembeddedwebapplicationcontext will call Createembeddedservletcontainer method to create the built-in servlet container, Currently only three types of Tomcat,jetty,undertow are supported.

(9) Registerlisteners adds the listener of the listener and beanfactory in the spring container to the broadcaster.

Finishbeanfactoryinitialization instantiates all instances of beanfactory that have been registered but not instantiated, except lazy loading. For example, classes that are parsed by various annotations in the Invokebeanfactorypostprocessors method are initialized. The various beanpostprocessor begin to function during the initialization process.

Finishrefresh initializes the life cycle processor Lifecycleprocessor and calls its Onrefresh method, finds all the implementation classes of the Smartlifecycle interface and calls the Start method, and publishes the event to inform listener , the Registerapplicationcontext method of Livebeansview is also called if the JMX-related properties are set.

Springboot boot Process (3)-refresh method

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.