提前配置好环境:
JDK 1.8.0_201(及以上版本)
Apache Maven 3.6.0
IntelliJ IDEA 2020
IDEA欢迎页
1.初始化maven
2.初始化JDK设置
一、构建Spring Boot项目
两种方式:Maven方式(1.1运行文件+3.1测试文件)、Spring Initiallizr快捷方式(1.2即全部)。
1.Maven方式构建Spring Boot项目
该方式所有文件及依赖需手工创建(测试项目在后文三,这里仅运行文件的构建)
(1)创建Maven项目
(2)在pom.xml中添加Spring Boot相关依赖
两个依赖:1)统一父类项目管理;2)Web依赖启动器
<!-- 引入Spring Boot依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<dependencies>
<!-- 引入Web场景依赖启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
(3)编写主程序启动类
(ps:好像必须在包内创建类)
标记该类为主程序启动类:@SpringBootApplication
启动主程序类方法:SpringApplication.run()
@SpringBootApplication
public class ManualChapter01Application {
public static void main(String[] args){
SpringApplication.run(ManualChapter01Application.class,args);
}
}
(4)创建一个用于Web访问的Controller
@RestController == Spring中@Controller+@ResponseBody注解
@GetMapping("/hello") == Spring框架中@RequestMapping(RequestMethod.GET)注解
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello Spring Boot";
}
}
(5)运行项目
启动项目,在浏览器上访问 http://localhost:8080/hello
2.Spring Initiallizr快捷方式构建Spring Boot项目
该方式构建条件:连接网络
该方式将自动生成项目运行与测试文件夹及相关依赖(测试在后文三,这里已生成测试相关)
(1)创建Spring Boot项目
创建好的项目结构如下图所示:
(2)创建一个用于Web访问的Controller
(3)运行项目
2-3同上4-5
二、基础配置
1.核心依赖
1)spring-boot-starter-parent依赖(统一父类项目依赖管理)
对常用技术框架的依赖文件进行统一的版本号管理。
这样,pom.xml引入受管理的依赖文件无需标注依赖文件版本号。(未受管理的依赖文件需使用<version>标签指定依赖文件版本号)
<!-- Spring Boot父项目依赖管理 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
2)spring-boot-starter-web依赖(Web依赖启动器)
提供Web开发场景所需的底层所有依赖文件,对Web开发场景所需的依赖文件进行统一管理。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.Spring Boot自动配置
Spring Boot应用的启动入口是@SpringBootApplication注解标注类中的main()方法
@SpringBootApplication能够扫描Spring组件并自动配置Spring Boot。
@SpringBootApplication =
@SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan
1)@SpringBootConfiguration:表示Spring Boot配置类,可被组件扫描器扫描
2)@EnableAutoConfiguration :表示开启自动配置功能
3) @ComponentScan:组件包扫描器,用于将指定包中的注解类自动装配到Spring的Bean容器中
3.Spring Boot执行流程
主程序启动类 -> 启动项目main()方法 -> 执行方法中SpringApplication.run() = 启动整个Spring Boot程序
三、单元测试
1.在pom文件中添加spring-boot-starter-test测试启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2.编写单元测试类
@RunWith(SpringRunner.class) :加载Spring Boot测试注解
@SpringBootTest:加载项目的ApplicationContext上下文环境
@RunWith(SpringRunner.class)
@SpringBootTest
public class Chapter01ApplicationTests {
@Test
public void contextLoads() {
}
}
3.编写单元测试方法
@Autowired //注入了HelloController实例对象
private HelloController helloController;
@Test
public void helloControllerTest() {
String hello = helloController.hello();
System.out.println(hello);
}
即整个测试类为:
@RunWith(SpringRunner.class)
@SpringBootTest
public class Chapter01ApplicationTests {
@Autowired
private HelloController helloController;
@Test
public void helloControllerTest() {
String hello = helloController.hello();
System.out.println(hello);
}
}
4.运行结果
四、热部署
避免修改后反复重启服务。开启热部署无须开发人员手动重启项目。
1.在pom文件中添加spring-boot-devtools热部署依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>