Springboot 2 activate the built Tomcat source code analysis

Spring MVC Servlet to start, for example, the application context for ServletWebServerApplicationContext, inherited most of the methods GenericWebApplicationContext, mainly rewritten postProcessBeanFactory (), refresh (), onRefresh (), finishRefresh (), onClose () method.

Start Process

() Seen from the refresh, the method of rewriting execution order: postProcessBeanFactory (), onRefresh (), finishRefresh ().

1、postProcessBeanFactory()

  1. Add postprocessor WebApplicationContextServletContextAwareProcessor
  2. Interface ServletContextAware ignored since automatic assembly
  3. Scope registered web application

2、onRefresh()

  1. OnRefresh call the parent class () method
  2. Create a Web server

Spring loaded container configuration classes ServletWebServerFactoryConfiguration @Configuration annotation, it will inject different web servers factory class conditional: Tomcat / Jetty / Undertow, default conditions, can be injected TomcatServletWebServerFactory, name of tomcatServletWebServerFactory.

When onRefresh () to create a Web server, first obtain ServletWebServerFactory type of Bean from BeanFactory, namely the above-mentioned TomcatServletWebServerFactory. Using the method of this factory class public WebServer getWebServer(ServletContextInitializer... initializers)to create TomcatWebServer, this method is primarily aimed at TomcatWebServer create Tomcat instance . Also, note that the parameter is a function ServletContextInitializer interface, which method may be passed as a parameter, the initialization TomcatWebServer, call Tomcat.start () Start Server, asynchronously perform the function interface implementation class, i.e. execution OnStartup ( ) method.

Create a Tomcat instance is started and Tomcat related content, that is, the creation of Tomcat related components, such as Server, Service, Connector, Engine, Host, Context, Wrapper, Lifecycle and other components.

Tomcat will be completed this step initialization work, change its life cycle states are: NEW-> INITIALIZING->INITIALIZED , INITIALIZEDsaid that it has initialized state.

3、finishBeanFactoryInitialization(beanFactory)

BeanFactory name is acquired from the requestMappingHandlerMapping Bean, and initialize the Bean, the method of its implementation InitializingBean.afterPropertiesSet interfaces during execution (), the process loops through all available Bean, or find @Controller @RequestMapping annotated classes, using reflection techniques, traversing the requests and their methods of these classes of URL cache mapping relationship.

4、finishRefresh()

Truly complete start Tomcat, its life cycle status changes: INITIALIZED-> STARTING_PREP-> STARTING-> STARTED, that means a successful start.

Request Process

Receiving a request Tomcat

  1. Browser input http://127.0.0.1:8080/test, address 127.0.0.1, port number 8080 Endpoint receives the request, and the request to the processor Processor processing;
  2. The adapter processor by request CoyoteAdapter, find the path from the cache mapping the Mapper, which in turn find Engine-> Host-> Context-> Wrapper
  3. After the first call the filter chain FilterChain, filter, perform Servlet
  4. Finished, and then returned in turn response Response

DispatcherServlet

As a Tomcat Servlet Container Socket responsible for monitoring requests and request mapping, and forwarded to a specific Servlet processing. Spring MVC is very important, responsible for processing requests schedule of classes DispatcherServlet, is an implementation of Servlet, so in Spring MVC application, using DispatcherServlet to process the request, its implementation inheritance Servlet as shown below:

DispatcherServlet

DispatcherServlet the acquired execution process request

DispatcherServlet request execution process

Guess you like

Origin www.cnblogs.com/bigshark/p/11361392.html