SpringBoot【基础篇】---- 快速上手
1. 入门案例(4 种方式)
1. 基于 IDEA 创建 SpringBoot 项目
2. 基于 SpringBoot 官网创建项目
- 打开 SpringBoot 官网,选择 Quickstart Your Project
- 创建工程,并保存项目
- 解压项目,通过 IDE 导入项目
3. 基于阿里云创建项目
- 选择 start 来源为自定义 URL
- 输入阿里云 start 地址
- 创建项目
阿里云提供的坐标版本比较低,如果需要使用高版本,进入工程后手工切换 SpringBoot 版本
阿里云提供的工程模板与 Spring 官网提供的工程模板略有不同
- 选择 start 来源为自定义 URL
- 输入 阿里云 start 地址
- 创建项目
4. 手工创建项目(手工导入坐标)
<?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.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot_01_01_quickstart</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_01_01_quickstart</name>
<description>springboot_01_01_quickstart</description>
<properties>
<java.version>17</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>
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Springboot0101QuickstartApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot0101QuickstartApplication.class, args);
}
}
- 创建普通 Maven 工程
- 继承 spring-boot-starter-parent
- 添加依赖 spring-boot-starter-web
- 制作引导类 Application
2. SpringBoot 概述
1. parent
- 开发 springboot 程序要继承 spring-boot-starter-parent
- spring-boot-starter-parent 中定义了若干个依赖管理
- 继承 parent 模块可以避免多个依赖使用相同技术时出现依赖版本冲突
- 继承 parent 的形式也可以采用引入依赖的形式实现效果
2. starter
starter
- springboot 中常见项目名称,定义了当前项目使用的所有依赖坐标,以达到减少依赖配置的目的
parent
- 所有 springboot 项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),以达到减少依赖冲突的目的
- spring-boot-starter-parent 各版本间存在这诸多坐标版本不同
实际开发
- 使用任意坐标时,仅书写 GAV 中的 G 和 A,V 由 SpringBoot 提供,除非 SpringBoot 未提供对应版本 V
- 如发生坐标错误,再指定 Version(要小心版本冲突)
- 开发 springboot 程序需要导入坐标时通常导入对应的 starter
- 每个不同的 starter 根据功能不同,通常包含多个依赖坐标
- 使用 starter 可以实现快速配置的效果,达到简化配置的目的
3. 引导类
启动方式
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Springboot0101QuickstartApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot0101QuickstartApplication.class, args);
}
}
SpringBoot 的引导类是 Boot 工程的执行入库,运行 main 方法就可以启动项目
SpringBoot 工程运行后初始化 Spring 容器,扫描引导类所在包加载 bean
- SpringBoot 工程提供引导类用来启动程序
- SpringBoot 工程启动后创建并初始化 Spring 容器
4. 辅助功能(内嵌 tomcat)
- 内嵌 Tomcat 服务器是 SpringBoot 辅助功能之一
- 内嵌 Tomcat 工作原理是将 Tomcat 服务器作为对象运行,并将该对象交给 Spring 容器管理
- 变更内嵌服务器思想是去除现有服务器,添加全新的服务器