This is the 16th day of my participation in the Novembermore Challenge. As mentioned in the previous chapter, we can use an external Servlet container. We originally created the Spring Boot project to package it and deploy it to our server as a Jar package, but we can also use war. Both packaging methods have their own advantages. Let’s do the analysis.

Embedded Servlet container: Application into executable JARS

Pros: Simple, portable;

Disadvantages: The default does not support JSP, optimization of custom complex (using custom 【 ServerProperties, custom EmbeddedServletContainerCustomizer 】, Write your own embedded Servlet container EmbeddedServletContainerFactory 】 【 create factory);

External Servlet container: Tomcat installed outside – war package packaging;

We used jar to create our project, this time we will use WAR to create the project

1. Create a project and select WAR as the package mode

2. When selecting a module, we still choose a Web.

Create projects without webApp

3. We press Ctrl+Shift+Alt+S

Choose the right location

4. Configure our Tomcat

5. Deploy artifacts

Create a JSP page

7. Start

Jar package: Execute main method of SpringBoot main class, start IOC container, create embedded Servlet container;

\

War file: start the server, application server startup SpringBoot SpringBootServletInitializer 】 【, start the ioc container;

\

Servlet3.0 (Spring annotations) :

\

8.2.4 Shared libraries/runtimes pluggability:

\

Rules:

1), the server to start (web application) creates the current web application inside each jar package inside ServletContainerInitializer instances:

2), the implementation of ServletContainerInitializer in jars meta-inf/services folder, there is a called javax.mail. Servlet. ServletContainerInitializer file, Content is the implementation class ServletContainerInitializer full name of the class

3) We can also use @handlestypes to load the classes we are interested in when the application starts.

Process:

1) Start Tomcat

2), org \ springframework \ spring – web \ 4.3.14 RELEASE \ spring – web – 4.3.14. The jar! \ meta-inf \ services \ javax.mail servlet. ServletContainerInitializer:

Spring’s web module contains the file: org. Springframework. Web. SpringServletContainerInitializer

3), SpringServletContainerInitializer will @ HandlesTypes (WebApplicationInitializer. Class) of all the types of classes are introduced into the onStartup method Set “class” ? > >; For these WebApplicationInitializer type of class instance is created;

4), every WebApplicationInitializer call their onStartup;

5), the equivalent of our SpringBootServletInitializer class object is created, and perform the onStartup method

6), will perform the onStartup createRootApplicationContext SpringBootServletInitializer instance; Create a container

protected WebApplicationContext createRootApplicationContext( ServletContext servletContext) { / / 1, to create SpringApplicationBuilder SpringApplicationBuilder builder = createSpringApplicationBuilder (); StandardServletEnvironment environment = new StandardServletEnvironment(); environment.initPropertySources(servletContext, null); builder.environment(environment); builder.main(getClass()); ApplicationContext parent = getExistingRootWebApplicationContext(servletContext); if (parent ! = null) { this.logger.info("Root context already created (using as parent)."); servletContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, null); builder.initializers(new ParentContextApplicationContextInitializer(parent)); } builder.initializers( new ServletContextApplicationContextInitializer(servletContext)); builder.contextClass(AnnotationConfigEmbeddedWebApplicationContext.class); // Call the configure method. Subclasses override this method, passing in SpringBoot's main program class. Builder.build (); SpringApplication = builder.build(); if (application.getSources().isEmpty() && AnnotationUtils .findAnnotation(getClass(), Configuration.class) ! = null) { application.getSources().add(getClass()); } Assert.state(! application.getSources().isEmpty(), "No SpringApplication sources have been defined. Either override the " + "configure method or add an @Configuration annotation"); // Ensure error pages are registered if (this.registerErrorPageFilter) { application.getSources().add(ErrorPageFilterConfiguration.class); } return run(application); }Copy the code

7) The Spring application starts and creates the IOC container

public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; FailureAnalyzers analyzers = null; configureHeadlessProperty(); SpringApplicationRunListeners listeners = getRunListeners(args); listeners.starting(); try { ApplicationArguments applicationArguments = new DefaultApplicationArguments( args); ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); Banner printedBanner = printBanner(environment); context = createApplicationContext(); analyzers = new FailureAnalyzers(context); prepareContext(context, environment, listeners, applicationArguments, printedBanner); // Refresh IOC container refreshContext(context); afterRefresh(context, applicationArguments); listeners.finished(context, null); stopWatch.stop(); if (this.logStartupInfo) { new StartupInfoLogger(this.mainApplicationClass) .logStarted(getApplicationLog(), stopWatch);  } return context; } catch (Throwable ex) { handleRunFailure(context, listeners, analyzers, ex); throw new IllegalStateException(ex); }}Copy the code

\

Start the Servlet container, and then start the SpringBoot application

\