Simulated Springboot 2: built-in tomcat

Posted by abduljan on Fri, 10 May 2019 17:39:18 +0200

Since tomcat is built into a project and can successfully start a project, you need to know what tomcat has done, so you need to understand first.

How a common web project is started and run by our locally configured tomcat

(1) Tell tomcat which projects to run first (that is, configuring tomcat before starting a project with eclipse, idea, or copying compiled war packages to webapp on linux)

Thereafter, when tomcat is started, tomcat loads the compiled. class project.

 

(2) tomcat also loads web.xml or the Web Application file of the previous blog when it loads the compiled project, and in this file

What you do is load spring and then spring MVC.

Load Spring MVC: Register Spring MVC's Dispatcher Servlet into the ServletContext container

Then just when Tomcat is integrated within the project, the compiled project is associated with Tomcat before the main method starts tomcat, and Tomcat can automatically load the Web Application class.

1. Adding tomcat maven dependencies (creating tomcat instances through code)

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>8.5.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-el</artifactId>
            <version>8.5.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>8.5.5</version>
        </dependency>

2. Create Spring Main (function: run main to start tomcat), Start Tomcat (function: create Tom instance, then let Tomcat load compiled items)

    

(1) What StartTomcat does: Create thread classes

public class StartTomcat implements Runnable {
    @Override
    public void run() {
        //Establish tomcat Example
        Tomcat tom = new Tomcat();
        //Set port
        tom.setPort(8081);
        try{
            //Get the compiled project claess Route
            String path = StartTomcat.class.getResource("/").getPath();

            //Obtain webapp file
            String filePath = new File("src/main/webapp").getAbsolutePath();

            //Then will webapp The following items are added to tomcat Of context Container ( context Corresponding to a running project)
            Context context =tom.addWebapp("/Project name",filePath); //Parametric 1: Typically, the project name corresponds to the project name in the request url
       //webResourceRoot For loading projects class file 
       WebResourceRoot webResource = new StandardRoot(context); webResource.addPreResources(new DirResourceSet(webResource,"/WEB-INF/classes",path,"/")); tom.start(); }catch (Exception e) { e.printStackTrace(); } //Blocking, Waiting for Front End Connection tom.getServer().await(); } }

Create tomcat instances

Obtain the claess path after project compilation

C. Get the items in the webapp directory

D. Then add the project under webapp to the context container of tomcat

E. Start tomcat

F. Wait the Server instance of tomcat to listen for requests

  (2) SpringMain     

public class SpringMain {

    public static void main(String[] args) {
        //start-up
        new StartTomcat().run();
    }
}

You can also customize a comment and scan the class to start the main method that annotates the comment, doing the new StartTomcat().run();

 

(3) Run the main method to start the tom service

  

Next article: How springboot accesses static, template and other static resources in the resources directory (because there is no webapp directory in the ssm project)

Topics: Java Tomcat Spring Apache Eclipse