• 我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml , 用来指定多个环境版本;
  • 注意:如果yml和properties同时都配置了端口,并且没有激活其他环境 , 默认会使用properties配置文件的!
  • 用properties: 可能不生效(乱码),需给idea配置encoding UTF8

    配置文件位置

    SpringBoot会从这四个位置按优先级加载主配置文件,高优先级的配置会覆盖低优先级的配置:
    image.png
    指定位置加载配置文件
    1. java -jar spring-boot-config.jar --spring.config.location=F:/application.properties

yaml

# 可以注入的配置类
person:
  # 随机uuid
  name: yanjing${random.uuid}
  # 随机int
  age: ${random.int}
  happy: true
  birth: 2021/01/01
  # 行内写法
  maps: { k1: v1, k2: v2 }
  # 数组
  lists:
    - a
    - b
    - c
  lists2: [cat, dog, pig]
  dog:
    # 冒号后面的hello是默认值
    name: ${person.hello:hello}_旺财
    age: 5


#SpringBoot的多环境配置,可以选择激活哪一个
server:
  port: 8081
spring:
  profiles:
    active: test
---

server:
  port: 8082
spring:
  config:
    activate:
      on-profile: dev

---
server:
  port: 8083
spring:
  config:
    activate:
      on-profile: test