Three ways to build a SpringBoot project (super detailed version)

Table of contents

1. Download and decompress the compressed package from the official website

2. Build through Idea scaffolding

3. Spring Boot project structure

3.1 pom.xml file

3.2 Startup class

3.3 Configuration file

4. Add dependencies by creating a Maven project


1. Download and decompress the compressed package from the official website

Next, we build a SpringBoot project and introduce the functions of SpringMVC. First, we can build the project through the official website:

1. Visit start.spring.io

Then we can click Add depenencies on the upper right to enter the dependencies we need

We can search what dependencies we want to add in the search box above and add them directly. For example, if we want to add Spring Web dependencies, as shown below:

Then customize the project name, select the project type, Maven, and project language: java

Then directly Generate

It has been downloaded, we unzip it directly to the directory we like, and then open it through Idea:

After the dependencies below are introduced, click Run directly: If you see the picture below, it means the operation is successful, and you can start our SpringBoot learning.

SpringBoot version notes:

  • SNAPSHOT: Snapshot version, which represents the development version and may be modified at any time;
  • M1 (Mn): M1 is the abbreviation of milestone, which is the milestone version;
  • RC1 (RCn): RC1 is the abbreviation of release candidates, which is the release preview version;
  • Release: official version, it may also mean official version without any suffix.

SpringBoot packaging type description:

What is being developed using SpringMVC is a web project, but since Web containers such as Tomcat are directly embedded in SpringBoot, there is no need to deploy War files when using SpringBoot for web development, and only need to be packaged into Jar packages.

2. Build through Idea scaffolding

Okay, now let’s talk about how to build a SpringBoot project directly on Idea:

1. Select Spring Initializer in the list on the left, which means this is a Spring Boot project

2. Project source address, because spring is a foreign server, so we usually use Alibaba Cloud's website to build it faster.

3. Click Settings and change the URL to: https://start.aliyun.com/

Then enter our project name Name, a Java version, packaging type, and click Next

1. Select the Spring Boot project. Since this is a domestic image, Alibaba Cloud’s version is generally lower than the official website.

2. Similarly, we search for the dependencies we need to add in the search box, for example, add a web dependency here. Then click Finish directly

After clicking Finish, the project we just created will be opened directly. You can see that a demo has been added to us here.

OK, we run it directly. The following page appears, which means that we have successfully built and run the project.

3. Spring Boot project structure

Next, let's analyze the Spring Boot project structure.

3.1 pom.xml file

1. SpringBoot projects must inherit spring-boot-starter-parent, that is, all SpringBoot projects are sub-projects of spring-boot-starter-parent. Common configuration, dependencies, plug-ins and other information are defined in spring-boot-starter-parent for inheritance and use by SpringBoot projects.

In fact, in some newer versions, there is no need to inherit the parent project...

Startup dependencies can be defined in the SpringBoot project. Startup dependencies are not in units of jar packages, but in units of functions. After all, it is an idea that convention is greater than configuration. Anyone who sees a dependency with the word "stater" basically does not need to add a dependency version, because This has all been configured in the parent project parent.

The spring-boot-maven-plugin plug-in is a plug-in that packages projects into jar packages. The SpringBoot project packaged by this plug-in does not need to rely on the web container and can be run directly using JDK.

3.2 Startup class

The function of the startup class is to start the SpringBoot project. Running the main method of the startup class can start the SpringBoot project.

Note that the startup class must be placed under the outermost package, because after the project is started, the loaded files and packages are all in the same directory as the startup class.

3.3 Configuration file

Since SpringBoot greatly simplifies Spring configuration, there is only one application.properties configuration file, and Spring's automatic configuration function makes most configurations have default configurations. The function of this file is to overwrite the default configuration information, and this file does not write any information. can start the project. The default port number after startup is 8080, we can override this configuration:

Note: The loading priority of the configuration file is to select config if there is config, and scan the project directory if there is a project directory. The following is the configuration file scanning priority.

  1. conifg directory under the project directory
  2. Configuration files in the project directory
  3. config directory under resources directory
  4. Configuration files in the resources directory

When we start the project, we can find that the port has changed to 8888.

4. Add dependencies by creating a Maven project

OK, after understanding the project structure of the Spring Boot project, we can build the Spring Boot project directly through Maven. Just introduce the corresponding SpringBoot dependencies and build the resource directory belonging to SpringBoot

1. Use Maven to build the Maven project:

Select the project JDK version and click Finish

Customize the project name and location, then click finish.

OK, after the project is created, we only need to introduce relevant dependencies in the pom.xml file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.7.16-SNAPSHOT</version>
    </parent>

    <dependencies>
        <!-- Spring MVC依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- springBoot的Test依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
            </plugin>
        </plugins>
    </build>

</project>

1. Introduce the parent project, 2. Introduce relevant dependencies, 3. Add the tomcat plug-in, 4. Refresh the pom file

OK, then we add the startup class. First, add the package, com.example.demo, under the source program directory (java), and then add the SpringBootDemo startup class to the package:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

Then click Run, OK, as shown in the picture below, it runs successfully.

The above are the three ways to create a SpringBoot project. Use whichever one you like.

Guess you like

Origin blog.csdn.net/qq_53317005/article/details/133105261