springboot Start
 
 
 
Spring boot usually has an entry class named *application, which has a main method in the entry class, which is actually a standard Java application entry method.
Use the Springapplication.run method in the main method to start the Springboot application project.
where @springbootapplication is the core annotation of spring boot, it is a combination annotation:
1 2 3 4 5 6 
 
The @springbootapplication annotation mainly combines @configuration, @EnableAutoConfiguration, @ComponentScan.
If you do not use the @springbootapplication annotation, you can use the @configuration, @EnableAutoConfiguration, @ Componentscan can also achieve the same effect.
the role of several of these annotations is roughly:
@Configuration: To do a annotation annotation on a class similar to the spring XML work, similar to the previous **.xml configuration file.
@EnableAutoConfiguration: The annotations needed when spring boot is automatically configured will let spring boot automatically configure the current project based on the jar package dependencies in the classpath. At the same time, it is also a combination annotation.
1 2 3 4 5 6 7 8 9 10 11 
 
In @enableautoconfiguration, the @import annotation is used to import the Enableautoconfigurationimportselector class, And Enableautoconfigurationimportselector is the key to automatic configuration.
@Import: Spring4.2 only supports import configuration classes
        before Spring4.2 supports importing ordinary Java classes and declaring them as a bean
@ComponentScan: Tell Spring which packages class that is identified by annotations is automatically scanned by spring and loaded into the bean container.
springboot automatic configuration: Springboot is a major feature of automatic configuration, for example: Add Spring-boot-starter-web dependencies, will automatically add Tomcat and Springmvc dependencies, Springboot will be configured automatically for Tomcat and SPRINGMVC.
Also, for example, adding SPRING-BOOT-STARTER-DATA-JPA dependencies, Springboot will automatically perform JPA-related configuration.
Springboot automatically scans the sibling packages of the @springbootapplication's classes, as well as the bean of the subordinate packages (if you can also scan the entity classes that annotate @entity for the JPA project), it is recommended that the entry class be placed under the outermost package.
spring-boot Boot process:
1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 
 
The Springapplication static Run method is called here, and the application class object and the parameter args of the main method are passed in as arguments.
Springapplication Run Method:
1 2 3 4 5 
 
In this static method, the Springapplication object is created and constructed, and the Run method of the object is invoked. To
construct a Springapplication object:
1 2 3 4 
 
The main is attached to some properties of the initial value, the key is still with the Springapplication object of the Initialize method.
1 2 
 
The Initialize method is called in the constructor in the Springapplication class to initialize the member variable sources,webenvironment,initializers of the Springapplication object. Listeners,mainapplicationclass.
1 2 
sources: We pass the arguments to the Springapplication.run method.
 
webenvironment:
 
 
You can see that Webenvironment is a Boolean that is used to indicate whether the current application is a Web application. By looking in classpath for the existence of a class contained in the web_environment_classes array, the current program is a Web application, and vice versa.
1 2 
Initializers:
 
    The key here is to call the Getspringfactoriesinstances method in the Springapplication object to get a list of Applicationcontextinitializer type objects.
1 2 
 
In this method, the first name of all spring factories is obtained by calling Springfactoriesloader.loadfactorynames (type, ClassLoader) ( Under Package spring-boot-version. jar).
1 2 
 
 
As you can see, from a resource file named Spring.factories, read the value of key Org.springframework.context.ApplicationContextInitializer. and spring.factories part of the content is as follows:
1 2 
 
 
We can see from the Spring.factories resource file above that we get the     configurationwarningsapplicationcontextinitializer
Contextidapplicationcontextinitializer
Delegatingapplicationcontextinitializer
Serverportinfoapplicationcontextinitializer the names of these four classes.
the Createspringfactoriesinstances method is then invoked to create a Applicationcontextinitializer instance based on the read-to name ( box with two lines of code executed to create the Applicationcontextinitializer instance).
1 2 3 4 5 6 7 
 
Finally, the list of created objects is sorted and returned.
the Springapplication object's member variable initalizers is initialized to, Configurationwarningsapplicationcontextinitializer, Contextidapplicationcontextinitializer,delegatingapplicationcontextinitializer, Serverportinfoapplicationcontextinitializer a list of objects of these four classes (this is the default member variable initalizers initialized).
below is a few classes of the role, here do not do detailed introduction, will have detailed introduction:
1 2 3 4 5 6 
 
Listeners:
 
Listeners member variable, is a collection of applicationlistener<?> type objects. You can see that getting the member variable content is using the same method as the member variable initializers, But the incoming type changed from Applicationcontextinitializer.class to Applicationlistener.class.
so it's important to see those classes where the value of the key in the spring.factories is Org.springframework.context.ApplicationListener:
1 2 3 4 5 
 
In other words, listener will eventually be initialized to
Clearcachesapplicationlistener
Parentcontextcloserapplicationlistener  Fileencodingapplicationlistener
ansioutputapplicationlistener           configfileapplicationlistener
Delegatingapplicationlistener           Liquibaseservicelocatorapplicationlistener
Classpathloggingapplicationlistener     Loggingapplicationlistener The
list of objects of these classes.
when to trigger, this is not done in detail, and so on when the event occurs, do a detailed description (here is an online map, you can understand):
1 2 3 4 5 6 7 8 9 10 
 
Mainapplicationclass:
 
In the Deducemainapplicationclass method, by getting the current call stack, locate the class of the entry method main and copy it to the Springapplication object's member variable Mainapplicationclass. In our example Mainapplicationclass is the application class that we write ourselves.
1 2 
 
Now that the Springapplication object has been constructed, it is then called to run the Spring boot project by calling the object's Run method.
 
Run Method:
 
Stopwatch is a tool class from org.springframework.util that can be used to conveniently record the running time of a program.
1 2 
 
Set the System Properties java.awt.headless, in our case this property will be set to true, because we are developing a server program, generally run in the absence of a monitor and keyboard environment, but still need some relevant data, So that we can set the system properties to headless mode.
1 2 
 
In the Run method, a series of Springapplicationrunlistener objects are loaded and the started method and finished method of the listeners object are called before and after the ApplicationContext method is created and updated. And when ApplicationContext is created and refreshed, the listeners is passed as a parameter to the Createandrefreshcontext method so that at different stages of creating and refreshing the ApplicationContext, Call the appropriate method of listeners to perform the action. So the so-called springapplicationrunlisteners is actually doing some operations at different stages of the execution of the Springapplication object's Run method, and these operations are configurable.
Call Startint method when creating ApplicationContext
1 2 3 4 
 
Call the finished method when updating the ApplicationContext method:
1 2 
 
Load Springapplicationrunlistener, in the same way as Initializers,listeners, So we can take the value of key to Org.springframework.boot.SpringApplicationRunListener in Spring.factories to see which classes are loaded:
1 2 
 
You can see that the Org.springframework.boot.context.event.EventPublishingRunListener class is loaded.
1 2 
 
Eventpublishingrunlistener:
1 2 
 
The Eventpublishingrunlistener object saves the Springapplication object's member variable listeners all at initialization time.
when your own public method is invoked, publish the appropriate event, or perform the appropriate action.
Runlistener is when the Springapplication object's Run method executes to a different stage, it publishes the event listener that is logged in the member variable listeners of the Springapplication object.
Springapplicationrunlisteners Related class structure:
1 2 3 4 5 6 7 8 9 
 
By default, the listeners started method is invoked, and when the Applicationstartedevent is released, what the event listener we have loaded does, (implements the class of the Applicationlistener interface, And the event type is applicationstartingevent type).
1 2 
 
 
By default, Liquibase is not present in classpath, so no action is performed.
Loggingapplicationlistener listens for applicationstartedevent, creates the corresponding log system object according to the class condition in classpath, and performs some initialization operations;
1 2 3 4 
 
By default, the object of the Org.springframework.boot.logging.logback.LogbackLoggingSystem class is created, Logback is the Springboot log system that is used by default.
1 2 
 
Here, the handling of the Applicationstartedevent event is over.
1 2 
Create and Refresh ApplicationContext:
 
 
Defaultapplicationarguments:
1 2 
 
 
This is to parse the args parameter of the main function as a propertysource, by default, the length of the args is 0, so the defaultapplicationarguments created here has no actual content.
1 2 
configurableenvironment: Create and configure Applicationconext environment
 
 
First, you call the Getorcreateenvironment method to get a environment object.
1 2 
 
By default, when executed here, the environment member variable is null and the Webenvironment member variable evaluates to True, so a Standardservletenvironment object is created and returned. The
Configureenvironment method of the object of the Configurableenvironment type is then invoked to configure the environment object that was acquired in the previous step.
1 2 3 4 
 
The Configureenvironment method first invokes the Configurepropertysources to configure properties, and then calls Configureprofiles to configure the profiles.
1 2 
 
Here, Configurepropertysources first looks at the member variable defaultproperties of the Springapplication object, if the variable is not null and the content is not empty, Add it to the end of the environment Propertysource list.
then look at the member variables of the Springapplication object addcommandlineproperties and the parameters of the main function args, if you set the Addcommandlineproperties=true, and the number of args is greater than 0, Then construct a propertysource that consists of the parameters of the main function to the top of the Environment Propertysource list (this ensures that the configuration we do with the parameters of the main function is the highest priority and can override other configurations).
By default, this function does nothing because defaultproperties is not configured and the main function has a parameter args number 0.
call Configureprofiles to configure profiles:
1 2 3 4 5 6 7 8 
 
Configureprofiles will first read the configuration items in the properties that are spring.profiles.active and configured to environment.
1 2 
 
The member variable additionalprofiles of the Springapplication object is then added to the environment active profiles configuration.
by default, There are no spring.profiles.active configuration items in the configuration file, and the Springapplication object's member variable Additionalprofiles is also an empty collection, so this function is not configured with any active Profile
1 2 3 4 
the object listeners the Springapplicationrunlisteners class is invoked to publish the Applicationenvironmentpreparedevent event:
 
By this step, environment even if the configuration is complete. Next, the object listeners the Springapplicationrunlisteners class is called to publish the Applicationenvironmentpreparedevent event.
1 2 
 
 
When the event is released, we can see what the loaded Applicationlistener object has responded to the event and what it does:
 
Fileencodingapplicationlistener responds to this event:
1 2 
 
Check that the file.encoding configuration is consistent with spring.mandatory_file_encoding
by default, because there is no spring.mandatory_file_encoding configuration, So this response method does nothing.
Ansioutputapplicationlistener responds to this event:
1 2 3 4 5 6 
 
According to Spring.output.ansi.enabled and spring.output.ansi.console-available the Ansioutput class is configured accordingly. By default, this response method does nothing because the configuration is not done.
Configfileapplicationlistener responds to this event:
1 2 3 4 5 
 
 
Can see that configfileapplicationlistener from meta-inf/ The Spring.factories file reads the environmentpostprocessor configuration, loads the object of the corresponding Environmentpostprocessor class, and invokes its Postprocessenvironment method. In our case, the Cloudfoundryvcapenvironmentpostprocessor and springapplicationjsonenvironmentpostprocessor are loaded and executed, Since there is no cloudfoundry and JSON configuration in our example, this response does not load any configuration files into environment.
1 2 
 
 
Delegatingapplicationlistener Response to this event: the configuration item with the key in the configuration file context.listener.classes is loaded in the member variable Multicaster