How to start tomcat springboot

When springboot starts, it will automatically recognize whether the current environment is a web environment or non-web environment.

The default web environment context (DEFAULT_WEB_CONTEXT_CLASS): org.springframework.boot. Web.servlet.context.AnnotationConfigServletWebServerApplicationContext
default non-web environment context (
DEFAULT_CONTEXT_CLASS) : org.springframework.context.annotation.AnnotationConfigApplicationContext

the following code for springboot startup class springApplication Creating context Code:
ConfigurableApplicationContext org.springframework.boot.SpringApplication.createApplicationContext():
 
    /**
     * Strategy method used to create the {@link ApplicationContext}. By default this
     * method will respect any explicitly set application context or application context
     * class before falling back to a suitable default.
     * @return the application context (not yet refreshed)
     * @see #setApplicationContextClass(Class)
     */
    protected ConfigurableApplicationContext createApplicationContext() {
        Class<?> contextClass = this.applicationContextClass;
        if (contextClass == null) {
            try {
                switch (this.webApplicationType) {
                case SERVLET:
                    contextClass = Class.forName(DEFAULT_WEB_CONTEXT_CLASS);
                    break;
                case REACTIVE:
                    contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
                    break;
                default:
                    contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
                }
            }
            catch (ClassNotFoundException ex) {
                throw new IllegalStateException(
                        "Unable create a default ApplicationContext, "
                                + "please specify an ApplicationContextClass",
                        ex);
            }
        }
        return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
    }

 

So take a closer look at the default web environment AnnotationConfigServletWebServerApplicationContext concrete realization:

public class AnnotationConfigServletWebServerApplicationContext extends ServletWebServerApplicationContext implements AnnotationConfigRegistry

Found AnnotationConfigServletWebServerApplicationContext inherited org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext,

AnnotationConfigServletWebServerApplicationContext we look at the code and found no webServer (tomcat, jetty ....) related code. So we continue to look at the parent ServletWebServerApplicationContext implementation:

ServletWebServerApplicationContext : This class is found in the boot bag, inherited GenericWebApplicationContext This jar is spring-web package, inference logic springboot webServer likely embedded in this ServletWebServerApplicationContext in

类结构:public class ServletWebServerApplicationContext extends GenericWebApplicationContext implements ConfigurableWebServerApplicationContext

 

We finally org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext see the code and webServer related to :

    @Override
    protected void onRefresh() {
        super.onRefresh();
        try {
            createWebServer();
        }
        catch (Throwable ex) {
            throw new ApplicationContextException("Unable to start web server", ex);
        }
    }
@Override
    protected void finishRefresh() {
        super.finishRefresh();
        WebServer webServer = startWebServer();
        if (webServer != null) {
            publishEvent(new ServletWebServerInitializedEvent(webServer, this));
        }
    }

 

Which OnRefresh . Org.springframework.context.support methods will void AbstractApplicationContext.refresh () will be registered in the context BeanFactoryPostProcessor, execution BeanFactoryPostProcessor , called after registration BeanPostProcessor,

The finishRefresh methods will void org.springframework.context.support. AbstractApplicationContext.finishRefresh () will be in the context complete various initialization (initialization bean) after the call to, so startup is complete, start WebServer (tomcat, jetty) in context .. .

 

Next we look to create a logical WebServer and WebServer the start:

private void createWebServer() {
        WebServer webServer = this.webServer;
        ServletContext servletContext = getServletContext();
        if (webServer == null && servletContext == null) {
            ServletWebServerFactory factory = getWebServerFactory();
            this.webServer = factory.getWebServer(getSelfInitializer());
        }
        else if (servletContext != null) {
            try {
                getSelfInitializer().onStartup(servletContext);
            }
            catch (ServletException ex) {
                throw new ApplicationContextException("Cannot initialize servlet context",
                        ex);
            }
        }
        initPropertySources();
    }

 

private WebServer start webserver () { 
        web server WEB SERVER = this .webServer;
        if (web server! = null ) { 
            webServer.start (); 
        } 
        Return Web server; 
    }

 

Guess you like

Origin www.cnblogs.com/swave/p/11697668.html