项目搭建
方式一
通过Maven来进行项目的创建
1、创建一个空项目 testSprBt (Empty Project)
2、Maven和Annotation配置
- Maven Settings TODO
- Enable annotation processing
3、给项目添加Module(Name=springBoot01)
SpringBoot已经内嵌了Tomcat
4、导入依赖
(1)spring-boot-starter-parent
通过继承父项目方式(依赖传递)
https://mvnrepository.com/(搜索 spring-boot-starter-parent) --> https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent/2.3.12.RELEASE
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
<parent>
<!-- <dependency> -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
<type>pom</type>
<!-- </dependency> -->
</parent>
(2)spring-boot-starter-web
启动器:总依赖(整合其他技术首先考虑导入启动器)(本质上就是一个依赖)(表示jar包的坐标)
- Spring自己封装的spring-boot-starter-xxxx
- 第三方公司提供的xxxx-spring-boot-starter
例如,MVC模式Controller启动器为spring-boot-starter-web
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
</dependencies>
5、业务逻辑代码(以controller为例)
package com.test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@ResponseBody
@RequestMapping("firstController")
public String firstController(){
return "hello springboot";
}
}
6、启动项目(定义启动类)
启动类:启动SpringBoot项目,基于Main方法来运行(项目的启动入口)
package com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBoot01Application {
public static void main(String[] args) {
SpringApplication.run(SpringBoot01Application.class,args);
}
}
7、Web访问
http://localhost:8080/firstController
方式二
核心点:依赖导入方式 + IDEA集成插件
1、创建新Module
2、用另一种方式导入依赖(不依赖parent)
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies </artifactId>
<version>2.4.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
</dependencies>
3~6 同上
7、Web访问
方式三
IDEA SpringInitializr快速创建SpringBoot项目(集成功能)
启动原理
TODO