This is the fifth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021
-
SpringBoot integration of Tomcat principles (very simple, not that complicated)
-
When a SpringBoot project is started, it is found that the current project is a Web project (introducing Web dependencies). You will create a web version of the IOC container ServletWebServerApplicationContext.
-
The implementation bean for the ServletWebServerFactory is looked for when the IOC container is loaded.
-
And we will automatically configure a ServletWebServerFactoryAutoConfiguration automatic configuration (Servlet factory automatic configuration class), the automatic configuration is on the basis of our reliance on loading the corresponding ServletWebServerFactory factory implementation.
-
The Tomcat server is then created and started using the SerlvetWebServer factory. The tomcat source code is packaged as a dependency and loaded into the project. We can create the Tomcat server by using new tomcat () and start(). Not externally, but through code).
-
-
Principle of Zero configuration
- The biggest manifestation of this zero configuration is actually the implementation of zero configuration in tomcat and Spring MVC configuration. (See the Spring MVC section.)
-
Principle of automatic assembly
-
There is an annotation @SpringBootApplication on top of the SpringBoot boot class. This is a composite annotation that combines an EnableAutoConfiguration annotation. According to the name, this is the one that turns on automatic configuration.
-
EnableAutoConfiguration role is to import a AutoConfigurationImportSelector class (all EnableXXX is based on the role of the). The main purpose of this select is to return a bunch of fully qualified class names that Spring loads into the IOC container.
-
The Selector class through SpringFactoriesLoader to load all the jars in the meta-inf/spring. Factories file configuration key is org. Springframework. Boot. Autoconfigur E.E nableAutoConfiguration automatic configuration class configuration. (Similar to JavaSPI mechanism, but not oh).
-
The spring-boot-Autoconfigure module has this configuration file, which has predefined more than 100 automatic configuration classes.
-
These auto-configuration classes are not all loaded, but on demand.
-
First we filter a wave of configuration classes, such as annotation classes that we annotate on the SpringBootApplication annotation to be excluded.
-
ConditionalClass Conditional annotations such as @conditionalClass will determine whether there are dependency classes that are required for automatic configuration in our current project. (We typically import these classes with the corresponding starter and do the automatic configuration.)
-
-
These configuration classes are then loaded and parsed, loading the beans that need to be configured into the Spring container.
-
-
How to customize the starter and the principle
-
For modules that are likely to be referenced by multiple projects and that we want to integrate quickly during development, it is highly recommended to encapsulate them as starter
-
Starter has the following core components
-
Write the @ConfigurationProperties configuration class to agree on the configuration your project needs.
-
Write an automatic configuration class that specifies what beans need to be created based on the configuration class information. The only thing to note here is the @Conditional annotation for Conditional injection of configuration classes.
-
Under the meta-inf/spring. Factories configuration org. Springframework. Boot. Autoconfigure. EnableAutoConfiguration, automatic configuration class of the specified needs to be loaded.
-
-
-
Springboot boot process
-
The main method calls the SpringApplication.run method. The method is NEW to create a SpringApplication object. Then run the Run method of the SpringApplication object.
-
Creating a SpringApplication object (New SpringApplication)
-
Determine whether the current project type is Web or non-Web
-
To the spring. Find ApplicationContextInitializer factories
-
To the spring. Factories find ApplicationListener
-
The main purpose is to determine the current project type, and find out all the spring under the factories ApplicationContextInitializer and ApplicationListener
-
-
Run the SpringApplication object (.run(args))
-
Obtain all RunListener, from all of the spring. The factories to find SpringApplicationRunListener, and call the starting method, notify the listener interested in the project launch, project in the boot.
-
Initialize the Environment Environment, the web will create StandardServletEnvironment Environment object. When the environment is ready, all listeners are notified that the environment is ready.
-
CreateApplicationContext Creates an IOC container based on the Web or some other project (reactive, Web, reactive).
-
prepareContext
-
Post-processing flow for IOC containers
-
Call all the initialize method of ApplicationContextInitializer, initialized to the ioc container extended functionality.
-
Notify all listeners of the contextPrepared event
-
Notify all listeners of the contextLoad event
-
-
refreshContext
-
Start formally refreshing the IOC container by calling the container’s refresh method.
-
When the refresh method is called to the onRefresh method, createWebServer is called after the onRefresh method is executed to get the ServletWebServerFactory implementation bean (automatically configured) from the container to create tomcat and start it
-
Start tomcat automatically ServletContainerInitializer callback interface implementation class, thus the DIspatcherServlet these are injected.
-
The summary is that the IOC container is initialized, and then after onRefresh, Tomcat is started and SpringMVC is initialized.
-
-
afterRefresh
- Nothing
-
listeners.started(context); Notify all listeners, started events
-
CallRunners call all runner classes and execute their run methods. (An implementation of CommandLineRunner and ApplicationRunner)
-
Listeners are notified that the boot project is running.
-
-
The whole SpringBoot startup process is nothing more than extending some event monitoring mechanism in the IOC startup process. And tomcat does the initial startup after the onRefresh method.
-
-
Some extension points for SpringBoot
-
SpringApplicationRunListener
- During the Spring Boot startup process, each link can listen to or publish some events.
-
ApplicationListener
- The @EventListener annotation listens for events within a SpringBoot project.
-
ApplicationContextInitializer
- Before ConfigurableApplicationContext container refresh to execute this type can do some processing.
-
CommandLineRunner and ApplicationRunner
- That is, what needs to be done after the SpringBoot project starts.
-
-
What if you don’t want to use Tomcat and you want to use something else?
-
Springboot provides three containers: Tomcat, Jetty, and Undertow
-
By default, Tomcat is used as the servlet container in Springboot. This is because tomcat is used as the default dependency in spring-boot-starter-web. Therefore, only tomcat’s servlet container is configured during automatic configuration
-
If you want to change, you need to remove the Tomcat dependency in your project and then add the starter dependency for the container you want. Like Jetty’s.
-
-
The principle of springboot
- That’s how SpringBoot works