In the last article, we gave you an overview of Spring Boot features, pros and cons. Today, let’s get started on the first SpringBoot project

Environment to prepare

Compiler: IDEA

JDK version: 1.8

Build tool: Maven

I. New Project

We suggest that you use IDEA to create projects, easy to operate and quick. In daily coding, it can get twice the result with half the effort.

Step 1: Open IDEA and click Create New Project to enter step 2

Step 2: Select Sping Initializer on the left, set JDK and Initializer addresses on the right, use the default address https://start.spring.io/, and click Next

Step 3: Check the project information automatically generated by the system and click Next

Step 4: Select dependencies. Because in step 2 we selected the service address for project initialization https://start.spring.io/, IDEA will load all dependencies in this address for us

Let’s just choose the Web and the frequently used Lombook dependencies, and click Next

Step 5: Click Finish to complete the project creation

Tips: People often create projects in a different way. On the start.spring. IO/page, configure the spring Boot project name and dependencies. After the configuration is complete, download the configuration file to the local PC and import IDEA

In fact, the two ways are the same, I am more used to directly create this way in IDEA, IDEA directly help us load all the dependencies in the web page, but also save the decompression, and then open the project steps. More convenient and faster

Ii Project Catalog

The created project directory is shown below

namely

demo 
 +-src 
  +-main 
    +-java 
      +- com.shumile.demo 
        +- DemoApplication.java  
  +-resources 
     +-static 
     +-templates 
     +-application.properties 
 +-test 
 +-pom.xml
Copy the code

Main Contents:

SRC /main/ Java: Program development and main program entry

SRC /main/resources: configuration file

SRC /test/ Java: test program

Main documents:

1 DemoApplication.java – Project unique startup class (annotated @SpringBootApplication)

2 static Stores static resources accessed on the Web

3 Templates Stores template files

4 Application. properties Project configuration file

5 Pom. XML Configures the maven dependent file

Looking at the POM file, we can see that the two dependencies we just selected, IDEA combined with Spring Initializer, are configured for us in pom

Test dependencies are automatically configured for quick unit testing.

Once the project is open, you first need to configure Maven and JDK separately. Once the configuration is complete, click the import Changes TAB that pops up. Downloadable dependencies

After the dependency download is complete, you can start the project

Iii Start-up Project

Right-click DemoApplication to start the class and start the project directly

Check log: Started DemoApplication in2.444 seconds (JVM Running for3.307) is successfully Started

Lo and behold, we have successfully created and launched the project with such ease

To get more intuitive, let’s create a Web interface that accesses our project directly from the page.

Create a new Controller class as follows:

import org.springframework.web.bind.annotation.GetMapping;
​
import org.springframework.web.bind.annotation.RequestMapping;
​
import org.springframework.web.bind.annotation.RestController;
​
@RestController
​
@RequestMapping("/")
​
public class HelloController {
​
@GetMapping("/hello")
​
public String helloWorld() {return "hello,world~";
​
}}

Copy the code

Restart the service, in the browser input address: http://localhost:8080/hello,

results

The first Spring Boot project is ten times easier than building a framework in Spring.

What does Spring Boot do for us

This is largely thanks to Spring Boot’s convention over configuration and starter-dependency invincibility

1 Automatic Configuration

For example, for the service port, SpringBoot is configured to 8080 by default. The interface requests the prefix configuration item server.servlet.context-path, which is configured as/by default. If we set this configuration item to “/shumile”

server.servlet.context-path=/shumile
Copy the code

At this point, the request page will return 404

2 Startup Dependence

Spring Boot (I) : As mentioned in the feature overview, spring-boot-starter-Web dependency is introduced in the project, that is, tomcat and webMvc and other related dependencies are introduced, so our server will directly use the Tomcat server to start and support webMvc function.

Spring Boot (1) : Features overview has mentioned the specific principles, in future articles, I will also continue to mention ~ you will experience more and more in the future in the implementation of more features.

Six summarize

If you have any questions, please feel free to leave a message and I will answer them patiently. If you want to know more about Spring Boot or Java, you can also leave a message and interact with me

If you’ve always been interested in software, you’ve rarely had a chance to learn it. If you are interested in Java, if you have Spring or Spring Boot related development experience, want to systematically review it, welcome to pay attention to our public number – green main code, together with easy learning Spring Boot ~