> For the complete documentation index, see [llms.txt](https://spring-boot.shujuwajue.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://spring-boot.shujuwajue.com/ji-chu-jiao-cheng/pei-zhi-wen-4ef6-duo-huan-jing-pei-zhi.md).

# 3.配置文件-多环境配置

## 一.多环境配置的好处：

1.不同环境配置可以配置不同的参数

2.便于部署，提高效率，减少出错

## 二.Properties多环境配置

1.配置激活选项

```
spring.profiles.active=dev
```

2.添加其他配置文件

![](/files/-LfnTHutotVju2-jz9tY)

## 三．YAML多环境配置

1.配置激活选项

```
spring:
  profiles:
    active: dev
```

2.在配置文件添加三个英文状态下的短横线即可区分

```
---
spring:
  profiles: dev
```

示例：

src/main/resources/下的 application.properties 重命名为 application.yaml

```
#配置文件环境配置
spring:
  profiles:
    active: dev

  #jackson
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: Asia/Chongqing

#端口
server: 
  port: 8888

---
spring:
  profiles: dev
server:
  port: 8080

---
spring:
  profiles: prod
server:
  port: 8082

---
spring:
  profiles: test
server:
  port: 8081
```

## 四．两种配置方式的比较

1. Properties配置多环境，需要添加多个配置文件，YAML只需要一个配件文件
2. 书写格式的差异，yaml相对比较简洁，优雅
3. YAML的缺点：不能通过`@PropertySource`注解加载。如果需要使用`@PropertySource`注解的方式加载值，那就要使用properties文件。

## 五．如何使用

```
java -jar myapp.jar --spring.profiles.active=dev
```
