An overview of
What is Spring Boot?
Spring Boot makes it easy to create stand-alone, production-grade Spring-based applications that you can “run straight”. Spring Boot takes precedence over configuration conventions, designed to get you up and running as quickly as possible. Based on the Spring Framework, is a new generation of Web application development Framework, the picture below to understand the overall picture of Spring Boot:
Spring Boot advantage
-
Automatically.
The Convention over Configuration design concept advocated by Spring Boot simplifies the Configuration of a large number of XML files and Maven dependencies compared to the previous use of the Spring MVC framework. The default values of various configuration items required to be specified by developers are set together with the code generator, which greatly reduces the threshold of Web project development. Later we will experience the power of Spring Boot to build a Web service through a demo
-
Monitor physical
Compared with the traditional Spring MVC framework, another highlight introduced by Spring Boot framework is the built-in monitoring component of the Actuator. Through the monitoring component of the Actuator, we can check the detailed information of applications including automatic assembly, JVM memory usage, garbage collection information and so on
-
Embedded Servlet container
In addition to automatic assembly and monitoring, Spring Boot framework embedded Servlet containers (including Tomcat, Jetty and other traditional Web containers supporting Servlet specifications), through the embedded Servlet container mechanism, developers want to start Web services. A single java-jar command is required, eliminating the need to deploy projects as war packages, reducing the complexity of o&M deployment projects.
Quick start
I used the “Hello World” demo project to experience the power of Spring Boot. Before starting, we need to configure the development environment and development tools:
-
JDK 1.8 +
Spring Boot 2.x requires JDK 1.8 or later. In addition, Spring Boot 2.x is only compatible with Spring Framework 5.0 and later versions.
-
Maven 3.2 +
The dependency build tool for Spring Boot 2.x is Maven, version 3.2 or later. Gradle requires version 1.12 or later.
-
IDEA
IntelliJ IDEA is recommended, Eclipse can also be used as a development tool, in addition, the source of the project will be open source on Github, if you need to download the need to install Git.
Build the project using Spring Initializr
-
Access to the start. Spring. IO /
-
It is recommended to use Maven Project, Java, and Spring Boot 2.4.1. Fill in the basic information of the Project and the dependency information of the Project. Add the Spring Web dependency, as shown in the following figure:
-
Click Generate to download the project package
-
After decompressing, import the Project using idea, File–>New–>Project from Existing Sources… –> Select the uncompressed directory, click [Open], select Maven build tool, and click [Finish]. After a while, Idea will download the dependency packages required by the project.
Build projects using IDEA
-
Select File –> New –> Project… The dialog box for creating a project is displayed
-
Select Spring Initializr and click Next.
-
Fill in the basic information of the project and click “Next”.
-
Select the dependencies for the project, again we select the Spring Web dependencies and click Next.
-
Fill in the project name and storage path [Finish].
Project structure analysis
As shown above, the basic structure of a Spring Boot project consists of three directories
-
src/main/java
The development directory of the program and the entry to the main program, HelloWorldApplication in that directory, the class with the main() method, which is the entire SpringBoot entry used to launch the application.
-
src/main/resources
The static directory stores static files (CSS,js, etc.), the templates directory stores page templates, and the application. Properties directory generates an empty configuration file by default. Add configuration items according to your project development needs.
-
src/man/test
Used to store the unit test code of the project, HelloWorldApplicationTests, the default generated an empty unit test class, it loads a using Spring Boot dictionary configuration function Spring application context
Write your first Controller code
@RestController
public class HelloWorldController {
@RequestMapping("/hello")
public String hello(a) {
return "Hello World"; }}Copy the code
The @RestController annotation is a combined @Controller and @responseBody annotation, indicating that methods under Controller are output in JSON format.
Start HelloWorldApplication the main method of a class, use the browser to http://localhost:8080/hello this endpoint, the page will output the Hello World, is a super simple ~
Program source code
Github:github.com/dragon8844/…
One last word
If this article is helpful to you, or inspired, help pay attention to it, your support is the biggest motivation I insist on writing, thank you for your support.
In addition, pay attention to the public number: black lighthouse, focus on Java back-end technology sharing, covering Spring, Spring the Boot, SpringCloud, Docker, Kubernetes middleware technology, etc.