SpringBoot——Hello入门(一)

概述

Spring Boot 提供了一组工具只需要极少的配置就可以快速的构建并启动基于 Spring 的应用程序。解决了传统 Spring 开发需要配置大量配置文件的痛点。

同时 Spring Boot 对于第三方库设置了合理的默认值,可以快速的构建起应用程序。

当然 Spring Boot 也可以轻松的自定义各种配置,无论是在开发的初始阶段还是投入生成的后期阶段。

优点

快速的创建可以独立运行的 Spring 项目以及与主流框架的集成。

使用嵌入式的 Servlet 容器,用于不需要打成war包。

使用很多的启动器(Starters)自动依赖与版本控制。

大量的自动化配置,简化了开发,当然,我们也可以修改默认值。

不需要配置 XML 文件,无代码生成,开箱即用。

准生产环境的运行时应用监控。

与云计算的天然集成。

创建一个springBoot项目

用idea可以快速的创建一个SpringBoot项目,项目结构如下:

SpringBoot——Hello入门(一)_2020-05-12-15-33-16.png

依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.qn</groupId>
<artifactId>springboot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-demo</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

spring-boot-starter-parent 是Spring Boot 的核心依赖,它里面定义了各种在开发中会用到的第三方 jar 的版本信息,因此我们在引入其他的 Spring Boot 为我们封装的启动器的时候都不在需要指定版本信息。如果我们需要自定义版本信息,可以直接覆盖版本属性值即可。

spring-boot-starter-web 提供 web 以及 MVC 和 validator 等web开发框架的支持。

spring-boot-starter-test 提供测试模块的支持。如 Junit,Mockito。

spring-boot-maven-plugin springboot提供maven打包的plugin。

Spring Boot 为我们提供了很多的已经封装好的称为启动器(starter)的依赖项。让我们在使用的时候不需要再进行复杂的配置就可以迅速的进行应用集成。所有的官方启动器依赖可以在这里查看。

启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@SpringBootApplication
public class SpringbootDemoApplication {

public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}

@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
// 开始检查spring boot 提供的 beans
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
};
}
}

控制器

1
2
3
4
5
6
7
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}

访问测试

SpringBoot——Hello入门(一)_2020-05-12-15-45-09.png

文章目录
  1. 1. 概述
  2. 2. 优点
  3. 3. 创建一个springBoot项目
    1. 3.1. 依赖
    2. 3.2. 启动类
    3. 3.3. 控制器
    4. 3.4. 访问测试
|