Spring Boot学习笔记 01

这是暑期学习第二天,从今天起我开始学习关于SpringBoot的框架,并将学习的内容写到博客上,供大家一起学习!

0x01 Spring Boot简介

​ Spring Boot是Spring家族中的一个全新的框架,它用来简化Spring应用程序的创建和开发过程,也可以说Spring Boot能简化我们之前采用(SSM)Spring+SpringMVC+Mybatis框架进行开发的过程。

​ 在以往我们采用SSM框架进行开发的时候,搭建和整合三大框架,我们需要做很多工作,比如配置web.xml,配置Spring,配置mybatis,并将他们整合在一起,而SpringBoot框架对此开发过程进行了革命性的颠覆,完全抛弃了繁琐的xml配置过程,采用了大量的默认配置简化我们的开发过程。

​ 所以采用SpringBoot可以非常容易和快速地创建基于Spring框架的应用程序,它让编码变简单了,配置变简单了,部署变简单了,监控变简单了。正因为Spring Boot化繁为简,让开发变得极其简单和快速,所以在业界变得备受关注。

0x02 Spring Boot的特性

  • 能够快速创建基于Spring的应用程序
  • 能够直接使用Java Main方法启动内嵌的Tomcat 服务器运行Spring Boot程序,不需要部署War包文件
  • 提供约定的starter POM来简化 Maven 配置,让Maven配置变得简单
  • 自动化配置,根据项目的Maven依赖配置,SpringBoot自动配置Spring、SpringMVC等等
  • 提供了程序的健康检查等功能
  • 基本可以完全不使用XML配置文件,采用注解配置

0x03 Spring Boot 四大核心

  • 自动配置

  • 起步依赖

  • Actuator(健康检测)

  • 命令行界面

0x04 创建SpringBoot项目

IDEA新建SpringBoot项目,勾选Spring Web。

创建后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
<!--SpringBoot父工程GAV坐标-->	
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--当前项目的GAV坐标-->
<groupId>com.example.springboot</groupId>
<artifactId>springboot001</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>springboot001</name>
<description>Demo project for Spring Boot</description>
<!--编译级别-->
<properties>
<java.version>1.8</java.version>
</properties>
<!-- 依赖-->
<dependencies>
<!-- SpringBoot框架Web项目起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot框架测试起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<!-- SpringBoot项目打包编译的插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

SpringBoot项目启动入口类:

src\main\java\com.example.springboot.springbootfirst\Springboot001Application.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.example.springboot.springbootfirst;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//Springboot项目启动入口类
@SpringBootApplication//SpringBoot核心注解,主要用于开启spring自动配置

public class Springboot001Application {

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

}
  • src\main\resources
    • static(文件夹):图标等静态资源放入此文件夹
    • templates(文件夹):模板,前端模板引擎放入此文件夹Thyme leaf.html(百叶箱)
    • application.properties:SpringBoot的核心配置文件

Springboot项目代码必须放到Application(Springboot001Application)类所在的同级目录或者下级目录

我们在com.example.springboot.springbootfirst下创建一个package Web,再在web这个包里面创建一个java类IndexController:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.example.springboot.springbootfirst.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class IndexController {
@RequestMapping(value = "/springboot/say")
public @ResponseBody String say() {
return "Hello ,Springboot!";
}
}

完成之后我们编译Springboot001Application这个入口,观察控制台输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.2)

2021-07-21 17:09:53.836 INFO 10616 --- [ main] c.s.s.Springboot002SpringMvcApplication : Starting Springboot002SpringMvcApplication using Java 11.0.10 on LAPTOP-47F3TRCJ with PID 10616 (C:\Users\段雅婷\Desktop\SpringBoot\springboot002-springMVC\target\classes started by 段雅婷 in C:\Users\段雅婷\Desktop\SpringBoot)
2021-07-21 17:09:53.836 INFO 10616 --- [ main] c.s.s.Springboot002SpringMvcApplication : No active profile set, falling back to default profiles: default
2021-07-21 17:09:54.634 INFO 10616 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-07-21 17:09:54.650 INFO 10616 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-07-21 17:09:54.650 INFO 10616 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.48]
2021-07-21 17:09:54.697 INFO 10616 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-07-21 17:09:54.697 INFO 10616 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 830 ms
2021-07-21 17:09:54.931 INFO 10616 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-07-21 17:09:54.931 INFO 10616 --- [ main] c.s.s.Springboot002SpringMvcApplication : Started Springboot002SpringMvcApplication in 1.409 seconds (JVM running for 2.861)
2021-07-21 17:10:38.256 INFO 10616 --- [nio-8080-exec-1] o.apache.tomcat.util.http.parser.Cookie : A cookie header was received [1626485908,1626487413,1626505943] that contained an invalid cookie. That cookie will be ignored.
Note: further occurrences of this error will be logged at DEBUG level.
2021-07-21 17:10:38.261 INFO 10616 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-07-21 17:10:38.261 INFO 10616 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-07-21 17:10:38.261 INFO 10616 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 0 ms

其中我们发现一条提示信息Tomcat started on port(s): 8080 (http) with context path ‘’

打开浏览器输入localhost:8080/springboot/say,观察到浏览器界面跳转到输出字符串“Hello ,Springboot!”。

这里我们完成了SpringBoot项目的搭建。

0x05 使用SpringBoot的核心配置文件

上面我们说,其中一条信息为Tomcat started on port(s): 8080 (http) with context path ‘’,我们要如何修改这里的端口号和上下文默认路径呢?

我们打开src\main\resources中的application.properties文件并添加:

1
2
3
4
#设置内嵌Tomcat端口号
server.port=8081
#设置上下文根
server.servlet.context-path=/springboot

再重启Springboot项目,观察控制台输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.2)

2021-07-21 17:32:42.607 INFO 15028 --- [ main] .s.s.Springboot003ContextPathApplication : Starting Springboot003ContextPathApplication using Java 1.8.0_181 on LAPTOP-47F3TRCJ with PID 15028 (C:\Users\段雅婷\Desktop\SpringBoot\springboot003-context-path\target\classes started by 段雅婷 in C:\Users\段雅婷\Desktop\SpringBoot)
2021-07-21 17:32:42.610 INFO 15028 --- [ main] .s.s.Springboot003ContextPathApplication : No active profile set, falling back to default profiles: default
2021-07-21 17:32:43.477 INFO 15028 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http)
2021-07-21 17:32:43.493 INFO 15028 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-07-21 17:32:43.493 INFO 15028 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.48]
2021-07-21 17:32:43.554 INFO 15028 --- [ main] o.a.c.c.C.[.[localhost].[/springboot] : Initializing Spring embedded WebApplicationContext
2021-07-21 17:32:43.554 INFO 15028 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 893 ms
2021-07-21 17:32:43.775 INFO 15028 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path '/springboot'
2021-07-21 17:32:43.775 INFO 15028 --- [ main] .s.s.Springboot003ContextPathApplication : Started Springboot003ContextPathApplication in 1.607 seconds (JVM running for 2.537)

我们可以看到 Tomcat started on port(s): 8081 (http) with context path '/springboot' 修改端口号和默认路径成功。

如果我们要访问http://localhost:8080/springboot/say 则要改成 http://localhost:8080/springboot/springboot/say ,因为添加了默认路径。

核心配置文件只能有一个

0x0501 使用.yml或.yaml的核心配置文件

将application.properties换成application.yml:

1
2
3
4
server:
port: 8081
servlet:
context-path: /

将application.properties换成application.yaml:

1
2
3
4
server:
port: 8081
servlet:
context-path: /

三种格式的配置文件都能使用,但是只能存在一个