The first SpringBoot program
I have put all the source code of SpringBoot on the link below. The link contains the source code and tutorials of SpringBoot integration other technologies that I have compiled, as well as other learning materials of SpringBoot. Welcome everyone to download and learn. If this tutorial is helpful to you, Please also star support, thank you!
Source link: https://gitee.com/oldou/springbootstudy
Introduction to SpringBoot
Spring Boot is a framework, a brand-new programming specification. Its production simplifies the use of the framework. The so-called simplification refers to the simplification of a large number of and cumbersome configuration files required in many frameworks in Spring, so Spring Boot is a framework that serves The framework of the service is to simplify the configuration file. So in essence, Spring Boot is actually
another manifestation of the Spring framework.
Features of Spring Boot
- Use Spring Boot to create a standalone Spring application
- Web containers such as Tomcat, Jetty, Undertow, etc. are directly embedded in Spring Boot, so there is no need to deploy WAR files when using SpringBoot for web development
- Simplify project construction and configuration by providing its own Starter dependencies
- Try to automatically configure Spring and third-party libraries
- Provides production-ready features, such as metrics, health checks and external configuration
- Absolutely no code generation and no need for XML configuration files
Spring Boot version introduction
SNAPSHOT: Snapshot version, that is, development version.
CURRENT: The latest version, but not necessarily the stable version.
GA: GeneralAvailability, the officially released version.
Create the first SpringBoot project using scaffolding
Ready to work
We will learn how to quickly create a Spring Boot application and implement a simple Http request processing. Through this example, have a preliminary understanding of Spring Boot and experience its simple structure and rapid development characteristics.
My computer environment is: JDK1.8, Maven3.5.4, SpringBoot2.3.x (the latest version)
Development tool: IDEA
Create project
Open IDEA, create a new project, use the picture below:
pom file analysis
<!--父依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!--web启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--SpringBoot的单元测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<!--剔除依赖-->
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<!--打包插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
A lot of Maven dependencies and configurations are provided in the parent dependency spring-boot-starter-parent. Regarding the configuration, you can click into the spring-boot-starter-parent to view it. It contains resource copy plugins and other configurations. Not only that, It also provides a large number of Maven dependencies. For details, you can click spring-boot-dependencies under spring-boot-starter-parent to view.
Now we begin to write our first SpringBoot program.
Write an http interface
1. Create a new controller package in the same level directory of the main program. It must be in the same level directory, otherwise it will not be recognized.
2. Create a new HelloController class in the package
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "Hello My First SpringBoot Code !!";
}
}
3. After writing, start the project from the main program, the browser initiates a request, and the page returns; the console outputs the port number accessed by Tomcat!
In a few simple steps, the development of a web interface is completed, and SpringBoot is that simple. So we often use it to build our microservice projects!
Change startup icon
How to change the letters of the characters displayed at startup?
In fact, that is a banner pattern. To realize a custom pattern, only one step is required: create a new banner.txt in the resources directory under the project.
For the pattern, you can go to: https://www.bootschool.net/ascii This website generates it, and then copy it to the file!