SpringBoot入门程序开发

Springboot 初衷

SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程。

  • SpringBoot程序优点
    • 起步依赖(简化依赖配置)
    • 自动配置(简化常用工程相关配置)
    • 辅助功能(内置服务器,……)

创建 SpringBoot 工程的四种方式

  • 基于Idea创建SpringBoot工程
  • 基于官网创建SpringBoot工程
  • 基于阿里云创建SpringBoot工程
  • 手工创建Maven工程修改为SpringBoot工程

基于Idea创建SpringBoot工程

①:创建新模块,选择Spring Initializr,并配置模块相关基础信息

image-20230524001822755

②:选择当前模块需要使用的技术集

image-20230524001512848

③:开发控制器类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package cn.aiyingke.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* Author: Rupert Tears
* Date: Created in 0:24 2023/5/24
* Description: Thought is already is late, exactly is the earliest time.
*/
@RestController
@RequestMapping("/demo")
public class DemoController {
@GetMapping
public String test() {
System.out.println("DemoController is running...");
return "success";
}
}

④:运行自动生成的Application类

image-20230524003521612

最简SpringBoot程序所包含的基础文件

  • pom.xml文件
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
<?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>3.1.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.aiyingke</groupId>
<artifactId>springboot-base-create-method-01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-base-create-method-01</name>
<description>springboot-base-create-method-01</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>
  • Application类
1
2
3
4
5
6
7
8
9
10
11
12
13
package cn.aiyingke;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootBaseCreateMethod01Application {

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

}

总结:

  1. 开发SpringBoot程序可以根据向导进行联网快速制作
  2. SpringBoot程序需要基于JDK进行制作
  3. SpringBoot程序中需要使用何种功能通过勾选选择技术
  4. 运行SpringBoot程序通过运行Application程序入口进行

基于官网创建SpringBoot工程

基于SpringBoot官网创建项目,地址:https://start.spring.io/

image-20230524004616611

  1. 打开SpringBoot官网,选择Quickstart Your Project
  2. 创建工程,并保存项目
  3. 解压项目,通过IDE导入项目

基于阿里云创建SpringBoot工程

基于阿里云创建项目,地址:https://start.aliyun.com

注意事项

  • 阿里云提供的坐标版本较低,如果需要使用高版本,进入工程后手工切换SpringBoot版本
  • 阿里云提供的工程模板与Spring官网提供的工程模板略有不同

手工创建Maven工程修改为SpringBoot工程

  • 创建普通Maven工程
  • 继承spring-boot-starter-parent
  • 添加依赖spring-boot-starter-web
  • 制作引导类Application

手工创建项目(手工导入坐标)

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
<?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>3.1.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.aiyingke</groupId>
<artifactId>springboot-base-create-method-01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-base-create-method-01</name>
<description>springboot-base-create-method-01</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>

手工创建项目(手工制作引导类)

1
2
3
4
5
6
7
8
9
10
11
package cn.aiyingke;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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