一、概述
1. 缺点
1) 配置繁琐
2) 依赖繁琐
2. 功能
1) 自动配置
2) 起步依赖
起步依赖本质上是一个 Maven 项目对象模型,定义了对其他库的传递依赖,这些东西加在一起即支持某项功能。
3) 辅助功能
提供了常见的非功能性特性,如嵌入式服务器、安全、指标等。
3. 小结
SpringBoot 提供了一种快速开发 Spring 项目的方式,而不是对 Spring 功能上的增强。
二、SpringBoot 快速入门
1. 需求
搭建 SpringBoot 工程,定义 HelloController.hello()
方法,返回 “HelloSpringBoot!"。
2. 实现步骤
- 创建 maven 项目
- 导入 SpringBoot 起步依赖
- 定义 Controller
- 编写引导类
- 启动测试
3. 实现
3.1 创建项目
项目名称:MySpringBoot
3.2 创建模块
创建一个 maven 模块。
项目名:springboot-helloworld
group ID:com.xuxumiao
ArtifactID:springboot-helloworld
3.3. 配置 pom.xml
根据 spring 官方的参考文档进行操作。
在 pom.xml
文件中引入 springboot 的父工程:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
</parent>
引入依赖:
<dependencies>
<!--web 开发的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3.4 创建 helloworldCrontroller 类
代码如下:
package com.xuxumiao.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author: xxm
* @Date: 2022/1/19 11:05
*/
@RestController
public class helloContoller {
@RequestMapping(value = "/hello")
public String hello(){
return " hello Spring Boot !";
}
}
3.5 创建引导类
创建一个引导类,作为 SpringBoot 项目的入口。代码如下:
package com.xuxumiao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author: xxm
* @Date: 2022/1/19 11:14
*/
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class,args);
}
}
3.6 启动项目
从引导类中运行项目。
成功:
三、快速构建 SpringBoot 工程
1. 创建模块
在项目中创建一个新模块
New Module -> Spring Initializer ,设置如下:
填写完毕后,点击下一步 -> 选择 Spring Web -> 完成。之后会自动下载一些依赖,等待下载完成后即可。
2. 创建 controller 类
代码如下:
package com.xuxumiao.springbotinit.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author: xxm
* @Date: 2022/1/20 15:31
*/
@RestController
public class HelloController {
@RequestMapping("/hello2")
public String hello(){
return "hello spring boot 2!";
}
}
3. 测试
启动项目,访问 localhost:8080/hello2。
成功访问: