Shandong University Software College Project Training-Innovative Training-Shandong University Software Institute Network Attack and Defense Shooting Range Experimental Platform (4)


Foreword:

The previous article briefly described the basic use of the springboot framework. This article briefly analyzes the underlying principles and operating mechanism of springboot.

Course video notes: https://www.bilibili.com/video/BV1PE411i7CV


1. Project structure analysis

The basic project is created through the above steps. The following files are automatically generated.

1. The main startup class of the program

2. An application.properties configuration file

3. A test class

4. A pom.xml

pom.xml analysis

Open pom.xml and analyze the dependencies of the Spring Boot project:

  1. parent dependency
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.5.RELEASE</version>
    <relativePath/>
</parent>
  1. web scene launcher
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  1. springboot unit tests
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <!-- 剔除依赖 -->
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>
  1. Packaging plugins
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>


2. Simple analysis of the operating principle

Study the SpringBoot project in the previous article, how does it work, first of all a Maven project, we generally start from the pom.xml file.


1. Parent Dependency

Among them, it mainly depends on a parent project, mainly to manage the resource filtering and plug-ins of the project!

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

Click in and find that there is also a parent dependency

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.2.5.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
</parent>

This is where all the dependency versions in the SpringBoot application are really managed, the version control center of SpringBoot;

In the future, when we import dependencies, we do not need to write the version by default; but if the imported package is not managed in the dependency, we need to manually configure the version.


2. Starter spring-boot-starter

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

springboot-boot-starter-xxx: is the scene starter of spring-boot

spring-boot-starter-web: Helps us import the components that the web module depends on for normal operation;

SpringBoot extracts all functional scenarios and makes them into starters (starters) one by one. We only need to introduce these starters into the project, and all related dependencies will be imported. We can import what kind of function we want to use. The scene launcher will do. Of course, we can also customize the starter ourselves.


3. Main startup class

After analyzing pom.xml, let's take a look at this startup class

Default main startup class

//@SpringBootApplication 来标注一个主程序类
//说明这是一个Spring Boot应用
@SpringBootApplication
public class SpringbootApplication {
    
    

   public static void main(String[] args) {
    
    
     //以为是启动了一个方法,没想到启动了一个服务
      SpringApplication.run(SpringbootApplication.class, args);
   }

}

In fact, a simple startup class is not simple. Its annotations will do many things. We will not conduct in-depth research here. After all, it is not the main purpose of our course to deeply understand the operating mechanism and principle of springboot. The goal is to use springboot to build the project. So it's basically enough to learn here.


4. Process analysis of run method

Paste a process analysis diagram described in the Mad God course
insert image description here
(the picture comes from the public account: Mad God Says)



References

Reference blog:
https://blog.csdn.net/cowbin2012/article/details/88861160
https://www.cnblogs.com/shamo89/p/8184960.html
https://www.jianshu.com/p/ef6f0c0de38f
https://www.cnblogs.com/jstarseven/p/11087157.html
https://www.cnblogs.com/gslblog/p/7986279.html


Reference video:
https://www.bilibili.com/video/BV1PE411i7CV
https://www.bilibili.com/video/av838087622/

Guess you like

Origin blog.csdn.net/m0_47470899/article/details/123357443