[Spring Boot] (1) Creation and use of Spring Boot projects


foreword

In today's software development world, Spring Boot is a Java framework that gets a lot of attention. It provides developers with a simplified and efficient way to build Java applications . This article mainly introduces the concept and advantages of Spring Boot, and demonstrates how to create Spring Boot projects in different ways, and finally demonstrates how to output "Hello World" through Spring Boot.

1. What is Spring Boot

1.1 Getting to know Spring Boot first

Spring Boot is an open source framework for building Java applications. It is a sub-project of Spring Framework, so it can be said that the Spring Boot project is also a Spring project . Spring Boot aims to simplify and accelerate the development process of Spring applications. By providing default configuration and automatic configuration, developers can focus more on the implementation of business logic without paying too much attention to tedious configuration and integration issues.

Simply put,Spring 的诞生是为了简化 Java 程序的开发,而 Spring Boot 的诞生则是为了简化 Spring 程序的开发 .

From the translation of Spring Boot (Spring scaffolding), it can be seen that scaffolding is built to build houses, and the purpose is to speed up the construction of houses; Spring scaffolding means that it was created to speed up the development of Spring programs.

1.2 The core design idea of ​​Spring Boot

The core design idea of ​​Spring Boot is 约定大于配置(Convention Over Configuration)and自动配置(Auto-configuration) .

1. Convention over configuration

Spring Boot is used 一系列的约定to provide default behavior, so that developers do not need to configure manually in most cases, thereby reducing the complexity of the project . For example, when creating a Spring Boot web project, it will configure an embedded web server by default, without requiring us to explicitly configure the Tomcat or Jetty container.

2. Automatic configuration
The Spring Boot project will automatically configure and assemble various functions for the application according to the dependencies of the application and the components in the class path . This means that we can introduce some specific dependencies into the project, and Spring Boot will automatically configure the corresponding functions based on these dependencies, which simplifies many manual configuration processes.

1.3 Advantages of Spring Boot

Learning and using Spring Boot has brought us a lot of convenience, such as:

  1. Improve development efficiency : Spring Boot's 约定大于配置and 自动配置functions make the development process more efficient. It greatly reduces the writing of boilerplate code, allowing developers to focus more on the implementation of business logic, thereby improving development efficiency .

  2. Simplified configuration : Spring Boot has many built-in default configurations, so that developers do not need to manually configure a large number of options . Not only can reduce the chance of error, but also greatly reduce the complexity of the project.

  3. Embedded server : Spring Boot includes commonly used web servers, such as Tomcat, Jetty, etc., which make it very simple to package the application into an executable JAR file and run it . There is no need to deploy an additional independent web server, and the application can be easily deployed to various environments.

  4. Microservice support : Spring Boot provides good support for building a microservice architecture. It can easily create and manage various microservices. Through the Spring Cloud project, it can also realize service registration and discovery, load balancing, circuit breaker mode and other microservices. Function.

  5. A large number of starting dependencies : Spring Boot provides many pre-configured "Starter" dependencies, covering a variety of commonly used libraries and frameworks, such as Spring Data JPA, Spring Security, Thymeleaf, etc. These starter dependencies simplify the process of integrating other frameworks, making it easier to build feature-rich applications.

2. Creation of Spring Boot project

2.1 Create with IDEA

When using IDEA to create a Spring Boot project, if you are using the community edition, you first need to install Spring Boot Helperthe plug-in, but it should be noted that if the version of IDEA is version 21 or earlier, because this plug-in has been charged since version 22.

Of course, if it is a professional version, you don't need to install this plug-in, it is VIP after all. The following demonstrates a Spring Boot project created using IDEA 2022 Professional Edition.

  1. First, use IDEA to create a new project

As shown in the figure below:

Note that the selected JDK version and the default Java version must be consistent, otherwise the creation will fail.

  1. Click Next, select the Spring Boot version and the required dependencies

When selecting the version, since JDK 8 is used, the selected Spring Boot version is 2.xx, and the 3.xx version needs to use a newer version such as JDK17.

Then select the required dependencies,

  1. Finally, choose Create to create.

    If you can see the startup class and a triangle symbol that can run, it means the creation is successful.

2.2 Create with webpage

When using IDEA to create a Spring Boot project, we found a start.spring.io address, open this address, you can create a Spring Boot project directly on the web page.

  1. open url


At this point you can discover a variety of options. Among them, the left side is some basic configuration options, while the right side is the selection of required dependencies.

  1. select configuration


The selected content is exactly the same as that selected when using IDEA to create.

  1. choose to depend

  1. download zip file


Click GENERATEto download the compressed package, and of course you can click next EXPLOREto preview it.

  1. Unzip the compressed package just downloaded, and then use IEDA to open the decompressed file

Unzip:

Open with IEDA:


5. At this point, a Spring Boot project has been successfully created using the web page

In addition, you can also use Alibaba Cloud start.aliyun.com to create, and the creation method is exactly the same:

2.3 Directory structure of the project

When a Spring Boot project is created, you can see its directory structure as shown below:

This is the directory structure of a typical Spring Boot project. The following is a brief description of each directory:

  • src/main/java: The directory where the main Java code is stored. This usually contains the main code logic of the project, such as controller (controller), model (model), data access layer (repository), etc.

  • src/main/resources: The main resource file storage directory. Here, configuration files, static resource files, template files, etc. can be placed. application.propertiesOr application.ymlgenerally used to configure the properties of the application.

  • src/test: The directory where the test code is stored. Here, various test cases can be written to test the program code.

  • pom.xml: The configuration file of the Maven project. Maven is the default build tool for the Spring Boot project, and configures the project's dependencies and build settings through the pom.xml file.

  • .mvn: It contains Maven Wrapper-related files for performing Maven builds and other Maven tasks without manually installing Maven.

  • mvnwAnd mvnw.cmd: This is the Maven Wrapper's script to execute Maven commands without installing Maven.

  • HELP.md: A help documentation file in Markdown format. It is used to provide a brief introduction about the project, frequently asked questions (FAQ), common commands, quick start guide, etc., to help developers quickly understand and use the project.

3. Hello World

3.1 Run the startup class

Click the method of the startup class mainto run the Spring Boot project, and the startup is successful, as shown in the figure below:

By starting the printed log, you can find that the default port number is 8080:

3.2 Output Hello World through the browser page

demoCreate a subdirectory under the directory and controllercreate a HelloControllerclass inside it. This is a simple Spring Boot controller (Controller) class that handles /helloHTTP requests from routes and returns a "Hello World" string in response.

@Controller
@ResponseBody
public class HelloController {
    
    

    @RequestMapping("/hello")
    public String hello(){
    
    
        return "Hello World";
    }
}

The explanation for this code is as follows:

  • @Controller: This annotation marks the class as a Spring controller class. It tells the Spring framework that this is a controller that handles HTTP requests.

  • @RequestMapping("/hello"): This annotation /hellomaps paths to hello()methods. When a client sends a request to /hello, Spring Boot will call hello()the method to process the request.

  • @ResponseBody: This annotation tells Spring to return the return value of the method directly as the content of the HTTP response instead of jumping to a view page. In this example, hello()the "Hello World" string returned by the method is returned directly to the client as an HTTP response.

This simple Spring Boot program demonstrates how to create a basic controller that handles an HTTP request and returns a response. After running this program, when visiting in the browser http://localhost:8080/hello, you will see the string "Hello World" output on the browser as a response.

3.3 The embodiment of agreement is greater than configuration

In the first Spring Boot program above, we can see that the convention is greater than the configuration, as follows :

  1. Naming conventions : In Spring Boot, by default, the naming conventions of controller classes and methods determine their behavior .

    • In this example, HelloControllerthe class is marked as @Controller, so Spring Boot will recognize it as a controller class, and handle HTTP requests.
    • hello()method is marked as @RequestMapping("/hello")such that it handles /helloHTTP requests from the path.
    • These default naming conventions eliminate a lot of configuration, so that developers do not need to manually configure the mapping between controller classes and methods and request paths.
  2. @ResponseBodyAnnotation : In hello()the method, the annotation is used @ResponseBody, which tells Spring Boot to return the return value of the method directly as the content of the HTTP response.

    • This means we don't need to hello()define an additional view template for the method, since the returned "Hello World" string will directly become the content of the response.
    • This convention simplifies the process of handling responses, allowing developers to focus on business logic without worrying about view rendering.
  3. Default configuration : Spring Boot provides a large number of default configurations, such as the default web server (such as: Tomcat, Jetty), default data source configuration, default log configuration, etc. These default configurations can meet the needs in most cases, reducing the work of manual configuration. Developers only need to perform configuration overrides when necessary.

Through these conventions, we do not explicitly perform a large number of configurations in the above code, but rely on Spring Boot's conventions and default configurations. This makes the code concise and easy to read, reduces unnecessary redundant code, and improves development efficiency. This is a typical embodiment of convention over configuration , making the development of Spring Boot applications easier and more efficient.

Guess you like

Origin blog.csdn.net/qq_61635026/article/details/132118556