Spring Boot 核心配置简介

Banner图标

spring boot 项目启动时默认加载 src/main/resources目录下的 banner.txt 图标文件,如果该文件不存在,则使用SpringBoot默认Banner。

在线生成banner网址:http://patorjk.com/software/taag/#p=display&f=Graffiti&t=Type%20Something%20

如果启动项目时不想启动图标,可以通过代码进行关闭。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package org.example;

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

@SpringBootApplication
public class Starter {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(Starter.class);
// 关闭 banner 图标
springApplication.setBannerMode(Banner.Mode.OFF);
springApplication.run();
}
}

配置文件

Spring Boot 项目默认会读取全局配置文件,配置文件名固定为 application.propertiesapplication.yml,放在 src/main/resources 目录下,使用配置文件来修改 SpringBoot 自动配置的默认值。

yaml 规则

  • k: v 表示键值对关系,冒号后面必须有一个空格
  • 使用空格的缩进表示层级关系,空格数目不重要,只要是左对齐的一列数据,都是同一层级的
  • 大小写敏感
  • 缩进时不允许使用Tab键,只允许使用空格
  • 松散表示,java中对于驼峰命名法,可用原名或使用 “-“ 代替驼峰,如 java 中的 lastName ,在 yml 中可以使用 lastNamelast-name 都可以正确映射。
application.yml
1
2
3
4
5
6
7
8
9
10
11
server:
port: 8081
servlet:
context-path: /admin
spring:
datasource:
type: com.mchange.v2.c3p0.ComboPooleDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/hr?useUnicode=true&characterEncoding=utf8
username: root
password: root

Starter依赖及自动化配置

Starter依赖配置

Spring Boot 引入了全新的Starter依赖体系,简化了项目开发。

Web Starter

使用Spring Mvc 构建 RESTful Web 应用,并使用 Tomcat 作为默认内嵌容器。

pom.xml
1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

视图 Starter

集成视图引擎

pom.xml
1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
pom.xml
1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

JavaMail

pom.xml
1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

aop

pom.xml
1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

自动化配置

核心配置类是 WebMvcAutoConfiguratioin 可以查看此类源码,了解SpringBoot的核心配置项目。
所有在配置文件中能配置的属性都是在 xxxxProperties 类中封装着

Spring Boot 自动化配置项目在 org.springframework.boot: spring-boot-autoconfigure 的 META-INF 下面的 spring.factories 中。

Profile 配置

Profile是Spring 用来针对不同环境对不同配置提供支持的全局 Profile 配置, 使用 application-{profile}.yml ,比如 application-dev.yml application-test.yml

通过在 application.yml 中设置 spring.profiles.active = test|dev|prod 来动态切换不同环境,如

application-dev.yml
1
2
server:
port: 8081
application-prod.yml
1
2
server:
port: 9000
application.yml
1
2
3
spring:
profiles:
active: dev

在一个文件中使用— 进行隔离不同的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
spring:
profiles:
active: dev
---
spring:
profiles: dev
server:
port: 8091
---
spring:
profiles: test
server:
port: 8092
---
spring:
profiles: pro
server:
port: 8093