一、初识springboot
1.1 springboot的概述
通过前面ssm三大框架的学习,在我们做项目的过程中,我们发现,由许多配置信息需要我们去配置,比如数据源连接池,事务,组件的注册,AOP;静态资源的访问,控制器的配置,编码器 等。有了SpringBoot后,就可以简化我们配置。让我们开发的效率得以提升。
所谓的springboot实际上本质就是Spring,只不过它封装了Spring,SpringMVC,Spring DATA。它就是一个可以快速开发一个单体应用或者产品级的应用,可以使用少量或者没有配置就能够让应用运行起来的一个框架。
1.2 spring boot的特点
- 创建独立的 Spring 应用程序
- 直接嵌入Tomcat,Jetty或Undertow(无需部署WAR文件)
- 提供固执己见的“入门”依赖项以简化构建配置
- 尽可能自动配置 Spring 和第三方库
- 提供生产就绪功能,例如指标、运行状况检查和外部化配置
- 绝对无需生成代码,也无需 XML 配置
1.3 spring boot的作用
1.4 为什么要使用springboot?
根据springboot的特点和作用,很容易得出为什么要使用springboot了。
- Spring Boot 可以轻松创建可以“直接运行”的独立的、生产级的基于 Spring 的应用程序。
- 对 Spring 平台和第三方库持固执己见的看法,因此您可以轻松上手。 大多数 Spring Boot 应用 程序需要最少的 Spring 配置。
1.5 springboot的缺点
凡事都是双面的,既有好的一面也有坏的一面,既然它高度封装了SpringMVC和Spring框架,那么他的源码就一定相对这两个框架难。还有就是使用者很多,那么它的版本更新就会恒快。
二、入门
相信各位读者都是学完ssm三大框架,才来到小编这里,那么各位一定是学过maven搭建项目了,所以小编在此就不介绍maven了,没有学习maven的读者建议先学习完maven在来这里!!!
springboot技术更新很快,那么小编就从3.1.1入手了和大家一起学习,这里springboot要求我们的JDK版本是17及以上,maven要求是3.8.3及其以上,所以建议大家检查一下自己的版本,尽量按照springboot的要求来,否则容易出现bug还不好解决!!!
2.1 创建maven工程
首先创建一个空的工程,然后创建一个模块,如下所示:
2.2 在项目中引入pom.xml依赖
1、把 SpringBoot 工程作为我们的父工程
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.1</version>
</parent>
2、然后在 pom.xml 文件中引入 spring-boot-starter-web 起步器
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3、在 pom.xml 文件中配置springboot打包插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.3 编写启动类
在创建好的工程中编写一个启动类
@SpringBootApplication
public class FirstDemoApplication {
public static void main(String[] args) {
SpringApplication.run(FirstDemoApplication.class, args);
}
}
2.4 编写一个控制器
@RestController
public class FirstDemoController {
@GetMapping("/first")
public String first() {
return "hello spring boot";
}
}
2.5 启动测试
启动页面如下所示
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.1.0)
2023-07-20T10:06:10.257+08:00 INFO 17620 --- [ main] com.openlab.FirstDemoApplication : Starting FirstDemoApplication using Java 17.0.7 with PID 17620 (D:\Workspace\Idea\zx720-software\spring-boot-first-demo\target\classes started by Jock in D:\Workspace\Idea\zx720-software)
2023-07-20T10:06:10.260+08:00 INFO 17620 --- [ main] com.openlab.FirstDemoApplication : No active profile set, falling back to 1 default profile: "default"
2023-07-20T10:06:11.221+08:00 INFO 17620 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-07-20T10:06:11.234+08:00 INFO 17620 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-07-20T10:06:11.235+08:00 INFO 17620 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.8]
2023-07-20T10:06:11.331+08:00 INFO 17620 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-07-20T10:06:11.332+08:00 INFO 17620 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1031 ms
2023-07-20T10:06:11.704+08:00 INFO 17620 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-07-20T10:06:11.712+08:00 INFO 17620 --- [ main] com.openlab.FirstDemoApplication : Started FirstDemoApplication in 1.812 seconds (process running for 2.508)
打开浏览器,在地址栏中输入 http://localhost:8080/first 就可以看到如下信息:
hello spring boot
2.6 项目配置
在 springboot 应用中,通过有两种配置方式:
-
application.properties
-
application.yml|application.yaml
下面我们详细来讲解 yml 格式的配置文件的语法。
1、基本语法
在 yml 文件中,使用的层层递进的配置方式。YML(YAML),YAML Ain't Markup Language,是 YAML 的一种标记语言。它适合用于配置文件的编写。
1)它是 key - value 对象,而 key 和 value 之间使用 英文冒号
和 至少一个空格
来编写
2)大小写敏感
3)使用缩进的方式来表示层级关系
4)缩进的空格数量不限,只要左边对齐即可
5)使用 #
来进行注释
6)字符串可以加双引号,也可以加单引号,也可以不加引号
server:
port: 8082
names:
name: springboot
@RestController
public class SecondController {
@Value("${names.name}")
private String company;
@GetMapping("hello")
public String hello() {
return "hello: " + company;
}
}
问题:什么时候需要用于引号?
-
当使用变量时
server: port: 8082 openlab: name: "西安${server.port}"
-
当值中有空格时
openlab: name: "西安 大雁塔"
注意:在 springboot 3.x 中使用变量也可以不使用引号了。但是作为一种好的习惯我们还是推荐使用引号
-
当值中有特殊字符时
openlab: name: "西安\n大雁塔*高度^"
2、数字类型
-
字面量:是一种不可再分的值,如 hello, 18
-
对象:
-
array:数组
1)编写一个配置类
@Data @Configuration @ConfigurationProperties(prefix = "user") public class User { private String[] names; }
2)在yml文件中配置数组
user: # names: ["刘备", "关羽", "张飞"] names: - 刘备 - 关羽 - 张洋
3)在控制器中使用
@RestController public class SecondController { @Value("${openlab.name}") private String company; @Resource private User user; @GetMapping("hello") public String hello() { Arrays.stream(user.getNames()).forEach(System.out::println); return "hello: " + company; } }
-
list:集合:它的配置方式和array一样
-
set:集合:它的配置方式和array一样
-
map:集合
1)定义属性
@Data
@Configuration
@ConfigurationProperties(prefix = "user")
public class User {
private String[] names;
private List<Integer> ages;
private Map<String, String> maps;
}
2)配置map格式的数据
user:
maps: { k1: v1, k2: v2, k3: v3}
maps:
k1: v4
k2: v5
k3: v6
3)使用map格式数据
@GetMapping("hello")
public String hello() {
Set<Map.Entry<String, String>> entries = user.getMaps().entrySet();
Iterator<Map.Entry<String, String>> iterator = entries.iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
System.out.println(entry.getKey() + " : " + entry.getValue());
}
return "hello: " + company;
}
3、使用细节
-
birthDay 推荐使用 birth-day 来进行配置
如果以 |开头,那么大文本写在下层,保留文件格式,换行符等
user:
likes:
- cat
- dog
- pig
- |
spring
springboot
springcloud
-
如果以 > 开头,折叠换行符
user:
likes:
- cat
- dog
- pig
- >
spring
springboot
springcloud
三、自动配置
3.1 启动详情
在 SpringBoot 中它帮我们管理了很的依赖,所以在项目启动时可以自动的装配进行,从而可以使用。
3.2 依赖管理
在 SpringBoot 项目中我们必须做 SpringBoot 子项目,所以我们需要在工程加入如下的配置。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.1</version>
</parent>
当我们点击 spring-boot-starter-parent
进入到这个 starter 中会发现它还有一个父工程 spring-boot-dependencies
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.1.0</version>
</parent>
<artifactId>spring-boot-starter-parent</artifactId>
继续进入到这个父程中,发现在这个工程管理了很的版本
<properties>
<activemq.version>5.18.1</activemq.version>
<angus-mail.version>1.1.0</angus-mail.version>
<artemis.version>2.28.0</artemis.version>
<aspectj.version>1.9.19</aspectj.version>
<assertj.version>3.24.2</assertj.version>
<awaitility.version>4.2.0</awaitility.version>
<brave.version>5.15.1</brave.version>
<build-helper-maven-plugin.version>3.3.0</build-helper-maven-plugin.version>
<byte-buddy.version>1.14.4</byte-buddy.version>
<cache2k.version>2.6.1.Final</cache2k.version>
<caffeine.version>3.1.6</caffeine.version>
<cassandra-driver.version>4.15.0</cassandra-driver.version>
<classmate.version>1.5.1</classmate.version>
...............
同时还可以看到它的版本仲裁信息:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-amqp</artifactId>
<version>${activemq.version}</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-blueprint</artifactId>
<version>${activemq.version}</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
<version>${activemq.version}</version>
................省略