Springboot ? How to Understand Parent, Starter, Bootstrap Class and Embedded Tomcat?

SpringBoot.Six, the difference between starter and parent



What exactly is SpringBoot? How to understand parent, starter, bootstrap class and embedded Tomcat? (two).
In the hazy feeling, starter and parent seem to help us simplify the configuration, but the functions are different:

The starter is a set of several coordinates in one coordinate. I used to write multiple ones, but now I write one, which is used to reduce the amount of writing that depends on the configuration.

Parent defines hundreds of dependency version numbers. In the past, you needed to manually control the version when writing dependencies. Now it is managed by SpringBoot , so that there is no version conflict, which is used to reduce dependency conflicts.

Tips

SpringBoot officially gives many definitions of starter, which is convenient for us to use, and the names are in the following format

Naming convention: spring-boot-starter-technical name

Seven, guide class introduction
After the configuration is finished, we found that SpringBoot really helped us reduce a lot of configuration work. Let's talk about how the program works. At present, the entry for the program to run is the class that comes with the SpringBoot project when it is created, the class with the main method, and running this class can start the SpringBoot project. Mine is this:

@SpringBootApplication _
public class Springboot0101Application {
public static void main( String[] args ) {
SpringApplication.run ( Springboot0101Application.class, args );
}

Write code to test, first create a User class and put it in the container

@Component
public class User {
}

Then write another BookController class and put it in the container as well

@RestController _
@RequestMapping ("/books" )
public class BookController {
@GetMapping ("/ getBooks " )
public String getBooks ( ) {
System.out.println ("The springboot program is running~");
return " Hello, SpringBoot is running";
}
}

Take a look at the directory structure of my corresponding class:

Finally write the code to test:

@SpringBootApplication _
public class Springboot0101Application {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext
= SpringApplication.run(Springboot0101Application.class, args);
BookController bookBean = applicationContext.getBean(BookController.class);
System.out.println("The message of bookBean : " + bookBean);
User userBean = applicationContext.getBean ( User.class );
System.out.println ("The message of userBean : " + userBean );
}
}

operation result:

Seeing the results, it's not hard for my friends to guess - SpringBoot program startup creates a Spring container object, right? The answer is this!

The Springboot0101Application class is the entry point for all functions in the SpringBoot program, and this class is called the bootstrap class.

The most typical feature of being a bootstrap class is that an annotation @SpringBootApplication is declared above the current class

Clicking into @SpringBootApplication , we can see:

Here are the package scanning annotations we used when we learned SSM before, and then click to enter @SpringBootConfiguration :

We can find that it finally uses the @Configuration annotation, so, in the final analysis, the reference class we use is also a configuration class.

8. Embedded Tomcat
1. Tomcat definition location
The program is now running, via the main method of the bootstrap class. But shouldn't running a java program end when it's executed? But we are obviously starting a web server now, otherwise how can the web page be accessed normally ? Where is this server written ?

Think about it, it's in the spring-boot-starter-web scene starter we introduced, let's open it and take a look:

There is a core coordinate, tomcat-embed-core, called tomcat embedded core. It is this thing that introduces the tomcat function into our program.


2. Tomcat operating principle
Let's talk about the second question, how does this server work?

Tomcat server is a piece of software, and it is a software developed in java language. Since it is developed in java language, it must conform to the principle of java program operation when running. What is the operation of java program? Objects, everything is an object, everything is an object. What about tomcat running? is also an object.

If it is an object, the Spring container is used to manage the object. Can this object be handed over to the Spring container for management? The answer is yes! The tomcat server is actually running in the Spring container in the form of an object. No wonder we did not install this tomcat, and it still works. In the daytime, this thing finally exists in the form of an object, which is stored in the Spring container and runs quietly. What exactly is running? In fact, it is the tomcat embedded core mentioned above.

The specific embedded core dependencies are as follows:


org.apache.tomcat.embed
tomcat-embed-core
9.0.58
compile


tomcat-annotations-api
< groupId > org.apache.tomcat




3.SpringBoot . Replace the embedded Tomcat


Since it is an object, if the object is removed from the Spring container, will it not have the function of a web server? Of course, this web server functionality can be removed by dependency exclusion. According to the working mechanism of SpringBoot , what technology is used and what dependencies are added. I chose the built-in server jetty provided by SpringBoot

The replacement code is as follows:


org.springframework.boot
spring-boot-starter-web


org.springframework.boot
spring-boot-starter-tomcat




< groupId > org.springframework.boot
< artifactId >spring-boot-starter-jetty


Let's run it and see what the result is:

There is no problem with the output, but the server is not the default Tomcat, but the jetty server I chose

9. SpringBoot.Summary


SpringBoot greatly simplifies the development steps, and we need to use it proficiently. The blogger is actively preparing blog articles related to the specific details of the SpringBoot project. If you like it, you can give the blogger a like ~~