SpringBoot environment construction and startup procedures

1: IDEA environment preparation

IDEA Community Edition version: 2021.1-2022.1.4

IDEA Professional Edition: No requirement

If the IEDA installed on your personal computer is not in this range, you need to uninstall and reinstall it; and you must delete the registry

Reference article? IDEA uninstalls and deletes the registry

Two: Maven

(1) Concept of Maven

Maven is a project/software management tool that obtains jar packages through the configuration of the pom.xml file without having to add jar packages manually

(2)The role of Maven

Simple and convenient, improve our development efficiency and reduce our development bugs

Maven provides many functions; such as project construction, dependency management…

(3) Create Maven project

①File?new?project in the upper left corner

②Select Maven, then next, and then configure the name and path to complete

③Understand the functions of different directories

(1)pom.xml: Mainly the most important configuration file of the Maven project

(2)src: Mainly used to store Java source code

(3)main: mainly put some business code

(4)main/Java: Mainly used to store Java code

(5)main/resources: Mainly used to store some dependent resources (pictures, audio…)

(6)test: mainly put some test code

(4)Maven core functions

1. Project construction

Maven provides us with a set of simple commands to complete the construction of the project

Click Maven on the far right and click Lifecycle, which contains the commands provided by Maven

Maven common commands

2.Manage dependencies

① Dependencies: refers to third-party libraries, frameworks, etc. required by the current project

②Introduce the corresponding dependencies in the pom.xml file

(There are a large number of dependencies for different functions in the Maven central warehouse? Maven central warehouse)

③ Steps to introduce dependencies

(1) First, add the tag in the pom.xml file

(You can put many dependencies in the tag)

(2) Then add the required dependencies in the tag

(For example, I introduce a mysql dependency)

(3) Click the refresh button to introduce the newly added dependencies

(You must click refresh to ensure that the dependency appears and takes effect)

3. Dependency transfer

Since Maven’s dependencies are transitive, other jar packages it depends on will be automatically imported

For example, A depends on B, and B depends on C. Then when A is introduced, B and C also need to be introduced

4. Dependency exclusion

①Function:Actively disconnect dependent resources

(Excluded resources do not need to be versioned)

② Reason for use:As the project becomes more and more complex, the dependencies between libraries will also become more and more complex; sometimes we do not need to use certain dependencies. Or there is a conflict between some dependent versions, then we will disconnect it

(For example, in the picture above, if project A does not require JarB, it can also be achieved by excluding dependencies)

③Method:Maven Help plug-in

(More details below)

(5)Maven HelpPlugin

1. Function

Question: When the project is more complex, we will have the problem of Jar package conflict. At this time, we need to resolve the dependency conflict; before resolving the conflict, we need to find the conflict first, so how to find the conflict?

We can use the Maven Help plug-in to observe the dependencies between packages and find dependency conflicts

2.Download

Install plugin:

File? Settings? Plugins? Search Maven Help? Find the corresponding plug-in? Click Install to install

(You need to restart IDEA after installation to take effect)

3. Function

At this time, there is a Dependency Analyzer at the bottom of pom.xml. Click on it to see the relationship between dependencies

(1)Conflicts: dependency conflicts

(2)All Dependencies as List: Display dependencies in sorted form

(3)All Dependencies as Tree: Display dependencies in a tree format

You can exclude dependencies by right-clicking on the dependency and selecting Exclude

(6) About coordinates and warehouses

1.Coordinates

In Maven, a jar package is uniquely identified based on the configuration of groupId, artifactId, and version. Each one is indispensable; we call this code similar to the following coordinates

2. Warehouse

①Essence: The essence of the Maven warehouse is a directory/folder. This directory is used to store all dependencies in development, such as jar packages, plug-ins, etc.

②Function: Used to store resources and manage various jar packages

③Category: central warehouse, local warehouse, private server

After we configure the dependencies in the pom file and click refresh, Maven will search for the Jar package in the warehouse based on the coordinate configuration, download it, and add it to the project

(7) Central Warehouse

1. Concept

① There are two remote warehouse addresses built into the Maven software, which is the central warehouse

②The Maven central warehouse serves the entire Internet; it is maintained by the Maven team and is the only one in the world

(The central warehouse is abroad, the local warehouse is on the local computer)

2.Address

Central warehouse address? Central warehouse

(The central warehouse stores many jar packages and dependencies. We will look for the required dependencies in the central warehouse)

3. How to find the required dependencies

①Open the central warehouse address

②Enter the jar package or dependent keywords you are looking for

(The following picture uses mysql as an example)

③Select the dependency or jar package version

④Click on the version and scroll down to find the page as shown below

(8)Local warehouse

1. Concept

A directory on your local computer, use this directory to store jar packages

(The central warehouse is abroad, the local warehouse is on the local computer)

2. Search

When the corresponding dependent jar package is introduced into the project, it will first check whether there is a corresponding jar package in the local warehouse

(1) If yes, quote it directly in the project

(2) If not, go to the central warehouse to download the corresponding jar package to the local warehouse

3.Address

The local warehouse address can be viewed through Maven configuration

(Local repository is the local warehouse address)

(9)Private server

1. Concept

Private warehouses generally built by company teams

Private servers belong to a certain company or department and often require certain permissions

2. Search

After having the private server, the order of downloading Maven dependencies has changed again

When Maven needs to download resources

1. Get it from the local warehouse first. If the local warehouse exists, return directly

2. If the local warehouse does not have it, request it from the private server. If the resource exists in the private server, it will return directly

3. If the resource does not exist on the private server, it will be downloaded from the central warehouse. If the central warehouse does not exist, an error will be reported

4. If it exists in the central repository, it will be cached on the private server first, then cached in the local repository, and then provide services for Maven download requests

(10)Maven sets domestic sources

1. Reason

Because the central warehouse is abroad, downloading will be slow, so we choose to use some public remote warehouses in China to download resources

2. Domestic source warehouse

Complete address of domestic source warehouses?Complete list of domestic source warehouses

3. Configure the current project Setting

① Find the address of setting.xml

② If there is no setting.xml file at this time, just open Baidu Cloud and copy the setting.xml inside to the .m2 directory

Baidu Cloud Address?setting.xml (password: lzh7)

?

③If there is a setting.xml file at this time, you need to open settings.xml and add the following content on the mirrors node

(Here is Alibaba Cloud as an example)

<mirror>
        <id>aliyunmaven</id>
        <mirrorOf>central</mirrorOf>
        <name>Ah? Cloud Public Warehouse</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
4. Set the Settings of the new project

The above configuration is only effective for the current project. In order to make it effective for subsequent new projects, you need to reset the Settings of the new project

?

Three: The first SpringBoot program

? Frequently asked questions about creating SpringBoot projects: Frequently asked questions about creating projects (extraction code: lzh7)

(1) Understanding SpringBoot

SpringBoot is a framework. We know that Spring makes Java programs faster, simpler and safer, and SpringBoot is designed to simplify Spring program development

Servlet: Servlet needs to write doGet, doPost and other methods, and also needs to configure Smart Tomcat

SpringBoot: After SpringBoot is written, it is run directly through DemoApplication.java without the need to configure anything

(2)Install SpringBootHelp plug-in

1. For versions

① Only those using the community version of IDEA need to install the SpringBootHelper plug-in

② There is no need to install plug-ins when using the professional version of IDEA, IDEA has been integrated

2. Find the plug-in and download

Don’t click Install directly here. Click Install directly. The paid version is installed (if you are rich, please feel free to do so)

?

Click Plugin homepage to enter the web page to download the plug-in

(The download is a compressed package, but remember not to unzip it, just put it in the directory you want)

?

?

3. Install plug-in

①Click on the gear-shaped settings above

②Click Install Plugin from Disk

③Select the plug-in you just downloaded, install it, and restart Idea

?

After restarting IEDA, you can see Spring Initializr and Assistant, as shown in the figure below

?

(3) Create SpringBoot project

1. New project

File?New?Project?Spring Initializr

?

2. Understand the project

The recommendation is the same as the picture I chose

①Project propertiesProject performance

?

②The following picture explains

(1) SpringBoot version: Choose any 2.X version without SNAPSHOT. This version will change with the upgrade of SpringBoot and is not fixed.(The 3.X version uses jdk17, not recommended; SNAPSHOT indicates an unstable version)

(2) Check to indicate third-party dependencies introduced at the beginning of the project, such as frameworks, plug-ins, components, etc.

(3) All referenced third-party frameworks

?

(4) Modify SpringBoot’s pom.xml

Find the code as shown below and delete .RELEASE

?

(5) Directory introduction of SpringBoot program

①Four useless directories or files

②.idea: Some configurations related to IEDA

③src/main/java: Java source code is placed; this is where the Java code runs

④src/main/resources: contains static resources or configuration files

(1)static: static resource folder; mainly stores static files and can be accessed directly

(Such as js, css, html and other static files, pages that do not require server data for binding)

(2)templates: template resource folder; mainly stores dynamic template files

(Such as JSP, Freemarker, Thymeleaf and other files that require the server to dynamically render data)

(3)application.properties: Configuration file of SpringBoot project; very important

(Detailed introduction in later articles)

⑤src/test/java: contains the test code source code

(Note: The developer’s test code is placed here; it has nothing to do with the tester)

⑥pom.xml: Maven configuration file

(6) Running of SpringBoot program

Find the picture below and run SpringBoot

?

The operation was successful as shown in the picture below

(7)SpringBoot observe startup log

(8)SpringBoot writes code and runs

① Under src/main/java, find the com.example.demo package, create a controller package under this package, and then create a java file named HelloController

②Write the following code under the HelloController file

package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloController {
    @RequestMapping("/sayhi")
    public String sayHi(){
        return "hello,SpringBoot";
    }
}

③Rerun SpringBoot

④Start the web page

⑤Code Analysis